OpenCV using python--to adjust object recognition parameters for AdaBoost Cascade classifiers based on Haar features

Source: Internet
Author: User

Adjust the object recognition parameters of the AdaBoost Cascade classifier based on the Haar feature 1. Object recognition problem of AdaBoost Cascade classifier based on Haar feature

Paul A. Viola and Michael J. Jones published in 2001 the article "Fast object detection using simple features to improve cascade detectors." At the same time, many bloggers in csdn from 07 to 13 have made a lot of work on the principle of this method, library function content, the training of XML file and the realization of OPENCV. At the same time, it is convenient to call the library function of OpenCV, which can realize the detection of human face with very short code. However, in practice, it is found that many well-trained XML files are not good to use.

2. Adjust the meaning of the parameters

Since the well-trained XML file doesn't work, does it mean retraining the classifier? If the object to be detected is in OpenCV, try to use the classifier that comes with the OPENCV. Because the self-contained classifier contains a lot of staff's efforts, it will not be so easy to use. Therefore, according to the actual situation to find the problem. The main problems include: image resolution, parameter setting and posture of object placement .

(1) Image resolution

If the image resolution is too low to cause the object in the image is not clear, and even the human eye can not see clearly, of course, the detection of objects; If the image resolution is too high, the size of the object beyond the size of the classifier, that is, the classifier can detect only a part of the object, but not the whole, so it will not detect

(2) Parameter setting

The contents of the parameter settings are described in detail in the steps to adjust the parameters.

(3) Position and posture of object placement

An extreme example: the classifier training sample is the front of the object, and the actual detection of the opposite side of the object, the front and opposite sides of the object is very different, so it is possible to detect the object. To put it simply, the samples of the training and the samples tested were the same type of object, but the differences in the images were too large. There are two solutions: the database is incomplete and the database sample is re-trained;

3. Code implementation someone claims to use less than 25 lines of code to achieve face detection, the author for the purpose of explaining the problem, temporarily do not consider the length of the code. The following is the application of the OpenCV library in the detection of human face, left eye, right eye, nose, mouth and other XML file implementation code.
Import Cv2import NumPy as Npfrom matplotlib import Pyplot as plt####################################################### ######################## #print ' Load Object Cascade Classifier ' Facecascade = cv2. Cascadeclassifier (' D:/opencv 2.4.9/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml ') Lefteyecascade = Cv2. Cascadeclassifier (' D:/opencv 2.4.9/opencv/sources/data/haarcascades/haarcascade_mcs_lefteye.xml ') Righteyecascade = Cv2. Cascadeclassifier (' D:/opencv 2.4.9/opencv/sources/data/haarcascades/haarcascade_mcs_righteye.xml ') NoseCascade = Cv2. Cascadeclassifier (' D:/opencv 2.4.9/opencv/sources/data/haarcascades/haarcascade_mcs_nose.xml ') MouthCascade = Cv2. Cascadeclassifier (' D:/opencv 2.4.9/opencv/sources/data/haarcascades/haarcascade_mcs_mouth.xml ') ################ ############################################################### #print ' Load Image ' imgfile = ' images/face.jpg ' # load An original imageimg = Cv2.imread (imgfile) # Convert color space from BGR to grayimg = Cv2.cvtcolor (IMG, Cv2. COLOR_BGR2RGB) Imggray = Cv2.cvtcolor (img, Cv2. Color_rgb2gray) faces = Facecascade.detectmultiscale (Imggray, scalefactor = 1.3, minneighbors = 4, minSize = (60,60), maxSi Ze = (300,300), flags = Cv2.cv.CV_HAAR_SCALE_IMAGE) for (x,y,w,h) in Faces:cv2.rectangle (IMG, (x, y), (x+w,y+h), (25 5,0,0), 2) Facegray = Imggray[y:y+h, x:x+w] Facecolor = img[y:y+h, X:x+w] "' # for small Alice lef Teye = Lefteyecascade.detectmultiscale (Facegray, scalefactor = 1.3, Minneighbors = 5, minSize = (20,20), maxSize = (80,80) , flags = Cv2.cv.CV_HAAR_SCALE_IMAGE) Righteye = Righteyecascade.detectmultiscale (Facegray, scalefactor = 1.3, Minneigh    Bors = 5, minSize = (20,20), maxSize = (80,80), flags = Cv2.cv.CV_HAAR_SCALE_IMAGE) # cannot search nose successfully! Nose = Nosecascade.detectmultiscale (Facegray, scalefactor = 1.1, minneighbors = 0, minSize = (5,5), maxSize = (80,80),    Flags = Cv2.cv.CV_HAAR_SCALE_IMAGE) # Mistaken eye for mouth! mouth = MOUTHCAScade.detectmultiscale (Facegray, scalefactor = 1.05, minneighbors = ten, minSize = (5,5), maxSize = (80,80), flags = CV2.CV . cv_haar_scale_image) ' ' ' # for big Alice Lefteye = Lefteyecascade.detectmultiscale (Facegray, scale Factor = 1.3, Minneighbors =, minSize = (40,40), maxSize = (80,80), flags = Cv2.cv.CV_HAAR_SCALE_IMAGE) Righteye = R Ighteyecascade.detectmultiscale (Facegray, scalefactor = 1.3, Minneighbors = +, MinSize = (40,40), maxSize = (80,80), flag s = Cv2.cv.CV_HAAR_SCALE_IMAGE) nose = Nosecascade.detectmultiscale (Facegray, scalefactor = 1.3, minneighbors = ten, Min Size = (40,40), maxSize = (80,80), flags = cv2.cv.CV_HAAR_SCALE_IMAGE) mouth = Mouthcascade.detectmultiscale (Facegr         Ay, Scalefactor = 1.3, minneighbors = ten, minSize = (40,40), maxSize = (80,80), flags = Cv2.cv.CV_HAAR_SCALE_IMAGE) " # for Face Lefteye = Lefteyecascade.detectmultiscale (Facegray, scalefactor = 1.3, Minneighbors =, minSize = (40,40), maxSize = (80,, flags = Cv2.cv.CV_HAAR_SCALE_IMAGE) Righteye = Righteyecascade.detectmultiscale (Facegray, scalefactor = 1.3, Minne Ighbors = +, MinSize = (40,40), maxSize = (80,80), flags = Cv2.cv.CV_HAAR_SCALE_IMAGE) nose = Nosecascade.detectmultis Cale (Facegray, scalefactor = 1.3, Minneighbors = +, MinSize = (40,40), maxSize = (80,80), flags = Cv2.cv.CV_HAAR_SCALE_IM  Age) Mouth = Mouthcascade.detectmultiscale (Facegray, scalefactor = 1.3, minneighbors = ten, minSize = (40,40), maxSize = (80,80), flags = Cv2.cv.CV_HAAR_SCALE_IMAGE) ###############################################################        ############# print ' Add Objects on Face ' for (Ex,ey,ew,eh) in Lefteye: Cv2.rectangle (Facecolor, (Ex,ey), (Ex+ew,ey+eh), (0,255,0), 2) for (Ex,ey,ew,eh) in Righteye:cv2.rectangle (f Acecolor, (Ex,ey), (Ex+ew,ey+eh), (255,255,0), 2) for (Ex,ey,ew,eh) in Nose:cv2.rectangle (Facecolor , (Ex,ey), (Ex+ew,ey+eh), (0,255,255), 2) for (Ex,ey,ew,eh) in Mouth:cv2.rectangle (Facecolor, (Ex,ey), (Ex+ew,ey+eh), (0,100 , +), 2) ############################################################################### #print ' Display face Image ' Plt.subplot (1,1,1), Plt.imshow (IMG), Plt.title (' Face Image '), Plt.xticks ([]), Plt.yticks ([]) plt.show () ################################################################################ print ' Goodbye! '
4. Steps to adjust parameters(1)Default Parameters: Set default parameters to see if an object is detected;
(2)detecting the range of objects: Modify the MinSize (minimum detected object size, in pixels * pixels) and maxsize (maximum size of the detected object), so that the object falls within the size range of the detector;
(3)Number of votes: Set Minneighbor to 0, observe some poll results, the most concentrated position of the poll result is the final test result. Increase the number of votes to filter out the object of the error detection, the more severe the condition of the object detection, reduce the number of votes to increase the probability of detecting objects, the more relaxed the condition of the detected object;
(4)scale: The scale change of the object to be detected, and the collection of the scaled image can form the image pyramid. Scalefactor the reasonable range between 1.1~1.4, the larger the scale, the easier the detection of the object, but the speed of detection, the smaller the scale, the more accurate detection, but the detection speed is slow. 5. Experimental results The first and the second face is the standard face, it should be said that the process of testing is quite perfect. The third face test result is also a try. When starting with the second picture, the classifier that detects the face will detect a second face on the left side of the snail shell. By adjusting the minsize and maxsize, the pseudo positive result of the background is removed. Then the left eye of the detection, Big Alice's left eye than Alice's left eye range is much larger, so need to be left eye minsize and maxsize small. Adjust the parameters of the right eye, the right eye test results overlap in the left eye, I think the left eye posture is more ambiguous, at the same time to meet the left eye detection standards. Detection of the nose, has not been detected results, because the naked eye view of the image found that Alice's nose is slender, the head slightly lower the width of the nose tip is not large enough. Detect mouth, the mouth of the vote all in the left eye, indeed little Alice's mouth because the face micro-side appears particularly small, and by contrast, the shape of the left eye is more like mouth.


The conclusion Paul Viola the Great God's article gives only a general framework. Would have liked to write this implementation process, but I have only finished the integration image and AdaBoost classifier. Finally how to combine to find more materials, but it is possible to stop, after all, this library function tuning speed can be very fast.

OpenCV using python--to adjust object recognition parameters for AdaBoost Cascade classifiers based on Haar features

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.