Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use opencv to realize face recognition

Shulou Source: shulou.com Published: 2022-06-03 17:31:19 09月11日 Update

This article mainly explains "how to use opencv to achieve face recognition function", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use opencv to achieve face recognition.

Catalogue

I. Environment

Second, using Haar cascade for face detection

Third, Haar cascade combined with camera

Fourth, face detection using SSD

5. SSD combined with camera face detection

I. Environmental pip install opencv-python

Python3.9

Pycharm2020

People don't talk too much, just go to the code, comment it in the code, and don't talk nonsense.

Second, using Haar cascade for face detection

Test case:

Code: (remember to download the corresponding xml from the download address)

# coding=gbk "" author: Kawagawa @ time: 16:38 on 2021-9-5, https://github.com/opencv/opencv/tree/master/data/haarcascades"""import cv2# the image path to be tested imagepath= "2.jpg" image = cv2.imread (imagepath) # read the picture gray = cv2.cvtColor (image) Cv2.COLOR_BGR2GRAY) # convert the image to a grayscale image: face_cascade = cv2.CascadeClassifier (rattleshaarcascadefrontalfacefaces default. Xml') # load use the face recognizer faces = face_cascade.detectMultiScale (gray) # detect all faces in the image # draw a blue rectangle for x, y, width, height in faces: # where the color is blue, yellow and red In contrast to rgb, thickness sets the width cv2.rectangle (image, (x, y), (x + width, y + height), color= (255,0,0), thickness=2) # finally, let's save the new image cv2.imwrite ("beauty_detected.jpg", image)

Effect:

The effect can be seen that the effect is not very good.

Third, Haar cascade combined with camera

Code: (still use the front xml)

# coding=gbk "" camera face recognition author: Kawagawa @ time: 2021-9-5 17:15Haar cascade combined with camera "import cv2# creates a new cam object cap = cv2.VideoCapture (0memcv2.CAPPRODSHOW) # initialize the face recognizer (default face haar cascade) face_cascade = cv2.CascadeClassifier (rpm. Xml') while True: # read images from the camera _ Image = cap.read () # convert to grayscale image_gray = cv2.cvtColor (image, cv2.COLOR_BGR2GRAY) # detect all faces in the image faces = face_cascade.detectMultiScale (image_gray, 1.3,5) # draw a blue rectangle for x, y, width, height in faces: cv2.rectangle (image, (x, y), (x + width, y + height), color= (255,0) 0), thickness=2) cv2.imshow ("image", image) if cv2.waitKey (1) = ord ("Q"): breakcap.release () cv2.destroyAllWindows ()

Effect:

Fourth, face detection using SSD

Code:

# coding=gbk "" Picture face recognition author: Kawagawa @ time: 17:22 on 2021-9-5 "" import cv2import numpy as np# download link: https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxtprototxt_path = r ". / deploy.prototxt.txt" # download link: https://raw.githubusercontent.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20180205_fp16/res10 _ 300x300_ssd_iter_140000_fp16.caffemodelmodel_path = r ". / res10_300x300_ssd_iter_140000_fp16.caffemodel" model = cv2.dnn.readNetFromCaffe (prototxt_path Model_path) image = cv2.imread ("2.jpg") h, w = image.shape [: 2] blob = cv2.dnn.blobFromImage (image, 1.0,300,300), (104.0, 177.0, 123.0) model.setInput (blob) output = np.squeeze (model.forward ()) font_scale = 1.0for i in range (0, output.shape [0]): confidence = output [I, 2] if confidence > 0.5: box = output [I 3:7] * np.array ([w, h, w, h]) start_x, start_y, end_x, end_y = box.astype (np.int) cv2.rectangle (image, (start_x, start_y), (end_x, end_y), color= (255,0,0), thickness=2) cv2.putText (image, f "{confidence*100:.2f}%", (start_x, start_y-5) Cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255,0,0), 2) cv2.imshow ("image", image) cv2.waitKey (0) cv2.imwrite ("beauty_detected.jpg", image)

Effect:

We can see that the recognition effect is very good now.

5. SSD combined with camera face detection

Code:

# coding=gbk "" author: Kawakawa @ time: 2021-9-5 face detection by 17:26SSD combined with camera "" import cv2import numpy as npprototxt_path = "deploy.prototxt.txt" model_path = "res10_300x300_ssd_iter_140000_fp16.caffemodel" model = cv2.dnn.readNetFromCaffe (prototxt_path, model_path) cap = cv2.VideoCapture (0) while True: _, image = cap.read () h W = image.shape [: 2] blob = cv2.dnn.blobFromImage (image, 1.0,300,300), (104.0, 177.0, 123.0) model.setInput (blob) output = np.squeeze (model.forward ()) font_scale = 1.0for i in range (0, output.shape [0]): confidence = output [I, 2] if confidence > 0.5: box = output [I 3:7] * np.array ([w, h, w, h]) start_x, start_y, end_x, end_y = box.astype (np.int) cv2.rectangle (image, (start_x, start_y), (end_x, end_y), color= (255,0,0), thickness=2) cv2.putText (image, f "{confidence*100:.2f}%", (start_x) Start_y-5), cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255,0,0), 2) cv2.imshow ("image", image) if cv2.waitKey (1) = ord ("Q"): breakcv2.destroyAllWindows () cap.release ()

Effect:

It can be found that SSD works very well!

At this point, I believe you have a deeper understanding of "how to use opencv to achieve face recognition". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Face detection camera effects camera code author image time Kawagawa function picture content grayscale environment rectangle blue link recognizer learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi Apple vpn Shulou Technology