diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-03-09 13:19:22 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-03-09 13:19:22 +0000 |
commit | c21c3b0befeb46a51b6bf3758ffa30813bea0ff0 (patch) | |
tree | 9754ff1ca740f6346cf8483ec915d4054bc5da2d /ml/dlib/python_examples/face_alignment.py | |
parent | Adding upstream version 1.43.2. (diff) | |
download | netdata-upstream/1.44.3.tar.xz netdata-upstream/1.44.3.zip |
Adding upstream version 1.44.3.upstream/1.44.3
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ml/dlib/python_examples/face_alignment.py')
-rwxr-xr-x | ml/dlib/python_examples/face_alignment.py | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/ml/dlib/python_examples/face_alignment.py b/ml/dlib/python_examples/face_alignment.py new file mode 100755 index 00000000..53df7a3e --- /dev/null +++ b/ml/dlib/python_examples/face_alignment.py @@ -0,0 +1,91 @@ +#!/usr/bin/python +# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt +# +# This example shows how to use dlib's face recognition tool for image alignment. +# +# COMPILING/INSTALLING THE DLIB PYTHON INTERFACE +# You can install dlib using the command: +# pip install dlib +# +# Alternatively, if you want to compile dlib yourself then go into the dlib +# root folder and run: +# python setup.py install +# or +# python setup.py install --yes USE_AVX_INSTRUCTIONS +# if you have a CPU that supports AVX instructions, since this makes some +# things run faster. This code will also use CUDA if you have CUDA and cuDNN +# installed. +# +# Compiling dlib should work on any operating system so long as you have +# CMake installed. On Ubuntu, this can be done easily by running the +# command: +# sudo apt-get install cmake +# +# Also note that this example requires OpenCV and Numpy which can be installed +# via the command: +# pip install opencv-python numpy +# Or downloaded from http://opencv.org/releases.html + +import sys + +import dlib +import cv2 +import numpy as np + +if len(sys.argv) != 3: + print( + "Call this program like this:\n" + " ./face_alignment.py shape_predictor_5_face_landmarks.dat ../examples/faces/bald_guys.jpg\n" + "You can download a trained facial shape predictor from:\n" + " http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2\n") + exit() + +predictor_path = sys.argv[1] +face_file_path = sys.argv[2] + +# Load all the models we need: a detector to find the faces, a shape predictor +# to find face landmarks so we can precisely localize the face +detector = dlib.get_frontal_face_detector() +sp = dlib.shape_predictor(predictor_path) + +# Load the image using OpenCV +bgr_img = cv2.imread(face_file_path) +if bgr_img is None: + print("Sorry, we could not load '{}' as an image".format(face_file_path)) + exit() + +# Convert to RGB since dlib uses RGB images +img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB) + +# Ask the detector to find the bounding boxes of each face. The 1 in the +# second argument indicates that we should upsample the image 1 time. This +# will make everything bigger and allow us to detect more faces. +dets = detector(img, 1) + +num_faces = len(dets) +if num_faces == 0: + print("Sorry, there were no faces found in '{}'".format(face_file_path)) + exit() + +# Find the 5 face landmarks we need to do the alignment. +faces = dlib.full_object_detections() +for detection in dets: + faces.append(sp(img, detection)) + +# Get the aligned face images +# Optionally: +# images = dlib.get_face_chips(img, faces, size=160, padding=0.25) +images = dlib.get_face_chips(img, faces, size=320) +for image in images: + cv_bgr_img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) + cv2.imshow('image',cv_bgr_img) + cv2.waitKey(0) + +# It is also possible to get a single chip +image = dlib.get_face_chip(img, faces[0]) +cv_bgr_img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) +cv2.imshow('image',cv_bgr_img) +cv2.waitKey(0) + +cv2.destroyAllWindows() + |