1 #course15.py2 ImportNumPy as NP3 ImportCv24 5 #multiple Cascades:https://github.com/itseez/opencv/tree/master/data/haarcascades6 7 #Https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml8Face_cascade = Cv2. Cascadeclassifier ('Haarcascade_frontalface_default.xml')9 #Https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_eye.xmlTenEye_cascade = Cv2. Cascadeclassifier ('Haarcascade_eye.xml') One AEyeglasses_cascade = Cv2. Cascadeclassifier ('Haarcascade_eye_tree_eyeglasses.xml') - -Smile_cascade = Cv2. Cascadeclassifier ('Haarcascade_smile.xml') the -Cap =Cv2. Videocapture (0) - - while(cap.isopened ()): +RET, IMG =Cap.read () -Gray =Cv2.cvtcolor (IMG, Cv2. Color_bgr2gray) +faces = Face_cascade.detectmultiscale (gray, 1.3, 5) ASmile =Smile_cascade.detectmultiscale (Gray) at for(Sm_x,sm_y,sm_w,sm_h)inchSmile: -Cv2.rectangle (Gray, (sm_x,sm_y), (Sm_x+sm_w,sm_y+sm_h), (0,0,255), 2) - - for(X,Y,W,H)inchfaces: -Cv2.rectangle (IMG, (x, y), (x+w,y+h), (255,0,0), 2) -Roi_gray = Gray[y:y+h, x:x+W] inRoi_color = Img[y:y+h, x:x+W] -Eyes =Eye_cascade.detectmultiscale (Roi_gray) to for(Ex,ey,ew,eh)incheyes: +Cv2.rectangle (Roi_color, (Ex,ey), (Ex+ew,ey+eh), (0,255,0), 2) - theFont =Cv2. Font_hershey_simplex *Cv2.puttext (IMG,' Eye', (ex+x,ey+y), font, 0.5, (11,255,255), 1, Cv2. LINE_AA) $ #eyeglasses = Eyeglasses_cascade.detectmultiscale (Roi_gray)Panax Notoginseng #For (E_GX,E_GY,E_GW,E_GH) in eyeglasses: - #Cv2.rectangle (Roi_color, (E_gx,e_gy), (E_gx+e_gw,e_gy+e_gh), (0,0,255), 2) the #Roi_gray = Gray[ey:ey+eh, ex:ex+ew]# + #Roi_color = Img[ey:ey+eh, ex:ex+ew]# A theCv2.imshow ('img', IMG) +K = Cv2.waitkey (& 0xff) - #print (k) $ ifK = = 27: $ Break - - cap.release () the cv2.destroyallwindows () - Print(smile)
Set up the operating platform see this series of (a),
The above code uses the principle of Haar cascade classifier, the following is a brief introduction to the principle of Haar cascade classifier:
The Haar classifier uses the class Haar feature of the target in the image to detect the target, and the method of integrating graph can accelerate the solution of the value of the class Haar feature.
One of the most basic class Haar features is a rudimentary weak classifier, which is optimized after the weak classifier is called the optimized weak classifier.
A strong classifier is formed by combining multiple optimized weak classifiers.
But the application of a single strong classifier is not good in actual detection,
So it was proposed to cascade or cascade several strong classifiers together,
This gives you a cascade classifier.
Detection effect: (can use the camera capture to form a video, on the video dynamic face, human eye detection, the following is the video)
Target detection for the use of OPENCV libraries in Python (ii)