Giter Club home page Giter Club logo

facer's Introduction

Facer

Face detection, alignment, and averaging using OpenCV and dlib.

Facer draws heavily on this tutorial from Satya Mallick. I had to update the code pretty heavily to get the project to work, so I thought I'd share my modifications.

Example

The image below is an example of Facer's output for one of my posts on r/dataisbeautiful.

Average faces of rap, rock, and country music

Installation

You have my 100% money-back guarantee that the most difficult part of using this package is installing its requirements. Once you've got OpenCV installed, the rest will should be smooth sailing. I've had the best luck with the OpenCV using the opencv-python package from PyPI.

The requirements.txt file lists the Python packages Facer depends on. Install the packages using pip:

pip install -r requirements.txt

Pre-trained detection model

The face landmark detection relies on a pre-trained model that must be downloaded separately from the dlib package itself.

wget http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

Unzip the compressed file after it finishes downloading and move it into the ./Facer/model directory.

Usage

from facer import facer
import matplotlib.pyplot as plt


# Load face images
path_to_images = "./face_images"  # Put your images here
images = facer.load_images(path_to_images)

# Detect landmarks for each face
landmarks, faces = facer.detect_face_landmarks(images)

# Use  the detected landmarks to create an average face
average_face = facer.create_average_face(faces, landmarks, save_image=True)

# View the composite image
plt.imshow(average_face)
plt.show()

Facer also supports creating animated GIFs of the averaging process:

from facer import facer

path_to_images = "./face_images"
gif, average_face = facer.create_animated_gif(path_to_images)

facer's People

Contributors

johnwmillr avatar meg-huggingface avatar panda7789 avatar veggiedefender avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

facer's Issues

Allow for a different path for the pre-trained model

Currently, the path where the pre-trained model is hardcoded and prevents usage of the library as a module without tweaking. It might be nice if there is a way to set this programmatically (e.g. via a config that the module loads).

cv2 error

Hi, Just trying your example code and I get :

Starting face averaging for 10 faces.
Image 1 / 10
Traceback (most recent call last):
  File "D:\Facer\facer.py", line 13, in <module>
    average_face = facer.create_average_face(faces, landmarks, save_image=True)
  File "D:\Facer\facer\facer.py", line 224, in create_average_face
    dt = calculateDelaunayTriangles(rect, np.array(pointsAvg))
  File "D:\Facer\facer\utils.py", line 62, in calculateDelaunayTriangles
    subdiv.insert((p[0], p[1]))
cv2.error: OpenCV(4.8.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\subdivision2d.cpp:288: error: (-211:One of the arguments' values is out of range)  in function 'cv::Subdiv2D::locate'

Anyone with ideas about this one?

Performance improvements with parallelization?

Hello!

First off, thank you for this awesome work. I've been messing around with it for a couple days now. After some serious yak shaving, I got it working and the results are awesome. The speed leave a lot to be desired though. Especially when working with big data sets (takes me about 3hrs for an 1000 image average).

I tried loading it on a cuda enabled ami but not really getting better results there. I'm not asking for free work here, but since I don't have a CV or ML background myself, curious if you have any tips on how we can get parallel processing going here or otherwise what the bottlenecks are which can be worked on? I might do it or commission it.

Thanks!
J

Error: legacy-install-failure

System

  • OS - Mac OS X 13.0.1
  • Chip - Apple M1
 pip install -r requirements.txt

Output

      error: Command "clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch arm64 -arch x86_64 -g -DNPY_INTERNAL_BUILD=1 -DHAVE_NPY_CONFIG_H=1 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1 -D_LARGEFILE64_SOURCE=1 -DNO_ATLAS_INFO=3 -DHAVE_CBLAS -Ibuild/src.macosx-10.9-universal2-3.1/numpy/core/src/umath -Ibuild/src.macosx-10.9-universal2-3.1/numpy/core/src/npymath -Ibuild/src.macosx-10.9-universal2-3.1/numpy/core/src/common -Inumpy/core/include -Ibuild/src.macosx-10.9-universal2-3.1/numpy/core/include/numpy -Inumpy/core/src/common -Inumpy/core/src -Inumpy/core -Inumpy/core/src/npymath -Inumpy/core/src/multiarray -Inumpy/core/src/umath -Inumpy/core/src/npysort -I/Library/Frameworks/Python.framework/Versions/3.10/include/python3.10 -Ibuild/src.macosx-10.9-universal2-3.1/numpy/core/src/common -Ibuild/src.macosx-10.9-universal2-3.1/numpy/core/src/npymath -Ibuild/src.macosx-10.9-universal2-3.1/numpy/core/src/common -Ibuild/src.macosx-10.9-universal2-3.1/numpy/core/src/npymath -c numpy/core/src/multiarray/buffer.c -o build/temp.macosx-10.9-universal2-3.10/numpy/core/src/multiarray/buffer.o -MMD -MF build/temp.macosx-10.9-universal2-3.10/numpy/core/src/multiarray/buffer.o.d -faltivec -I/System/Library/Frameworks/vecLib.framework/Headers" failed with exit status 1
      [end of output]

image

Saves average faces with holes in them

For some combinations of images, the average face that gets saved has a hole, usually around the mouth area:
image
image

Any idea what this may be due to?

Thanks for your amazing work!

Error running code

Thanks for this interesting project.

I am running your example on a directory of images. I get this error:

Loading face detector and landmark prediction models...
Done, models loaded.

Found 137 in '../faces'.
(1 / 137): ../faces/ffmpeg_1.jpg
(15 / 137): ../faces/ffmpeg_111.jpg
(29 / 137): ../faces/ffmpeg_125.jpg
(43 / 137): ../faces/ffmpeg_138.jpg
(57 / 137): ../faces/ffmpeg_25.jpg
(71 / 137): ../faces/ffmpeg_38.jpg
(85 / 137): ../faces/ffmpeg_50.jpg
(99 / 137): ../faces/ffmpeg_63.jpg
(113 / 137): ../faces/ffmpeg_77.jpg
(127 / 137): ../faces/ffmpeg_9.jpg

Starting face landmark detection...
Processing 137 images.
(1 / 137): ../faces/ffmpeg_1.jpg
(15 / 137): ../faces/ffmpeg_111.jpg
(29 / 137): ../faces/ffmpeg_125.jpg
(43 / 137): ../faces/ffmpeg_138.jpg
(57 / 137): ../faces/ffmpeg_25.jpg
(71 / 137): ../faces/ffmpeg_38.jpg
(85 / 137): ../faces/ffmpeg_50.jpg
(99 / 137): ../faces/ffmpeg_63.jpg
(113 / 137): ../faces/ffmpeg_77.jpg
(127 / 137): ../faces/ffmpeg_9.jpg
Skipped 1.5% of images.

Starting face averaging for 135 faces.
Image 1 / 135
Image 8 / 135
Image 15 / 135
Image 22 / 135
Image 29 / 135
Image 36 / 135
Image 43 / 135
Image 50 / 135
Image 57 / 135
Image 64 / 135
Image 71 / 135
Image 78 / 135
Image 85 / 135
Image 92 / 135
Image 99 / 135
Image 106 / 135
Image 113 / 135
Image 120 / 135
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    average_face = facer.create_average_face(faces, landmarks, save_image=True)
  File "/Users/asi/connor_asi/face_avg/Facer/facer/facer.py", line 221, in create_average_face
    dt = calculateDelaunayTriangles(rect, np.array(pointsAvg))
  File "/Users/asi/connor_asi/face_avg/Facer/facer/utils.py", line 62, in calculateDelaunayTriangles
    subdiv.insert((p[0], p[1]))
cv2.error: OpenCV(4.5.3) /private/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/pip-req-build-tetsazc6/opencv/modules/imgproc/src/subdivision2d.cpp:288: error: (-211:One of the arguments' values is out of range)  in function 'locate'

I'm running OpenCV 4.5.3 if thats helpful.

Any ideas? Thank you!

Modul install on mac os 14.x

Just wanted to give a heads up that the setup.py and installing as a modul does not work - at least not for me.
Could not make numpy 1.6.. Something with clang etc.
But installing latest version of numpy & matlibplot and just running it inside IDE worked fine.

Just changed the import from:
from facer.utils import similarityTransform, constrainPoint, calculateDelaunayTriangles, warpTriangle
to:
from utils import similarityTransform, constrainPoint, calculateDelaunayTriangles, warpTriangle

then I could just run the "create_average_face_from_directory()" function no problem

Thanks for the great work

/// W

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.