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 use Python+MediaPipe to realize the function of face detection

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use Python+MediaPipe to achieve face detection function". In daily operation, I believe many people have doubts about how to use Python+MediaPipe to achieve face detection function. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Python+MediaPipe to achieve face detection function". Next, please follow the editor to study!

Overview of MediaPipe

Google's open source MediaPipe was first launched in June 2019. Its goal is to make our life easier by providing some integrated computer vision and machine learning functions.

MediaPipe is a framework for building ML pipelines for multimodal (such as video, audio, or any time series data) and cross-platform (i.e., eAndroid, IOS, web, edge devices) applications.

Mediapipe also facilitates the deployment of machine learning technologies in demos and applications on a variety of hardware platforms.

Application

Face detection

Multi-hand tracking

Hair segmentation

Target detection and tracking

Target: 3D target detection and tracking

AutoFlip: video clipping pipe

Other

Why do you need MediaPipe

Effectively manage resources (CPU and GPU) to achieve low latency performance and handle synchronization of time series data such as audio and video frames.

MediaPipe abstracts each perception model into a module and connects them to maintenance diagrams.

In addition to the above functions, MediaPipe also supports TensorFlow and TF Lite inference engines. Any TensorFlow and TF Lite model can be used for MediaPipe. At the same time, MediaPipe also supports GPU acceleration of the device itself on mobile and embedded platforms.

Now it's time to move towards the application of MediaPipe, face detection.

Face detection

Consider a scenario where "the retailer asks you to count visitors and track the movement of visitors."

It looks hard! How can we solve this problem? Um...

Oh, yes! We will use face detection to solve this problem.

Face detection is a problem in computer vision, that is, to locate and locate one or more faces in photos.

The general statement of the problem can be defined as follows: given a still or video image, detect and locate an unknown number of faces, if any.

Use MediaPipe to perform face detection:

To perform face detection, you can use three models:

Close-up model (most suitable for faces within 2 meters of the camera)

Full-range model (dense, most suitable for faces within 5 meters of the camera)

Full range model (sparse, most suitable for faces within 5 meters of the camera)

The full-range dense model and sparse model have the same quality in F score, but they are different in basic measurement.

The recall rate of dense model is slightly higher than that of sparse model, while the accuracy of sparse model is higher than that of dense model.

Now it's time to use MediaPipe's face detection model.

Install the necessary libraries

To perform face detection, you must first install MediaPipe on the machine. If you are a windows user, you can run the following code from a command prompt on your computer.

Pip install mediapipe

For more information, you can visit the following link:

Https://google.github.io/mediapipe/getting_started/python.html

You also need to install OpenCV for webcam or image input. If you are a windows user, you can run the following code from a command prompt.

Pip install opencv-python

For more information, you can visit the following link:

Https://pypi.org/project/opencv-python/

Write code to understand the use of API:

We use Google Colab to run the code. You can choose to use it.

We need cv2, the ability to read and display images, and the MediaPipe module, which exposes the functions we need to perform face detection

Import cv2import mediapipe as mp

Then we will access the two sub-modules face_detection and drawing_utils. Face detection is used to load all functions to perform face detection, while the drawing tool is used to draw the detected face on the image.

Mp_face_detection = mp.solutions.face_detectionmp_drawing = mp.solutions.drawing_utils

It's time to delve deeper into the code. First, we take the image as input. Here we use two types of images.

(I) an image of a human face within 2 meters

(ii) contains images of faces within 5 meters.

We use the files in colab to load the image directly from the local directory. You can also use cv2.imread to load images while working on your local computer.

(a) first photo

From google.colab import files uploaded_short_range = files.upload ()

(B) second photo

From google.colab import files uploaded_full_range = files.upload ()

When working on a local PC, you can use the

Cv2.imread () # get input

Click here to learn about cv2.imread:

Now we will resize the image and display it. To display the image, we must use the cv2_imshow module of colab or cv2.

Displays cv2.imshow (frame name, iamge) when working on the local machine. We can use the following code to resize and display the image in google colab.

Code to resize and display the image:

Import cv2from google.colab.patches import cv2_imshowimport mathimport numpy as np DESIRED_HEIGHT = 480DESIRED_WIDTH = 480def resize_and_show (image): h, w = image.shape [: 2] if h < w: img = cv2.resize (image, (DESIRED_WIDTH, math.floor (h / (w/DESIRED_WIDTH) else: img = cv2.resize (image, (math.floor (w / (h/DESIRED_HEIGHT) DESIRED_HEIGHT) cv2_imshow (img) # Preview the picture. Short_range_images = {name: cv2.imread (name) for name in uploaded_short_range.keys ()} for name, image in short_range_images.items (): print (name) resize_and_show (image) full_range_images = {name: cv2.imread (name) for name in uploaded_full_range.keys ()} for name, image in full_range_images.items (): print (name) resize_and_show (image)

An example of the output of the above code

Now, we will draw key points on the face.

We can change the values of thickness and circle_radius as follows.

Drawing_spec = mp_drawing.DrawingSpec (thickness=1, circle_radius=1)

The following code learns more about mp.solutions.face_detection.

Help (mp_face_detection.FaceDetection)

After that, we will create an object of the FaceDetection class. This object will allow us to process the image and perform face key point detection. The constructor of this class supports the following parameters:

(I) Model selection: integer index 0 or 1. Use 0 to select the short-distance model that best fits the face within 2 meters of the camera, and 1 to select the full-range model that best fits the face within 5 meters of the camera. For full-range options, sparse models are used to improve reasoning speed.

(ii) minimum detection confidence: the minimum confidence value in the face detection model ([0.010 1.0]), the detection is successful. The default value is 0.5.

With mp_face_detection.FaceDetection (min_detection_confidence=0.5, model_selection=0) as face_detection:

The code model_selection=0 above means that we choose a short-distance model for face detection. Using the following code, we use a short image model to perform the final face detection and draw the key points.

# run MediaPipe face detection and close model with mp_face_detection.FaceDetection (min_detection_confidence=0.5, model_selection=0) as face_detection: for name, image in short_range_images.items (): # convert BGR images to RGB, and use MediaPipe face detection for processing. Results = face_detection.process (cv2.cvtColor (image, cv2.COLOR_BGR2RGB)) # draws the detection of each human face. Print (f'Face detections of {name}:') if not results.detections: continue annotated_image = image.copy () for detection in results.detections: mp_drawing.draw_detection (annotated_image, detection) resize_and_show (annotated_image)

Face Detection Model for short length (less than 2 meters) Images

Now for model_selection=1, this means that we choose the full range model of face detection. Using the following code, we use the complete image model to perform the final face detection and draw the key points.

With mp_face_detection.FaceDetection (min_detection_confidence=0.5, model_selection=1) as face_detection: for name, image in full_range_images.items (): # converts the BGR image to RGB, and uses MediaPipe face detection for processing. Results = face_detection.process (cv2.cvtColor (image, cv2.COLOR_BGR2RGB)) # draws the detection of each human face. Print (f'Face detections of {name}:') if not results.detections: continue annotated_image = image.copy () for detection in results.detections: mp_drawing.draw_detection (annotated_image, detection) resize_and_show (annotated_image)

Face Detection Model for full range (less than 5 meters) Images

We can also use the code of the full-range face detection model to perform this process on group photos.

At this point, the study on "how to use Python+MediaPipe to achieve face detection function" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report