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

An example Analysis of facial Detection with OpenCV Haar Cascade detector

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

OpenCV Haar cascade detector for face detection example analysis, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

1. Effect picture

The effect of Haar cascade detection is as follows:

You can see that there are fixed-size faces moving in the image, and you can "train" the classifier to identify whether a given area of the image contains a human face.

The results of face, eye and mouth detection are as follows:

Sometimes there will be false positive results, as shown below:

It can be seen that the test is not the most accurate, the face is accurate, but there are several false positives in the cascade of mouth and eyes. When blinking, two situations occur: (1) the eye area is no longer detected, or (2) it is mistakenly marked as the mouth, and there are often multiple mouth detection results in many frames.

two。 Principle 2.1 what is the Haar cascade?

Haar cascade detects five features: edge feature, line feature and corner-rectangle feature. To calculate the feature, the sum of pixels under the black area needs to be subtracted from the sum of pixels under the white area. Interestingly, these features are of practical importance in face detection:

The eye area is often darker than the cheek area.

The nose area is brighter than the eye area.

Given these five rectangular regions and their corresponding sum differences, features that can classify each part of the human face can be formed.

Some of the benefits of Haar cascading is that they are very fast in calculating Haar-like features because of the use of integral images (also known as summation area tables). By using AdaBoost algorithm, they are also very effective for feature selection. Most importantly, they can detect the face in the image, regardless of the position or proportion of the face.

2.2 problems and limitations of Haar cascading

The main problems and limitations of Haar cascade detectors are as follows:

A face that needs the most effective positive image

Prone to false positives-Viola-Jones algorithm can easily report faces in images without faces

Tuning OpenCV detection parameters can be tedious. Sometimes all the faces in the image can be detected, sometimes (1) the area of the image is misclassified as the face, (2) when the face is completely left out.

2.3 Model of Haar cascade pre-training

The OpenCV library maintains a pre-trained Haar cascading library. These include:

Haarcascade_frontalface_default.xml: detecting faces

Haarcascade_eye.xml: detection of left and right eyes

Haarcascade_smile.xml: detect the presence of the mouth on the face

Haarcascade_eye_tree_eyeglasses.xml: check if you are wearing sunglasses?

Haarcascade_frontalcatface.xml: detect cat face

Haarcascade_frontalcatface_extended.xml: detect cat face extension

Haarcascade_frontalface_alt.xml: detect cat face attributes

Haarcascade_frontalface_alt_tree.xml

Haarcascade_frontalface_alt2.xml

Haarcascade_fullbody.xml: testing the whole body

Haarcascade_lefteye_2splits.xml: detect the left eye

Haarcascade_licence_plate_rus_16stages.xml: test certificate

Haarcascade_lowerbody.xml: testing the lower body

Haarcascade_profileface.xml

Haarcascade_righteye_2splits.xml: detection of the right eye

Haarcascade_russian_plate_number.xml: test Russian alphabet license plate number

Haarcascade_upperbody.xml: testing the upper body

Other pre-trained Haar cascades are also available, including one for Russian license plates and the other for cat face detection.

You can use cv2.CascadeClassifer to load a pre-trained Haar cascade detector from disk:

Detector = cv2.CascadeClassifier (path)

You can use detectMultiScale to predict it:

Results = detector.detectMultiScale (gray, scaleFactor=1.05, minNeighbors=5, minSize= (30,30), flags=cv2.CASCADE_SCALE_IMAGE) 3. Source code 3.1Image detection # USAGE# python opencv_haar_cascades_images.py-- cascades cascades-- image ml.jpg# imports necessary package import argparseimport os # different system path separator import cv2 # opencv binds import imutils# to build command line parameters and parses #-- path of cascades cascading detector ap = argparse.ArgumentParser () ap.add_argument ("- c", "- cascades", type=str, default= "cascades" Help= "path to input directory containing haar cascades") ap.add_argument ("- I", "--image", type=str, default= "ml2.jpg", help= "path to input image") args = vars (ap.parse_args ()) # initialization dictionary And save the Haar cascade detector name and file path detectorPaths = {"face": "haarcascade_frontalface_default.xml", "eyes": "haarcascade_eye.xml", "smile": "haarcascade_smile.xml",} # initialize the dictionary to save multiple Haar cascade detectors print ("[INFO] loading haar cascades...") detectors = {} # traversal detector path for (name Path) in detectorPaths.items (): # load the Haar cascade detector and save it to map path = os.path.sep.join ([args ["cascades"], path]) detectors [name] = cv2.CascadeClassifier (path) # read the image from disk Scalin And convert the grayscale image print (args ['image']) image = cv2.imread (args ["image"]) image = imutils.resize (image, width=500) gray = cv2.cvtColor (image, cv2.COLOR_BGR2GRAY) # perform facial detection faceRects = detectors ["face"]. DetectMultiScale (gray, scaleFactor=1.05, minNeighbors=5, minSize= (30,30), flags=cv2.CASCADE_SCALE_IMAGE) # traverse all detected faces for (fX, fY, fW) FH) in faceRects: # extract facial ROI faceROI = gray [fY:fY + fH, fX:fX + fW] # apply left and right eye cascade detectors eyeRects = detectors ["eyes"] .detectMultiScale (faceROI, scaleFactor=1.1, minNeighbors=10, minSize= (15,15), flags=cv2.CASCADE_SCALE_IMAGE) # detect smileRects = detectors ["smile"] .detectMultiScale (faceROI, scaleFactor=1.1, minNeighbors=10) in facial ROI MinSize= (15,15), flags=cv2.CASCADE_SCALE_IMAGE) # iterate through the eye bounding box for (eX, eY, eW, eH) in eyeRects: # draw the eye bounding box (red) ptA = (fX + eX, fY + eY) ptB = (fX + eX + eW, fY + eY + eH) cv2.rectangle (image, ptA, ptB, (0,0,255) 2) # iterate through the mouth bounding box for (sX, sY, sW, sH) in smileRects: # draw the mouth bounding box (blue) ptA = (fX + sX, fY + sY) ptB = (fX + sX + sW, fY + sY + sH) cv2.rectangle (image, ptA, ptB, (255,0,0), 2) # draw the facial bounding box (green) cv2.rectangle (image, (fX) FY), (fX + fW, fY + fH), (0,255,0), 2) # display the output frame cv2.imshow ("image") Image) cv2.waitKey (0) # cleanup work cv2.destroyAllWindows () 3.2 Real-time Video Stream Detection # USAGE# python opencv_haar_cascades.py-- cascades cascades# imports necessary packages import argparseimport os # different system path separators import time # sleep 2 seconds import cv2 # opencv binding import imutilsfrom imutils.video import VideoStream # access webcam # build command line parameters and parse #-cascades cascade detector Path ap = argparse.ArgumentParser () ap.add_argument ("- c") "--cascades", type=str, default= "cascades", help= "path to input directory containing haar cascades") args = vars (ap.parse_args ()) # initialization dictionary And save the Haar cascade detector name and file path detectorPaths = {"face": "haarcascade_frontalface_default.xml", "eyes": "haarcascade_eye.xml", "smile": "haarcascade_smile.xml",} # initialize the dictionary to save multiple Haar cascade detectors print ("[INFO] loading haar cascades...") detectors = {} # traversal detector path for (name Path) in detectorPaths.items (): # load the Haar cascade detector and save it to map path = os.path.sep.join ([args ["cascades"], path]) detectors [name] = cv2.CascadeClassifier (path) # initialize the video stream Allow the camera to preheat 2sprint ("[INFO] starting video stream...") vs = VideoStream (src=0). Start () time.sleep (2.0) # traverse every frame of the video stream while True: # get every frame of the video stream, zoom And convert the grayscale image frame = vs.read () frame = imutils.resize (frame, width=500) gray = cv2.cvtColor (frame, cv2.COLOR_BGR2GRAY) # use the appropriate Haar detector to perform facial detection faceRects = detectors ["face"]. DetectMultiScale (gray, scaleFactor=1.05, minNeighbors=5, minSize= (30, 30), flags=cv2.CASCADE_SCALE_IMAGE) # traverses all detected facial for (fX, fY, fW) FH) in faceRects: # extract facial ROI faceROI = gray [fY:fY + fH, fX:fX + fW] # apply left and right eye cascade detector eyeRects = detectors ["eyes"] .detectMultiScale (faceROI, scaleFactor=1.1, minNeighbors=10, minSize= (15,15) Flags=cv2.CASCADE_SCALE_IMAGE) # detecting smileRects = detectors ["smile"]. DetectMultiScale (faceROI, scaleFactor=1.1, minNeighbors=10, minSize= (15, 15), flags=cv2.CASCADE_SCALE_IMAGE) # traversing the eye bounding box for (eX, eY, eW) EH) in eyeRects: # draw the eye bounding box (red) ptA = (fX + eX, fY + eY) ptB = (fX + eX + eW, fY + eY + eH) cv2.rectangle (frame, ptA, ptB, (0,0,255), 2) # traverse the mouth bounding box for (sX, sY, sW) SH) in smileRects: # draw mouth bounding box (blue) ptA = (fX + sX, fY + sY) ptB = (fX + sX + sW, fY + sY + sH) cv2.rectangle (frame, ptA, ptB, (255,0,0), 2) # draw facial bounding box (green) cv2.rectangle (frame, (fX, fY), (fX + fW) FY + fH), (0,255,0), 2) # display the output frame cv2.imshow ("Frame", frame) key = cv2.waitKey (1) & 0xFF # Press the'Q' key Exit the loop if key = = ord ("Q"): break# cleanup work cv2.destroyAllWindows () vs.stop () the answer to the sample analysis question about the OpenCV Haar cascade detector for facial detection is shared here. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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