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 realize face and eye recognition based on python3+OpenCV

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

Share

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

This article mainly introduces how to achieve face and eye recognition based on python3+OpenCV, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

1. Download xml files for OpenCV face detection

Haarcascade_eye.xml and haarcascade_frontalface_default.xml are used for face detection and eye detection. These two files can be downloaded from OpenCV's official website as follows:

1. Open the xml file to be downloaded, as shown below:

2. Click Raw:

3. Right-click in the newly opened web page, select Save as, and finally save it.

Second, the import of face detection files and the processing of pictures

Then you can load the two xml files you just downloaded into the code, and then perform grayscale processing on the images you want to recognize. The code is as follows:

Import cv2# loads two xml files for face recognition and eye recognition: face_xml = cv2.CascadeClassifier ('haarcascade_frontalface_default.xml') eye_xml = cv2.CascadeClassifier (' haarcascade_eye.xml') # load image img = cv2.imread ('face.jpg') cv2.imshow (' src', img) # grayscale processing gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY)

The original picture we used is as follows:

Third, face recognition and use a box to display the position of the face

Using the face_xml.detectMultiScale () method to identify the location of the face, and using cv2.rectangle () to draw the box, the specific code is as follows:

# face recognition face = face_xml.detectMultiScale (gray, 1.3,2) # parameters: 1, grayscale image, 2, zoom ratio 3. Threshold print ("% d faces in this picture"% len (face)) # draw the recognized face for (x, y, w, h) in face: cv2.rectangle (img, (x, y), (x, y), (0,255,0), 2) # draw the face box cv2.imshow ('dst', img) cv2.waitkey (0)

The effect of recognition is as follows:

4. Eye recognition and display of eye position in a box

Similar to face recognition, but eye recognition is based on face recognition, so the face image is grayed out, and then recognized, the code is as follows:

# Eye recognition on the basis of human face face_gray = gray [y:y+h, x:x+w] face_color = img [y:y+h, x:x+w] # Eye recognition eyes = eye_xml.detectMultiScale (face_gray) print ("% d eyes on this face"% len (eyes)) # draw the recognized eyes for (etherex, eBay, eBay) In eyes: cv2.rectangle (face_color, (eBay, eBay), (e_x+e_w, e_y+e_h), (0,255,0), 2) # draw eye box 5, source code and effect

The source code is as follows

#-*-coding: utf-8-*-""-- File Name: 03-face.py Description: face and eye recognition based on OpenCV Author: love Mo Xiaowai Email: xiaowen0392@qq.com date: 2019Accord 2Meltel-Change Activity: 2019Universe 6gambit 2Rose- -"" _ _ author__ = 'WWQ'import cv2# two xml files loaded with face recognition and eye recognition face_xml = cv2.CascadeClassifier (' haarcascade_frontalface_default.xml') eye_xml = cv2.CascadeClassifier ('haarcascade_eye.xml') # load picture img = cv2.imread (' face.jpg') cv2.imshow ('src') Img) # Grayscale processing gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY) # face recognition face = face_xml.detectMultiScale (gray, 1.3,2) # parameters: 1, grayscale images 2. Zoom ratio 3. Threshold print ("% d faces in this picture"% len (face)) # draw the recognized face for (x, y, w, h) in face: cv2.rectangle (img, (x, y), (x, y), (0,255,0), 2) # draw the face box # cv2.imshow ('dst', img) # recognize the eye face_gray = gray [y:y+h] on the basis of the face X:x+w] face_color = img [y:y+h, x:x+w] # Eye recognition eyes = eye_xml.detectMultiScale (face_gray) print ("% d eyes on this face"% len (eyes)) # draws the recognized eye for (etherex, efoly, eBay, eBay) in eyes: cv2.rectangle (face_color, (eBay, eBay) (e_x+e_w, e_y+e_h), (0,255,0), 2) # draw the eye box cv2.imshow ('dst', img) cv2.waitKey (0)

The effect is as follows:

Thank you for reading this article carefully. I hope the article "how to realize face and eye recognition based on python3+OpenCV" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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