You can also read a translated version of this file in Chinese 简体中文版 or in Korean 한국어 or in Japanese 日本語.
Recognize and manipulate faces from Python or from the command line with the world’s simplest face recognition library.
Built using dlib’s state-of-the-art face recognition built with deep learning. The model has an accuracy of 99.38% on the Labeled Faces in the Wild benchmark.
This also provides a simple
face_recognition command line tool that lets you do face recognition on a folder of images from the command line!
Find all the faces that appear in a picture:
import face_recognition image = face_recognition.load_image_file("your_file.jpg") face_locations = face_recognition.face_locations(image)
Get the locations and outlines of each person’s eyes, nose, mouth and chin.
import face_recognition image = face_recognition.load_image_file("your_file.jpg") face_landmarks_list = face_recognition.face_landmarks(image)
Finding facial features is super useful for lots of important stuff. But you can also use it for really stupid stuff like applying digital make-up (think ‘Meitu’):
Recognize who appears in each photo.
import face_recognition known_image = face_recognition.load_image_file("biden.jpg") unknown_image = face_recognition.load_image_file("unknown.jpg") biden_encoding = face_recognition.face_encodings(known_image) unknown_encoding = face_recognition.face_encodings(unknown_image) results = face_recognition.compare_faces([biden_encoding], unknown_encoding)
You can even use this library with other Python libraries to do real-time face recognition:
See this example for the code.
User-contributed shared Jupyter notebook demo (not officially supported):
First, make sure you have dlib already installed with Python bindings:
Then, make sure you have cmake installed:
brew install cmake
Finally, install this module from pypi using
pip2 for Python 2):
pip3 install face_recognition
Alternatively, you can try this library with Docker, see this section.
If you are having trouble with installation, you can also try out a pre-configured VM.
pkg install graphics/py-face_recognition
While Windows isn’t officially supported, helpful users have posted instructions on how to install this library:
When you install
face_recognition, you get two simple command-line programs:
face_recognition- Recognize faces in a photograph or folder full for photographs.
face_detection- Find faces in a photograph or folder full for photographs.
face_recognitioncommand line tool
face_recognition command lets you recognize faces in a photograph or folder full for photographs.
First, you need to provide a folder with one picture of each person you already know. There should be one image file for each person with the files named according to who is in the picture:
Next, you need a second folder with the files you want to identify:
Then in you simply run the command
face_recognition, passing in the folder of known people and the folder (or single image) with unknown people and it tells you who is in each image:
$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/ /unknown_pictures/unknown.jpg,Barack Obama /face_recognition_test/unknown_pictures/unknown.jpg,unknown_person
There’s one line in the output for each face. The data is comma-separated with the filename and the name of the person found.
unknown_person is a face in the image that didn’t match anyone in your folder of known people.
face_detectioncommand line tool
face_detection command lets you find the location (pixel coordinatates) of any faces in an image.
Just run the command
face_detection, passing in a folder of images to check (or a single image):
$ face_detection ./folder_with_pictures/ examples/image1.jpg,65,215,169,112 examples/image2.jpg,62,394,211,244 examples/image2.jpg,95,941,244,792
It prints one line for each face that was detected. The coordinates reported are the top, right, bottom and left coordinates of the face (in pixels).
If you are getting multiple matches for the same person, it might be that the people in your photos look very similar and a lower tolerance value is needed to make face comparisons more strict.
You can do that with the
--tolerance parameter. The default tolerance value is 0.6 and lower numbers make face comparisons more strict:
$ face_recognition --tolerance 0.54 ./pictures_of_people_i_know/ ./unknown_pictures/ /unknown_pictures/unknown.jpg,Barack Obama /face_recognition_test/unknown_pictures/unknown.jpg,unknown_person
If you want to see the face distance calculated for each match in order to adjust the tolerance setting, you can use
$ face_recognition --show-distance true ./pictures_of_people_i_know/ ./unknown_pictures/ /unknown_pictures/unknown.jpg,Barack Obama,0.378542298956785 /face_recognition_test/unknown_pictures/unknown.jpg,unknown_person,None
If you simply want to know the names of the people in each photograph but don’t care about file names, you could do this:
$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/ | cut -d ',' -f2 Barack Obama unknown_person
Face recognition can be done in parallel if you have a computer with multiple CPU cores. For example, if your system has 4 CPU cores, you can process about 4 times as many images in the same amount of time by using all your CPU cores in parallel.
If you are using Python 3.4 or newer, pass in a
--cpus <number_of_cpu_cores_to_use> parameter:
$ face_recognition --cpus 4 ./pictures_of_people_i_know/ ./unknown_pictures/
You can also pass in
--cpus -1 to use all CPU cores in your system.
You can import the
face_recognition module and then easily manipulate faces with just a couple of lines of code. It’s super easy!
API Docs: https://face-recognition.readthedocs.io.
import face_recognition image = face_recognition.load_image_file("my_picture.jpg") face_locations = face_recognition.face_locations(image) # face_locations is now an array listing the co-ordinates of each face!
See this example to try it out.
You can also opt-in to a somewhat more accurate deep-learning-based face detection model.
Note: GPU acceleration (via NVidia’s CUDA library) is required for good performance with this model. You’ll also want to enable CUDA support when compliling
import face_recognition image = face_recognition.load_image_file("my_picture.jpg") face_locations = face_recognition.face_locations(image, model="cnn") # face_locations is now an array listing the co-ordinates of each face!
See this example to try it out.
If you have a lot of images and a GPU, you can also find faces in batches.
import face_recognition image = face_recognition.load_image_file("my_picture.jpg") face_landmarks_list = face_recognition.face_landmarks(image) # face_landmarks_list is now an array with the locations of each facial feature in each face. # face_landmarks_list['left_eye'] would be the location and outline of the first person's left eye.
See this example to try it out.
import face_recognition picture_of_me = face_recognition.load_image_file("me.jpg") my_face_encoding = face_recognition.face_encodings(picture_of_me) # my_face_encoding now contains a universal 'encoding' of my facial features that can be compared to any other picture of a face! unknown_picture = face_recognition.load_image_file("unknown.jpg") unknown_face_encoding = face_recognition.face_encodings(unknown_picture) # Now we can see the two face encodings are of the same person with `compare_faces`! results = face_recognition.compare_faces([my_face_encoding], unknown_face_encoding) if results == True: print("It's a picture of me!") else: print("It's not a picture of me!")
See this example to try it out.
All the examples are available here.
If you want to create a standalone executable that can run without the need to install
face_recognition, you can use PyInstaller. However, it requires some custom configuration to work with this library. See this issue for how to do it.
If you want to learn how face location and recognition work instead of depending on a black box library, read my article.
face_recognition depends on
dlib which is written in C++, it can be tricky to deploy an app using it to a cloud hosting provider like Heroku or AWS.
To make things easier, there’s an example Dockerfile in this repo that shows how to run an app built with
face_recognition in a Docker container. With that, you should be able to deploy to any service that supports Docker images.
You can try the Docker image locally by running:
docker-compose up --build
There are also several prebuilt Docker images.
Linux users with a GPU (drivers >= 384.81) and Nvidia-Docker installed can run the example on the GPU: Open the docker-compose.yml file and uncomment the
dockerfile: Dockerfile.gpu and
runtime: nvidia lines.
If you run into problems, please read the Common Errors section of the wiki before filing a github issue.
Download Link: Download The Source Code
Official Website: https://github.com/ageitgey/face_recognition
#python #face-detection #data-science
Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.
#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners
Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.
Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is
Syntax: x = lambda arguments : expression
Now i will show you some python lambda function examples:
#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map
Python is awesome, it’s one of the easiest languages with simple and intuitive syntax but wait, have you ever thought that there might ways to write your python code simpler?
In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.
Swapping value in Python
Instead of creating a temporary variable to hold the value of the one while swapping, you can do this instead
>>> FirstName = "kalebu" >>> LastName = "Jordan" >>> FirstName, LastName = LastName, FirstName >>> print(FirstName, LastName) ('Jordan', 'kalebu')
#python #python-programming #python3 #python-tutorials #learn-python #python-tips #python-skills #python-development
Today you’re going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates.
In many situations you may find yourself having duplicates files on your disk and but when it comes to tracking and checking them manually it can tedious.
Heres a solution
Instead of tracking throughout your disk to see if there is a duplicate, you can automate the process using coding, by writing a program to recursively track through the disk and remove all the found duplicates and that’s what this article is about.
But How do we do it?
If we were to read the whole file and then compare it to the rest of the files recursively through the given directory it will take a very long time, then how do we do it?
The answer is hashing, with hashing can generate a given string of letters and numbers which act as the identity of a given file and if we find any other file with the same identity we gonna delete it.
There’s a variety of hashing algorithms out there such as
#python-programming #python-tutorials #learn-python #python-project #python3 #python #python-skills #python-tips
Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc…
You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like init, call, str etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).
Now there are a number of such special methods, which you might have come across too, in Python. We will just be taking an example of a few of them to understand how they work and how we can use them.
class AnyClass: def __init__(): print("Init called on its own") obj = AnyClass()
The first example is _init, _and as the name suggests, it is used for initializing objects. Init method is called on its own, ie. whenever an object is created for the class, the init method is called on its own.
The output of the above code will be given below. Note how we did not call the init method and it got invoked as we created an object for class AnyClass.
Init called on its own
Let’s move to some other example, add gives us the ability to access the built in syntax feature of the character +. Let’s see how,
class AnyClass: def __init__(self, var): self.some_var = var def __add__(self, other_obj): print("Calling the add method") return self.some_var + other_obj.some_var obj1 = AnyClass(5) obj2 = AnyClass(6) obj1 + obj2
#python3 #python #python-programming #python-web-development #python-tutorials #python-top-story #python-tips #learn-python