Transfer: https://www.cnblogs.com/neo-T/p/6430583.html
OpenCV has helped us to do, we can simply call the API function corresponding to first give the code:
1 # - * - Coding: UTF-8 - * - 2 3 # # keyword can not turn off the camera, code indentation might be a problem. Displaying an image for which is not introduced . 4 Import SYS . 5 from the PIL Import Image . 6 Import CV2 . 7 . 8 DEF AICatchVideo (v_name, camera_index): . 9 # open the camera, the video source may use the saved video 10 CAP = CV2. VideoCapture (0) . 11 # Create window 12 is cv2.namedWindow (v_name) 13 is # loading face data (facial feature), using # tell OpenCV face classifier 14 face_cascade cv2.CascadeClassifier = ( './haarcascade_frontalface_alt2.xml ' ) 15 the while True: 16 RET, Frame = cap.read () . 17 IF Not RET: 18 is # waiting for user . 19 cv2.waitKey (30 ) 20 is # give each frame - "image calculation 21 # gradation conversion: converted to grayscale to reduce the strength calculation in FIG 22 is gray = cv2.cvtColor (Frame, cv2.COLOR_BGR2GRAY) 23 is # Comparative data captured by the camera - "training set of facial feature 24 faces = face_cascade.detectMultiScale (gray , by scaleFactor = 1.3, minNeighbors =. 5, the minSize = (32, 32 )) 25 IFlen (Faces)> 0: 26 is 27 for (X, Y, W, H) in Faces: 28 # in the window face recognition, draw a rectangle 29 Frame cv2.rectangle = (Frame, (X, Y), ( W + X, Y + H), (255, 0, 0), 2 ) 30 # display image 31 is cv2.imshow (v_name, Frame) 32 Key = cv2.waitKey (20 is ) 33 is IF Key & == 0xFF the ord ( ' Q ' ): 34 is BREAK 35 36 # window release: cache - "memory 37 [ cap.release () 38 is cv2.destroyAllWindows () 39 40 41 is IF the __name__ == ' __main__ ' : 42 is # Print (len (the sys.argv)) 43 is # Print ( 'content' + the sys.argv [0]) 44 is # IF len (the sys.argv) = 2!: 45 # Print ( 'the Usage: the camera_ID% S: {}' the format (the sys.argv [0]).) 46 is # the else: 47 AICatchVideo ( ' face recognition region ' , the sys.argv [0])
# Tell OpenCV face recognition using classifiers classfier = cv2.CascadeClassifier ( "./ haarcascade_frontalface_alt2.xml" )
This line of code specifies the OpenCV choose which classifier (note that certain habits classify this argument, ML's study and research is a variety of supervised classification), OpenCV offers a variety of classifiers:
There may be more than one face on the same screen, so we need to use a for loop to all who read out the detected face, and then one by one with a rectangular box out, this is the role for the next statement. Opencv gives the start coordinates of each human face in the image (upper left corner, x, y) and the length, width (h, w), we can accordingly taken out of the face. Which, cv2.rectangle () to complete the frame work, where I consciously extended by 10 pixels to the frame slightly larger than the face area. cv2.rectangle () function of the last two parameters to specify a color for the border of the rectangle, a degree of thickness for the specified rectangular border line.