Step-by-Step Guide to Building a Face Recognition System on Ubuntu with Python
This tutorial walks through setting up Ubuntu 17.10 with Python 2.7, installing required packages, compiling dlib, and using the face_recognition library to detect, identify, and beautify faces through multiple code examples.
Environment Requirements
Ubuntu 17.10
Python 2.7.14
Environment Setup
Install Ubuntu 17.10 (installation steps linked in the original article).
Python 2.7.14 is the default version on Ubuntu 17.10.
Install git, cmake, and python‑pip.
# 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 (requires Boost).
# Install Boost
$ 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 . # note the space
$ cd ..
$ python setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDAInstall the face_recognition package.
# Install face_recognition
$ pip install face_recognition
# Dependencies such as numpy and scipy are installed automaticallyFace Recognition Examples
Example 1: One‑line face recognition
Prepare a folder of known people (one image per person, filename is the name) and another folder of images to identify, then run the face_recognition command with both folders as arguments.
Example 2: Detect all faces in a picture
# 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 a specific person
# 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_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_face_encoding = face_recognition.face_encodings(rong_image)[0]
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
known_faces = [babe_face_encoding, rong_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 any(results)))Example 5: Facial feature 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)
for face_landmarks in face_landmarks_list:
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image, 'RGBA')
d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128))
d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128))
d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5)
d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5)
d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128))
d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128))
d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8)
d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8)
d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))
d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30))
d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6)
d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6)
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.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
