Human face detection and Recognition Python implementation series (2)--recognition of human face

Source: Internet
Author: User

Human face detection and Recognition Python implementation series (2)--recognition of human face

From the real-time video stream to recognize the face area, from the principle, it is still part of the Machine learning field, in essence, Google using deep learning to identify the cat is no different. The program is trained by a large number of face picture data, and the mathematical algorithm is used to establish a reliable facial feature model so that human face can be identified. Fortunately, these work OpenCV have done for us, we just need to call the corresponding API function, first give the code:

#-*-coding:utf-8-*-ImportCv2ImportSYS fromPILImportImagedefCatchusbvideo (Window_name, Camera_idx): Cv2.namedwindow (window_name)#video sources can come from a saved video or directly from a USB cameraCap =Cv2. Videocapture (CAMERA_IDX)#tell OpenCV to use the face recognition classifier.Classfier = Cv2. Cascadeclassifier ("/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml")        #identifies the color of the border to be drawn after the face, RGB formatcolor = (0, 255, 0) whilecap.isopened (): OK, frame= Cap.read ()#reading a frame of data        if  notOK: Break  

#将当前帧Convert to grayscale images
Grey = Cv2.cvtcolor (frame, cv2. Color_bgr2gray)#face Detection, 1.2 and 2 are picture scaling and valid points to detectFacerects = Classfier.detectmultiscale (grey, Scalefactor = 1.2, minneighbors = 3, MinSize = (32, 32)) ifLen (facerects) > 0:#More than 0 to detect human face forFacerectinchFacerects:#frame each face individually.X, Y, W, h =facerect Cv2.rectangle (frame, (x-y-10), (x + W + ten, Y + H +), color, 2) #Display Imagecv2.imshow (Window_name, frame) C= Cv2.waitkey (10) ifC & 0xFF = = Ord ('Q'): Break #release the camera and destroy all Windowscap.release () cv2.destroyallwindows ( )if __name__=='__main__': ifLen (SYS.ARGV)! = 2: Print("usage:%s camera_id\r\n"%(Sys.argv[0]))Else: Catchusbvideo ("recognize face areas", int (sys.argv[1]))

First look at the program output results:

The program correctly identified my face, plus a blank line of less than 50 lines of code, or very simple. Of course, most of the work OpenCV has been quietly for us to do, so we use it so simple. There are several places to focus on the code, the first is the face classifier line:

# tell OpenCV to use the face recognition classifier classfier = Cv2. Cascadeclassifier ("/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml  ")

This line of code specifies which classifier the OPENCV chooses to use (note that it is customary to classify this, ML's supervised learning is a variety of classification issues), and OpenCV offers a variety of classifiers:

All the classifiers provided for the OpenCV3.2 installed on my computer have the recognition of the eyes (even the left and right eye), the recognition of the body, the smiling faces, and even the recognition of the cat face, interested can try each. With respect to face recognition, OPENCV offers multiple classifiers to choose from, of which Haarcascade_frontalface_alt_tree.xml is the strictest classifier, and light, with a hat, may not recognize the face. The others are slightly better, the default one is the most relaxed, and in some cases my family's lanterns will be recognized as adult faces; In addition to different installation environments, the installation path of the classifier may be different, please modify the code according to the actual installation path of the classifier after installing OPENCV. In addition, if we want to build our own classifiers, such as detecting flames (fire alarms), cars (determining the number of cars at intersections), we can still use OPENCV training to build, detailed instructions see OPENCV's official documentation.

Next, we'll explain the following lines of code:

# face Detection, 1.2 and 2 are picture scaling and the number of effective points to be detected facerects = Classfier.detectmultiscale (grey, Scalefactor = 1.2, minneighbors = 3 , MinSize = (+, +))if len (facerects) > 0:          # greater than 0 detects face                                    for       in facerects:  # separate frame out each face        x, y, W, h = facerect                -Ten, Y (x + W + ten, Y + H +), color, 2)

where Classfier.detectmultiscale () is the function that completes the actual face recognition work, the function parameter is described as follows:

Grey: The image data to be recognized, even if not converted to grayscale, can be recognized, but gray-scale graphs can reduce the computational strength, because the detection is based on the Hal feature , after the conversion of each point of the RGB data into one-dimensional grayscale, so that the computational strength is reduced a lot)

scalefactor: Image scaling, it can be understood that the same object and camera distance, its size is also different, it must be scaled to a certain size to facilitate identification, this parameter specifies the scale of each zoom

minneighbors: Detection of the number of effective points around the feature detection point at the same time, so as to avoid the selected feature detection point is too small to cause omission

minSize: Minimum value of the feature detection point

It is possible to have multiple faces on the same screen, so we need to use a For loop to read all the detected faces and then use the rectangle box one after the other for the purpose of the next for statement. OpenCV will give each face a starting coordinate in the image (upper left, X, y), and length, Width (h, W), so we can intercept the face accordingly. Among them,Cv2.rectangle () finished the work of the frame, where I have consciously expanded the area of 10 pixels that are slightly larger than the face of a person. Cv2.rectangle () The last two parameters of the function a color that specifies the rectangle's border, and a level that specifies the thickness of the rectangle's border line.

OK, the face recognition of the matter is clear, the next article about how to prepare training data, only training data enough, our program can identify who this is, and not whether the misbehavior box personal face to finish.

Human face detection and Recognition Python implementation series (2)--recognition of human face

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.