Skip to content

Instantly share code, notes, and snippets.

@brimston3
Created November 14, 2022 22:04
Show Gist options
  • Save brimston3/7329d8d72526f62d407d02883bef9635 to your computer and use it in GitHub Desktop.
Save brimston3/7329d8d72526f62d407d02883bef9635 to your computer and use it in GitHub Desktop.
face landmarks tutorial (just the code bits, not the explanations)
#!/usr/bin/env python3
# Dear future me, you probably just want these commands:
'''
source py_fl/bin/activate
python3 face_show.py --shape-predictor shape_predictor_68_face_landmarks.dat --image face.jpg
'''
# If you're reading this file, I strongly suggest going to the source:
# https://pyimagesearch.com/2017/04/03/facial-landmarks-dlib-opencv-python/
# Archive.org, in case it goes missing:
# https://web.archive.org/web/20220716064237/https://pyimagesearch.com/2017/04/03/facial-landmarks-dlib-opencv-python/
# I'm re-copying the license I found near the tutorial page:
# Copyright (c) 2020 PyImageSearch.com
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files
# (the \"Software\"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# Notwithstanding the foregoing, you may not use, copy, modify, merge,
# publish, distribute, sublicense, create a derivative work, and/or
# sell copies of the Software in any work that is designed, intended,
# or marketed for pedagogical or instructional purposes related to
# programming, coding, application development, or information
# technology. Permission for such use, copying, modification, and
# merger, publication, distribution, sub-licensing, creation of
# derivative works, or sale is expressly withheld.
# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ----
# Prep commands for Ubuntu 22.04 LTS:
'''
wget 'http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2'
bunzip2 shape_predictor_68_face_landmarks.dat.bz2
sudo apt-get install python3-opencv
python3 -m venv py_fl --system-site-packages
source py_fl/bin/activate
pip install dlib
pip install imutils
'''
# Test that our installation worked successfully by importing the packages:
'''
% python3
python> import cv2
python> import dlib
python> import imutils
'''
# Generate a test image using the webcam on my device:
'''
ffmpeg -f video4linux2 -s 640x480 -i /dev/video0 -ss 0:0:2 -frames 1 face.jpg
'''
# Run this file:
'''
python3 face_show.py --shape-predictor shape_predictor_68_face_landmarks.dat --image face.jpg
'''
# import the necessary packages
from imutils import face_utils
import numpy as np
import argparse
import imutils
import dlib
import cv2
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--shape-predictor", required=True,
help="path to facial landmark predictor")
ap.add_argument("-i", "--image", required=True,
help="path to input image")
args = vars(ap.parse_args())
# initialize dlib's face detector (HOG-based) and then create
# the facial landmark predictor
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(args["shape_predictor"])
# load the input image, resize it, and convert it to grayscale
image = cv2.imread(args["image"])
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect faces in the grayscale image
rects = detector(gray, 1)
# loop over the face detections
for (i, rect) in enumerate(rects):
# determine the facial landmarks for the face region, then
# convert the facial landmark (x, y)-coordinates to a NumPy
# array
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
# convert dlib's rectangle to a OpenCV-style bounding box
# [i.e., (x, y, w, h)], then draw the face bounding box
(x, y, w, h) = face_utils.rect_to_bb(rect)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# show the face number
cv2.putText(image, "Face #{}".format(i + 1), (x - 10, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# loop over the (x, y)-coordinates for the facial landmarks
# and draw them on the image
for (x, y) in shape:
cv2.circle(image, (x, y), 1, (0, 0, 255), -1)
# show the output image with the face detections + facial landmarks
cv2.imshow("Output", image)
cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment