In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "python implementation of face detection example analysis," Xiaobian through the actual case to show you the operation process, the operation method is simple and fast, practical, I hope this "python implementation of face detection example analysis" article can help you solve the problem.
OpenCV
OpenCV is the most popular open source library in computer vision, originally written in C/C++ and now available in Python.
OpenCV can use machine learning algorithms to search for faces in images. Because faces are complex, there is no simple test that tells us whether it has detected a face. However, the algorithm can match thousands of very small patterns and features. The algorithm breaks down the task of recognizing faces into thousands of very, very small tasks, which are relatively easy to solve. Such small tasks are called classifiers.
Code Results:
The method is as follows:
Looking for imported xml files
import cv2 print(cv2.__ file__)
Find the path where cv2 is installed and find the/data file under that path.
Some xml files are downloaded by default, not all xml files. If the required xml file is not in it, you need to download it from the Internet and put it in the directory for invocation. e.g. self-installation ('haarcascade_mcs_nose.xml','haarcascade_mcs_mouth.xml')
Import package:
import cv2
Import xml file, you can choose the xml file to import according to the task requirements.
#Face detector face_cascade = cv2.Cascade Classifier (cv2.data.haarcascades + 'haarcascade_frontface_default.xml')#eye detectoreye_cascade = cv2.CascadeClassifier (cv2.data.haarcascades + 'haarcascade_eye.xml')#mouth detector mouth_cascade = cv2.CascadeClassifier (cv2.data.haarcascades + 'haarcascade_mcs_mouth.xml')#nose detector nose_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_mcs_nose.xml')
Settings window:
cv2.namedWindow('mytest', 0);cv2.resizeWindow('mytest', 1500, 1000)
Turn on the camera, Face Recognition:
#Get camera cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)#Open camera cap.open(0)while cap.isOpened(): #Get the picture flag, frame = cap.read() #Face detection faces = face_cascade.detectMultiScale(frame, 1.3, 2) img = frame for (x, y, w, h) in faces: #Draw a rectangle based on face coordinates and length, width img = cv2.rectangle(img, (x, y), (x+w, y+h),(255, 0 ,0), 2) #Determine the face range and search for other features on the face face_area = img[y:y+h, x:x+w] #Eye detection eyes = eye_cascade.detectMultiScale(face_area, 1.3, 2) for (ex, ey, ew, eh) in eyes: cv2.rectangle(face_area, (ex, ey), (ex + ew, ey + eh), (255, 0 ,0), 1) #Mouth detection mouth = mouth_cascade.detectMultiScale(face_area, 1.5, 2) for (mx, my, mw, mh) in mouth: cv2.rectangle(face_area, (mx, my), (mx + mw, my + mh), (0, 0, 255), 2) #nose detection nose = nose_cascade.detectMultiScale(face_area, 1.2, 5) for (nx, ny, nw, nh) in nose: cv2.rectangle(face_area, (nx, ny), (nx + nw, ny + nh), (255, 0, 255), 2) #Picture Display cv2.imshow('mytest', img) #Set exit button key_pressed = cv2.waitKey(100) print ('stand-alone window, input keys, computer keys are ',key_pressed,' press esc key to end') if key_pressed == 27: break #Close camera cap.release()#Close image window cv2.destroyAllWindows() Full code: import cv2#face detector face_cascade = cv2.Cascade Classifier (cv2.data.haarcascades + 'haarcascade_frontface_default.xml')#eye detectoreye_cascade = cv2.CascadeClassifier (cv2.data.haarcascades + 'haarcascade_eye.xml')#mouth detector mouth_cascade = cv2.CascadeClassifier (cv2.data.haarcascades + 'haarcascade_mcs_mouth.xml')#nose detector nose_cascade = cv2.CascadeClassifier (cv2.data.haarcascades + 'haarcascade_mcs_nose.xml')#Get Camera Cap = cv2.VideoCapture (0,cv2.CAP_DSHOW)#Open camera cap.open(0)cv2.namedWindow ('mytest ', 0);cv2.resizeWindow ('mytest', 1500, 1000)while cap.isOpened(): #Get the picture flag, frame = cap.read() #Face detection faces = face_cascade.detectMultiScale(frame, 1.3, 2) img = frame for (x, y, w, h) in faces: #Draw a rectangle based on face coordinates and length, width img = cv2.rectangle(img, (x, y), (x+w, y+h),(255, 0 ,0), 2) #Determine the face range and search for other features on the face face_area = img[y:y+h, x:x+w] #Eye detection eyes = eye_cascade.detectMultiScale(face_area, 1.3, 2) for (ex, ey, ew, eh) in eyes: cv2.rectangle(face_area, (ex, ey), (ex + ew, ey + eh), (255, 0 ,0), 1) #Mouth detection mouth = mouth_cascade.detectMultiScale(face_area, 1.5, 2) for (mx, my, mw, mh) in mouth: cv2.rectangle(face_area, (mx, my), (mx + mw, my + mh), (0, 0, 255), 2) #nose detection nose = nose_cascade.detectMultiScale(face_area, 1.2, 5) for (nx, ny, nw, nh) in nose: cv2.rectangle(face_area, (nx, ny), (nx + nw, ny + nh), (255, 0, 255), 2) #Picture Display cv2.imshow('mytest', img) #Set exit button key_pressed = cv2.waitKey(100) print ('stand-alone window, input keys, computer keys are ',key_pressed,' press esc key to end') if key_pressed == 27: break #Close camera cap.release()#Close image window cv2.destrucyAllWindows () The content of "Python implementation of face detection instance analysis" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.