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
Then, use the CascadeClassifier
function provided by OpenCV for face recognition:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
Among them, haarcascade_frontalface_default.xml
is 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)
Among them, test.jpg
is the image to be processed, and the cvtColor
function 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)
Among them, the detectMultiScale
function is used to detect the face in the image and returns the face The coordinates and size of the box. Finally, we use the rectangle
function 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
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)
Among them, face_recognition.load_image_file
The function is used to read pictures, and the face_recognition.face_locations
function 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)
Among them, the cv2.rectangle
function 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!