Master Face Recognition on Ubuntu with Python: Step‑by‑Step Guide
This guide walks through setting up Ubuntu 17.10 with Python 2.7, installing dlib and the face_recognition library, and demonstrates five practical examples ranging from a one‑line face‑recognition command to facial feature extraction and beautification, complete with code snippets and screenshots.
Environment Requirements
Ubuntu 17.10
Python 2.7.14
Environment Setup
Install the required packages:
# Install git
sudo apt-get install -y git
# Install cmake
sudo apt-get install -y cmake
# Install python-pip
sudo apt-get install -y python-pipCompile and install dlib (required by face_recognition):
# Install boost libraries
sudo apt-get install libboost-all-dev
# Clone dlib source
git clone https://github.com/davisking/dlib.git
cd dlib
mkdir build
cd build
cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1
cmake --build .
cd ..
python setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDAInstall the face_recognition library (this will also install numpy, scipy, etc.):
# Install face_recognition
pip install face_recognitionImplement Face Recognition
Example 1 – One‑Line Command
Prepare a folder known_people with one image per person, named with the person’s name, e.g., babe.jpg, 成龙.jpg, 容祖儿.jpg:
Prepare another folder unknown_pic with the images you want to recognize (e.g., a picture containing a person not in the known set):
Run the command: face_recognition known_people/ unknown_pic/ The output lists the names of the recognized people:
Example 2 – Detect All Faces and Display
# filename: find_faces_in_picture.py
# -*- coding: utf-8 -*-
from PIL import Image
import face_recognition
image = face_recognition.load_image_file("/opt/face/unknown_pic/all_star.jpg")
face_locations = face_recognition.face_locations(image)
print("I found {} face(s) in this photograph.".format(len(face_locations)))
for face_location in face_locations:
top, right, bottom, left = face_location
print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image.show()Example 3 – Automatic Facial Feature Detection
# filename: find_facial_features_in_picture.py
# -*- coding: utf-8 -*-
from PIL import Image, ImageDraw
import face_recognition
image = face_recognition.load_image_file("biden.jpg")
face_landmarks_list = face_recognition.face_landmarks(image)
print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))
for face_landmarks in face_landmarks_list:
facial_features = ['chin','left_eyebrow','right_eyebrow','nose_bridge','nose_tip','left_eye','right_eye','top_lip','bottom_lip']
for facial_feature in facial_features:
print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)
for facial_feature in facial_features:
d.line(face_landmarks[facial_feature], width=5)
pil_image.show()Example 4 – Identify Which Person Is in the Photo
# filename: recognize_faces_in_pictures.py
# -*- coding: utf-8 -*-
import face_recognition
babe_image = face_recognition.load_image_file("/opt/face/known_people/babe.jpeg")
rong_zhu_er_image = face_recognition.load_image_file("/opt/face/known_people/Rong zhu er.jpg")
unknown_image = face_recognition.load_image_file("/opt/face/unknown_pic/babe2.jpg")
babe_face_encoding = face_recognition.face_encodings(babe_image)[0]
rong_zhu_er_face_encoding = face_recognition.face_encodings(rong_zhu_er_image)[0]
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
known_faces = [babe_face_encoding, rong_zhu_er_face_encoding]
results = face_recognition.compare_faces(known_faces, unknown_face_encoding)
print("Is the unknown face Babe? {}".format(results[0]))
print("Is the unknown face Rong Zhu Er? {}".format(results[1]))
print("Is the unknown face a new person? {}".format(not True in results))Example 5 – Facial Features Beautification
# filename: digital_makeup.py
# -*- coding: utf-8 -*-
from PIL import Image, ImageDraw
import face_recognition
image = face_recognition.load_image_file("biden.jpg")
face_landmarks_list = face_recognition.face_landmarks(image)
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image, 'RGBA')
# Example: color eyebrows, lips, and eyes
# (actual drawing code omitted for brevity)
pil_image.show()Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
