A Python notebook exploring two computer vision tasks with OpenCV and dlib-based face embeddings: identifying known faces from a small custom database, and detecting people in group photos using a classical HOG + SVM detector.
- Encodes each known face into a 128-dimension vector using the
face_recognitionlibrary (built ondlib's ResNet model). - Stores the encodings + labels in a small pandas "database" and saves it to disk as
face_db.pkl, so it can be reloaded without re-encoding. - A
predict(img_name)function loads a new photo, encodes it, and compares it against every entry in the database (tolerance=0.5). If a match is found it returns that person's name; otherwise it returns'unknown'.
Database (known faces): Messi (2 reference photos), Ronaldo (1 reference photo)
Test results:
| Input image | Predicted | Correct? |
|---|---|---|
messi3.jpg |
messi |
Yes |
ronaldo2.jpg |
ronaldo |
Yes |
bale.jpg (not in database) |
unknown |
Yes, correctly rejected |
- Uses OpenCV's built-in HOG descriptor with the default pre-trained pedestrian SVM (
cv2.HOGDescriptor_getDefaultPeopleDetector()). - Runs
detectMultiScaleon a group photo (people2.jpg) to find people, then draws bounding boxes around detections with a confidence score above0.4.
Result on people2.jpg: 4 people detected, with confidence scores of 1.01, 0.98, 0.77, and 0.49 — all above the threshold, so all four got bounding boxes drawn.
- Python
face_recognition(dlib-based face embeddings)- OpenCV (
cv2) — HOG people detector - pandas — lightweight in-memory "face database"
- matplotlib — visualizing results
| File | Purpose |
|---|---|
face_recognition_ai.ipynb |
Main notebook — both face recognition and people detection |
messi.jpg, messi2.jpeg, ronaldo.jpg |
Reference images used to build the face database |
messi3.jpg, ronaldo2.jpg, bale.jpg |
Test images used to check prediction accuracy |
people1.jpg, people2.jpg |
Group photos used for the people-detection part |
pip install face_recognition dlib opencv-python pandas matplotlibNote:
dlibrequires CMake and a C++ compiler to build from source. If installation fails locally, running this notebook in Google Colab (via the badge above) sidesteps that entirely, since Colab has it preinstalled.
import face_recognition as fr
# Build the database once
img_enc = fr.face_encodings(fr.load_image_file('messi.jpg'))[0]
# Later, identify a new photo
predict('some_new_photo.jpg') # returns a name or 'unknown'- The face database only recognizes the specific people it was built with (Messi, Ronaldo) - anyone else returns
unknown. - Face matching accuracy depends on the
toleranceparameter (set to0.5here); lowering it makes matches stricter, raising it makes them looser. - The HOG people detector is a classical (non-deep-learning) method - accurate for clearly visible, upright people but less reliable with occlusion, unusual poses, or crowded scenes.
