Face recognition example in Python

WBOY
Release: 2023-06-11 08:57:46
Original
1140 people have browsed it

With the continuous development of computer technology, artificial intelligence technology has received more and more attention and application, among which face recognition technology is the most popular direction. As one of the most popular programming languages at present, Python is increasingly used in face recognition. This article will introduce face recognition examples in Python.

1. OpenCV

OpenCV is an open source computer vision library that provides a variety of algorithm-based image processing and computer vision methods. In Python, we can use OpenCV to implement face recognition.

First you need to import the OpenCV module:

import cv2
Copy after login

Then, use theCascadeClassifierfunction provided by OpenCV for face recognition:

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
Copy after login

Among them,haarcascade_frontalface_default.xmlis a pre-trained model provided in OpenCV for detecting faces.

Next, we need to read the image and process it:

img = cv2.imread('test.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Copy after login

Among them,test.jpgis the image to be processed, and thecvtColorfunction will The image is converted to grayscale.

Finally, face recognition is performed on the processed image:

faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
Copy after login

Among them, thedetectMultiScalefunction is used to detect the face in the image and returns the face The coordinates and size of the box. Finally, we use therectanglefunction to draw the face frame on the original image.

2. face_recognition

face_recognition is a face recognition library based on dlib and Python. It uses deep learning methods for face recognition and has high accuracy and robustness.

You need to install the face_recognition library before use:

pip install face_recognition
Copy after login

Then, we need to read the image and process it:

import face_recognition import matplotlib.pyplot as plt image = face_recognition.load_image_file("test.jpg") face_locations = face_recognition.face_locations(image) plt.imshow(image)
Copy after login

Among them,face_recognition.load_image_fileThe function is used to read pictures, and theface_recognition.face_locationsfunction is used to detect face locations in pictures.

Finally, we can mark the position of the face in the image:

import numpy as np import cv2 for face_location in face_locations: top, right, bottom, left = face_location cv2.rectangle(image, (left, top), (right, bottom), (0, 0, 255), 2) plt.imshow(image)
Copy after login

Among them, thecv2.rectanglefunction is used to mark the rectangular frame on the original image, indicating The position of the human face.

Conclusion

The application range of face recognition technology is becoming more and more extensive. Python, as one of the most popular programming languages at present, also has excellent performance in the field of face recognition. The two examples introduced above, through the application of OpenCV and face_recognition library, allow us to realize the face recognition function more conveniently and quickly.

The above is the detailed content of Face recognition example in Python. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!