Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to detect facial features in Python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to detect facial features in Python, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Installation requirements

As usual, this article will demonstrate the example in code, and will guide you step by step to achieve a complete facial feature recognition example. But before you start, you need to start a new Python project and install three different libraries:

Opencv python

Dlib

If you use pipenv as I did, you can install all of these files using the following command:

Pipenv install opencv-python, dlib

If you are using Mac and some versions of Linux, you may encounter some problems when installing dlib. If you encounter compilation errors during installation, be sure to check the version of the CMake library you are using. In Mac, make sure you have a CMake available and that you can run it with the correct version:

Brew install cmake

For other operating systems, check online for specific support.

Step 1: load and display the picture

We'll start small and code-based until we have an example that works.

Usually, I like to use drawings to render images, but since we have prepared something cool in a later article, we will do something different and create a window to show the results of our work.

Let's look at the code together.

Import cv2# read the imageimg = cv2.imread ("face.jpg") # show the imagecv2.imshow (winname= "Face" mat=img) # Wait for a key press to exitcv2.waitKey (delay=0) # Close all windowscv2.destroyAllWindows ()

It's simple, isn't it? We just load the image with imread and then tell OpenCV to display the image in winname, which opens the window and gives it a title.

After that, we need to pause execution because the window is destroyed when the script stops, so we use cv2.waitKey to hold the window until a key is pressed, then destroy the window and exit the script.

If you use the code and add an image called face.jpg to the code directory, you should get the following:

Original image:

Step 2: face recognition

So far, we haven't done any processing on the image, just presenting it in a window, which is very boring, but now we're going to start coding the content, and we'll start by identifying where there is a face in the image.

To do this, we will use a Dlib function called get_frontial_face_detector (), which is very straightforward. But there is a warning that this function only applies to grayscale images, so we must first use OpenCV.

Get_frontial_face_detector () returns a detector, which is a function that we can use to retrieve face information. Each face is an object that contains points where the image can be found.

But we'd better look at the code:

Import cv2import dlib# Load the detectordetector = dlib.get_frontal_face_detector () # read the imageimg = cv2.imread ("face.jpg") # Convert image into grayscalegray = cv2.cvtColor (src=img, code=cv2.COLOR_BGR2GRAY) # Use detector to find landmarksfaces = detector (gray) for face in faces: X1 = face.left () # left point y1 = face.top () # top point x2 = face.right () # right point y2 = face.bottom () # bottom point # Draw a rectangle cv2.rectangle (img=img Pt1= (x1, y1), pt2= (x2, y2), color= (0,255,0), thickness=4) # show the imagecv2.imshow (winname= "Face", mat=img) # Wait for a key press to exitcv2.waitKey (delay=0) # Close all windowscv2.destroyAllWindows ()

The above code retrieves all faces from the image and renders a rectangle on each face, resulting in the following image:

So far, we have done a good job in finding faces, but we still need some work to extract all the features (landmarks). Let's get started.

Step 3: recognize other people's facial features

Do you like magic? So far, the way DLib works is amazing, and we can achieve a lot in just a few lines of code, but now that we have a whole new question, will it continue to be that simple?

The answer is yes! It turns out that DLib provides a function called shape_predictor (), which will provide us with all the magic, but requires a pre-trained model to work.

There are several models that can work with shape_predictor, and the model I'm using can be downloaded here, or you can try other models.

Let's see what the new code looks like now.

Import cv2import dlib# Load the detectordetector = dlib.get_frontal_face_detector () # Load the predictorpredictor = dlib.shape_predictor ("shape_predictor_68_face_landmarks.dat") # read the imageimg = cv2.imread ("face.jpg") # Convert image into grayscalegray = cv2.cvtColor (src=img Code=cv2.COLOR_BGR2GRAY) # Use detector to find landmarksfaces = detector (gray) for face in faces: X1 = face.left () # left point y1 = face.top () # top point x2 = face.right () # right point y2 = face.bottom () # bottom point # Look for the landmarks landmarks = predictor (image=gray, box=face) x = landmarks.part (27). X y = landmarks.part (27). Y # Draw a circle cv2.circle (img=img, center= (x, y), radius=5 Color= (0,255,0), thickness=-1) # show the imagecv2.imshow (winname= "Face", mat=img) # Wait for a key press to exitcv2.waitKey (delay=0) # Close all windowscv2.destroyAllWindows ()

As before, we always build code on the same code, and now we use our prediction function to find landmarks for each face. Now I am still doing some strange things, such as what was I doing there on the 27th?

Landmarks = predictor (image=gray, box=face) x = landmarks.part (27). Xy = landmarks.part (27). Y

Our prediction function will return an object containing all 68 points. According to the picture we saw earlier, if you notice that point 27 is right between the eyes, so if all the calculations are correct, you should see a green dot between the eyes, as shown in the following figure:

We are very close, now let's render all the points, not just one:

Import cv2import numpy as npimport dlib# Load the detectordetector = dlib.get_frontal_face_detector () # Load the predictorpredictor = dlib.shape_predictor ("shape_predictor_68_face_landmarks.dat") # read the imageimg = cv2.imread ("face.jpg") # Convert image into grayscalegray = cv2.cvtColor (src=img Code=cv2.COLOR_BGR2GRAY) # Use detector to find landmarksfaces = detector (gray) for face in faces: X1 = face.left () # left point y1 = face.top () # top point x2 = face.right () # right point y2 = face.bottom () # bottom point # Create landmark object landmarks = predictor (image=gray, box=face) # Loop through all the points for n in range (0 68): X = landmarks.part (n). X y = landmarks.part (n). Y # Draw a circle cv2.circle (img=img, center= (x, y), radius=3, color= (0,255,0), thickness=-1) # show the imagecv2.imshow (winname= "Face", mat=img) # Delay between every framcv2.waitKey (delay=0) # Close all windowscv2.destroyAllWindows ()

But what if you're not interested in all the points? In fact, you can adjust your range interval to get any of the features specified in the glossary above, as I did here:

Great, but can we do something cooler?

Step 4: real-time detection

Yes, you read it right! This may be the effect you want! The next step is to connect to our webcam and identify landmarks in real time from your video stream.

You can do real-time facial landmark detection on your face by using a camera to traverse video frames or using video files.

If you want to use your own camera, refer to the following code, and if you are using a video file, be sure to change the number 0 to the video path.

If you want to end the window, press the ESC key on the keyboard:

Import cv2import dlib# Load the detectordetector = dlib.get_frontal_face_detector () # Load the predictorpredictor = dlib.shape_predictor ("shape_predictor_68_face_landmarks.dat") # read the imagecap = cv2.VideoCapture (0) while True: _, frame = cap.read () # Convert image into grayscale gray = cv2.cvtColor (src=frame Code=cv2.COLOR_BGR2GRAY) # Use detector to find landmarks faces = detector (gray) for face in faces: X1 = face.left () # left point y1 = face.top () # top point x2 = face.right () # right point y2 = face.bottom () # bottom point # Create landmark object landmarks = predictor (image=gray, box=face) # Loop through all the points for n in range (0 68): X = landmarks.part (n). X y = landmarks.part (n). Y # Draw a circle cv2.circle (img=frame, center= (x, y), radius=3, color= (0,255,0), thickness=-1) # show the image cv2.imshow (winname= "Face", mat=frame) # Exit when escape is pressed if cv2.waitKey (delay=1) = 27: break# When everything done Release the video capture and video write objectscap.release () # Close all windowscv2.destroyAllWindows () finished reading the above content Have you mastered the method of how to detect facial features in Python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report