Do you want to use a powerful Face Detector Library and build a Windows App using Python Build Tools? Try Face Recognition library for Python. This post will guide you on how to recognize all faces in any given image using the Face Recognition library and use Python for Delphi to display it in the Delphi Windows GUI app.
To create the GUI you can use the excellent Python4Delphi (P4D). P4D is a free and simple tool that allows you to run Python scripts as well as create new Python modules and types in Delphi.
Face Recognition library-known as the world’s simplest face recognition library, has the capabilities to recognize and manipulate faces using Python or the command line.
Face Recognition is built using dlib’s state-of-the-art face recognition built with deep learning. The model has an accuracy of 99.38% on the Labeled Faces in the Wild benchmark.
First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.
Another requirement is, you need to install CMake and dlibs on your computer.
Table of Contents
Demo
We will test the Face Recognition library to detect faces in this image:
Use the following code to recognize faces from any image, using Histogram of Oriented Gradients (HOG) based model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
from PIL import Image import face_recognition # Load the jpg file into a numpy array image = face_recognition.load_image_file("C:/Users/ASUS/people.jpg") # Find all the faces in the image using the default HOG-based model. # This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated. # See also: find_faces_in_picture_cnn.py 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: # Print the location of each face in this image top, right, bottom, left = face_location print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right)) # You can access the actual face itself like this: face_image = image[top:bottom, left:right] pil_image = Image.fromarray(face_image) pil_image.show() |
The histogram of oriented gradients (HOG) is a feature descriptor used in computer vision and image processing for object detection. The technique counts occurrences of gradient orientation in localized portions of an image.
The usage of the HOG-based model became widespread in 2005 when Navneet Dalal and Bill Triggs, researchers for the French National Institute for Research in Computer Science and Automation (INRIA), presented their supplementary work on HOG descriptors at the Conference on Computer Vision and Pattern Recognition (CVPR). In this work, they focused on pedestrian detection in static images, although since then they expanded their tests to include human detection in videos, as well as to a variety of common animals and vehicles in static imagery.
Here are the outputs of recognized faces by Face Recognition + Python4Delphi:
Congratulations, now you have learned how to recognize all faces in any given image using the Face Recognition library and use Python for Delphi to display it in the Delphi Windows GUI app!
Check out the Face Recognition library for Python and use it in your projects: https://pypi.org/project/face-recognition/ and
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi