OpenCV3 Computer Vision +python (v)

Source: Internet
Author: User

Human face detection and recognition

This chapter introduces the Haar cascade classifier to determine whether a given image or sub-image matches a known object by contrasting the area of the adjacent image. This chapter will consider how to make multiple Haar cascade classifiers a hierarchy in which a classifier can recognize a whole area (such as a face), while other classifiers can recognize small areas (eyes, nose, and mouth).

The concept of Haar cascade

What do you want to pinpoint when it comes to target classification and location tracking? What is the identifiable part of the target?

Photographs (even images from webcams) may contain many delightful details. However, image details become unstable due to changes in lighting, viewing angle, visual range, camera jitter, and digital noise. People are not affected by the differences in these physical details when classifying them.

Extracting the details of the image is useful for generating stable classification results and tracking results. The results of these extracts are called features, which are professionally expressed as: extracting features from image data. Although any pixel can affect more than one feature, the feature should be much less than the number of pixels. The degree of similarity of two images can be measured by the Euclidean distance of their corresponding features.

For example, distances may be defined in spatial or color coordinates. The class Haar feature is a feature used to implement real-time human face tracking. The document "Robust real-time face Detection,paul Viola and Michael jones,kluwer academic publishers,2001" is the first to use this feature for facial detection. Each class Haar feature describes the contrast pattern of adjacent image regions. For example, edges, vertices, and thin lines can produce discriminant features.

For a given image, the feature may vary depending on the size of the area, and the area size can also be referred to as the window size. Even if the window size is not the same, only two images of different scales should have similar characteristics. Therefore, it is useful to generate features for different sizes of Windows. These feature sets are called cascading. The Haar cascade has scale invariance, in other words, it is robust in scale variation. OPENCV provides a scale-invariant Haar cascade classifier and tracker that can be saved to the specified file format.

The Haar cascade of OPENCV does not have rotational invariance. For example, the Haar cascade does not consider the inverted face image to be the same as an upright face image, while the face image on the side is not the same as the face image on the front. It is more possible to improve the rotational robustness of haar cascade with multiple image transformations and various window sizes, but this can become complex and cost more computing resources.

Get Haar cascading data

There will be a folder data/haarcascades in the copy of the OpenCV3 source code. This folder contains all the OpenCV face detection XML files and copies all the files in the Haarcascades folder to the Cascades folder:

Haarcascade_profileface.xml

Haarcascade_righteye_2splits.xml

Haarcascade_russian_plate_number.xml

Haarcascade_smile.xml

Haarcascade_upperbody.xml

From the file name, these cascade is used for the tracking of faces, eyes, nose and mouth. These files require a frontal, upright face image. These files are used when you create a face detector later. With a lot of patience and a powerful computer, you can create your own cascade and train these cascading to detect various objects.

Human face detection using OPENCV

The operation of detecting faces in a still image or video is very similar. The video face detection only reads each frame image from the camera, then uses the face detection method in the static image to detect. Of course, the video face detection also involves other concepts, such as tracking, while the human face detection in the static image has no such concept, but their basic theory is consistent.

Face detection in a static image

Face detection begins with loading images and detecting faces, which is also the most basic step. To make sense for the resulting results, you can draw a rectangle around the face of the original image.

Now that the project already contains the contents of the Haarcascades folder, a basic script is created below to implement face detection.

Import Cv2filename='3.jpg'def detect (filename): Face_cascade=cv2. Cascadeclassifier ('Haarcascade_frontalface_default.xml') Face_cascade.load ('c:/users/yif/anaconda3/envs/tensorflow/lib/site-packages/opencv_python-3.4.0.dist-info/haarcascades/ Haarcascade_frontalface_default.xml') img=cv2.imread (filename) Gray=Cv2.cvtcolor (Img,cv2. Color_bgr2gray) faces=face_cascade.detectmultiscale (Gray,1.3,5)     for(X,Y,W,H)inchfaces:img=cv2.rectangle (IMG, (x, y), (x+w,y+h), (255,0,0),2) Cv2.namedwindow ('Vikings Detected') Cv2.imshow ('Vikings Detected', IMG) cv2.waitkey () detect (filename)

Attention:

Face_cascade=cv2. Cascadeclassifier ('haarcascade_frontalface_default.xml')

face_cascade.load ('c:/users/yif/anaconda3/envs/tensorflow/lib/site-packages/opencv_ Python-3.4.0.dist-info/haarcascades/haarcascade_frontalface_default.xml')

The variable is a Cascadeclassifier object

The Face_cascade.detectmultiscale pass-through parameters are Scalefactor and minneighbors, which represent the compression rate of the image at each iteration of the face detection process and the minimum number of neighbors that each face rectangle retains.

The return value of the instrumentation operation is the face rectangle array. The function Cv2.rectangle allows the rectangle to be drawn by coordinates (x and y represent the coordinates of the upper-left corner, W and H for the width and height of the face rectangle)

The face is found by extracting the values in the faces variable in turn, and a blue rectangle is drawn around the face, which is drawn on the original image instead of the gray version of the image.


Human Face detection in video

Repeat this process on the frame of the video to complete the face detection in the video (such as the camera's input or video file).

The script will perform the following tasks: Turn on the camera, read the frame, detect the face in the frame, scan the eyes in the detected face, draw a blue rectangle to the face, and draw a green rectangle to the eye.

Import Cv2def Detect (): Face_cascade=cv2. Cascadeclassifier ('Haarcascade_frontalface_default.xml') Eye_cascade=cv2. Cascadeclassifier ('Haarcascade_eye.xml') Face_cascade.load ('c:/users/yif/anaconda3/envs/tensorflow/lib/site-packages/opencv_python-3.4.0.dist-info/haarcascades/ Haarcascade_frontalface_default.xml') Eye_cascade.load ('c:/users/yif/anaconda3/envs/tensorflow/lib/site-packages/opencv_python-3.4.0.dist-info/haarcascades/ Haarcascade_eye.xml') Camera=cv2. Videocapture (0)     while(True): Ret,frame=Camera.read () Gray=Cv2.cvtcolor (Frame,cv2. Color_bgr2gray) faces=face_cascade.detectmultiscale (Gray,1.3,5)         for(X,Y,W,H)inchfaces:img=cv2.rectangle (frame, (x, y), (x+w,y+h), (255,0,0),2) Roi_gray=gray[y:y+h,x:x+W] Eyes=eye_cascade.detectmultiscale (Roi_gray,1.03,5,0,( +, +))             for(Ex,ey,ew,eh)inchEyes:cv2.rectangle (IMG, (Ex,ey), (ex+ew,ey+eh), (0,255,0),2) Cv2.imshow ("Camera", frame) camera.release () cv2.destroyallwindows ()if__name__=="__main__": Detect ()

First, the Detect function is used to load the Haar cascade file, so that face detection can be performed.

Then open a videocapture target (initialize the camera). The parameters of the Videocapture constructor are used to represent the camera to be used

Next, capture the frame. The read () function returns two values: the first value is a Boolean value that indicates whether the frame was successfully read, and the second value is the frame itself. After snapping to a frame, convert it to a grayscale image. This is a necessary operation.

As with the example of a static image, call Detectmultiscale on a frame with a grayscale color space

There are several other parameters in eye detection, because the eye is determined on the basis of the human face, on the relatively small image to determine the eye.

Also use the loop output to detect the result of the eye and draw a green rectangle around it

Human Face recognition

Face detection is a very good function of OPENCV, it is the basis of face recognition. What is human face recognition? It's actually a program that can recognize a face in a given image or video. One way to achieve this is to train programs with a series of well-OPENCV images (face databases) and identify them based on these images.

This is the process of face recognition by OPENCV and its human face recognition module.

Another important feature of the face recognition module is that each recognition has a confidence score, so it can be filtered by setting thresholds in real-world applications.

Face recognition requires a face that can be obtained in two ways: get an image yourself or get a usable face image from a human face database for free

1. Generate facial recognition data

The script that generates the image continues below. Some images with different expressions are required, but you must ensure that the sample images meet the following criteria:

1) The image is in grayscale format with the suffix named. PGM

2) image shape as Square

3) image size to be the same

import Cv2def Generate (): Face_cascade=cv2. Cascadeclassifier ('Haarcascade_frontalface_default.xml') Eye_cascade=cv2. Cascadeclassifier ('Haarcascade_eye.xml') Face_cascade.load ('c:/users/yif/anaconda3/envs/tensorflow/lib/site-packages/opencv_python-3.4.0.dist-info/haarcascades/ Haarcascade_frontalface_default.xml') Eye_cascade.load ('c:/users/yif/anaconda3/envs/tensorflow/lib/site-packages/opencv_python-3.4.0.dist-info/haarcascades/ Haarcascade_eye.xml') Camera=cv2. Videocapture (0) Count=0     while(True): Ret,frame=Camera.read () Gray=Cv2.cvtcolor (Frame,cv2. Color_bgr2gray) faces=face_cascade.detectmultiscale (Gray,1.3,5)         for(X,Y,W,H)inchfaces:img=cv2.rectangle (frame, (x, y), (x+w,y+h), (255,0,0),2) F=cv2.resize (Gray[y:y+h,x:x+w], ( $, $)) Cv2.imwrite ('./DATA/%S.PGM'%Str (count), F) Count+=1Cv2.imshow ("Camera", frame)if(Cv2.waitkey (int( +/ A))) & (0xFF==ord ("Q")):             Breakcamera.release () cv2.destroyallwindows ( )if__name__=="__main__": Generate ()


2. Human Face recognition

OpenCV3 has three ways of face recognition based on three different algorithms: Eigenfaces, Fisherfaces, and local Binary Pattern

OpenCV3 Computer Vision +python (v)

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.