π― Face Recognition with Python: Using the face_recognition
Library by ageitgey
Security systems are getting smarter every day β and face recognition is one of the leading technologies behind this evolution. Thanks to the amazing work of Adam Geitgey, we have a powerful yet simple-to-use Python library called face_recognition
, which allows you to build facial recognition systems with just a few lines of code.
In this blog, weβll walk through how to use the face_recognition
library to:
- Load images of known people.
- Identify faces in new images or webcam feeds.
- Match unknown faces against a stored database.
π¦ What Is face_recognition
?
face_recognition
is a Python library that uses deep learning models (via dlib) under the hood to identify and compare human faces. It is:
- Open-source
- Accurate
- Beginner-friendly
- Cross-platform (Windows, Linux, macOS, Raspberry Pi)
π§ Installation
Before you begin, you need Python 3 and pip. Then, simply run:pip install face_recognition pip install opencv-python
Note: You may need
dlib
dependencies. On Raspberry Pi or Linux, install via system packages first (e.g.,libdlib-dev
).
π§ How Face Recognition Works
The library uses a pre-trained deep learning model to extract 128-dimensional face encodings from images. These encodings represent unique features of a personβs face.
Matching is done by comparing face encodings using Euclidean distance β if it’s small enough, the faces are considered a match.
π Project Structure
face_recognition_project/ βββ known/ β βββ obama.jpg β βββ biden.jpg βββ unknown/ β βββ group_photo.jpg βββ recognize_faces.py
π§ͺ Python Code Example
import face_recognition import cv2 import os # Load known face encodings known_face_encodings = [] known_face_names = [] for file in os.listdir("known"): image = face_recognition.load_image_file(f"known/{file}") encoding = face_recognition.face_encodings(image)[0] known_face_encodings.append(encoding) known_face_names.append(file.split('.')[0]) # Load an unknown image unknown_image = face_recognition.load_image_file("unknown/group_photo.jpg") face_locations = face_recognition.face_locations(unknown_image) face_encodings = face_recognition.face_encodings(unknown_image, face_locations) # Draw results for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): matches = face_recognition.compare_faces(known_face_encodings, face_encoding) name = "Unknown" if True in matches: first_match_index = matches.index(True) name = known_face_names[first_match_index] # Draw a rectangle and label cv2.rectangle(unknown_image, (left, top), (right, bottom), (0, 255, 0), 2) cv2.putText(unknown_image, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2) # Convert BGR to RGB and show cv2.imshow("Face Recognition", cv2.cvtColor(unknown_image, cv2.COLOR_RGB2BGR)) cv2.waitKey(0) cv2.destroyAllWindows()
πΌοΈ Example Image (Generated)
Let’s visualize what the system detects when scanning a group photo with known faces.
Here’s a simulated result:

Note: Green boxes show recognized faces, with labels identifying them.
π» Real-World Applications
- Home Security Systems β Raspberry Pi can trigger events when an unknown face is detected.
- Office Entry Systems β Automatically unlock doors for authorized personnel.
- School/College Attendance β Face-based roll calls.
- Retail Surveillance β Track VIP or blacklisted customers.
β Proof of Use
The library is actively used in Raspberry Pi-based facial recognition projects and is maintained on GitHub:
π https://github.com/ageitgey/face_recognition
π Final Thoughts
Whether youβre a hobbyist working on a Raspberry Pi project or an engineer building intelligent surveillance systems, face_recognition
gives you the power of deep learning with minimal code.
No Responses