Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use opencv to realize face detection and eye detection

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use opencv to achieve face detection and eye detection". In daily operation, I believe many people have doubts about how to use opencv to achieve face detection and eye detection. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "how to use opencv to achieve face detection and eye detection". Next, please follow the editor to study!

Here, we will conduct face detection. Initially, the algorithm requires a large number of positive images (facial images) and negative images (no facial images) to train the classifier. Then, we need to extract features from it. To do this, use the Haar feature shown in the following figure. They're like our convolution cores. Each feature is a single value obtained by subtracting the sum of pixels under the white rectangle from the sum of the pixels under the black rectangle.

Now, all possible sizes and locations of each kernel are used to calculate many functions. Just imagine how many calculations it takes? Even a 24x24 window can produce more than 160000 features. For each feature calculation, we need to find the sum of pixels under the white and black rectangles. In order to solve this problem, they introduced the overall image. No matter how large your image is, it reduces the calculation of a given pixel to only four pixels. It's good, isn't it? It makes things so fast.

But of all the functions we have calculated, most are irrelevant. For example, consider the following figure. The first line shows two good features. The first feature selected seems to focus on the nature that the eye area is usually darker than the nose and cheek region. The second function you choose depends on the fact that the eyes are darker than the bridge of the nose. However, it doesn't matter if the same window is applied to the cheek or anywhere else. So how do we choose the best feature from more than 160000 features? It is implemented by Adaboost.

To this end, we apply all functions to all training images. For each function, it finds the best threshold that divides faces into positive and negative ones. Obviously, there will be errors or classification errors. We choose the features with the lowest error rate, which means that they are the most accurate features for the classification of face and non-face images. (the process is not that simple. At the beginning, each image has an equal weight. After each classification, the weight of the misclassified image will be increased. Then perform the same process. The new error rate will be calculated. And calculate the new weight. Continue this process until the desired precision or error rate is reached or the number of features required is found.

The final classifier is the weighted sum of these weak classifiers. The reason why it is called weak classification is that it alone cannot classify images, but forms a strong classifier together with other classifications. Even 200 functions can provide 95% accuracy testing, the paper said. Their final settings have about 6000 functions. (imagine reducing the number of features from more than 160000 to 6000. This is a great harvest.

So now you take a picture. Take each 24x24 window. Apply 6000 functions to it. Check if you have a face. Wow. Isn't this inefficient and time-consuming? Right. The author has a good solution to this.

In the image, most images are non-facial areas. Therefore, it is best to have an easy way to check whether the window is not a facial area. If not, discard it at once and do not deal with it again. Instead, focus on areas where you may have a face. In this way, we will spend more time examining possible facial areas.

For this reason, they introduced the concept of cascade classifier. Instead of applying all 6000 features to one window, they are grouped into classifiers at different stages and applied one by one. (usually the first few phases will contain very little functionality.) If the window fails in the first phase, it is discarded. We don't consider the rest of its functions. If passed, the second phase of the function is applied and the process continues. The window that goes through all stages is a facial area. How about this plan?!

The author's detector has more than 6000 features, 38 stages, and 1, 10, 25, 25 and 50 features in the first five stages. (the two features in the figure above are actually the best two features you get from Adaboost). According to the authors, each sub-window evaluates an average of 10 of more than 6000 features.

First, create a cv:: CascadeClassifier and load the necessary XML files using the cv:: CascadeClassifier:: load method. Then, the detection is completed using the cv:: CascadeClassifier:: detectMultiScale method, which returns the boundary rectangle of the detected face or eye

From _ _ future__ import print_functionimport cv2 as cvimport argparsedef detectAndDisplay (frame): frame_gray = cv.cvtColor (frame, cv.COLOR_BGR2GRAY) # Grayscale processing frame_gray = cv.equalizeHist (frame_gray) # histogram equalization #-- Detect faces faces = face_cascade.detectMultiScale (frame_gray) for (x + w) in faces: center = (x + w) / / 2) # get the central position of the box frame = cv.ellipse (frame, center, (w _ 2, _ 2), 0,0,360, (255,0,255), 4) # draw an ellipse faceROI = frame_gray [y _ frame _ That is, the rectangle of human face #-- In each face, detect eyes eyes = eyes_cascade.detectMultiScale (faceROI) for) in eyes: eye_center = (x + x2 + w2lash 2, y + y2 + h3lash 2) # get the center of the eye radius = int (round ((w2 + h3) * 0.25) # get the radius frame = cv.circle (frame, eye_center, radius) (255,0,0), 4) cv.imshow ('Capture-Face detection', frame) parser = argparse.ArgumentParser (description='Code for Cascade Classifier tutorial.') parser.add_argument ('-face_cascade', help='Path to face cascade.', default='haarcascades/haarcascade_frontalface_alt.xml') parser.add_argument ('- eyes_cascade', help='Path to eyes cascade.', default='haarcascades/haarcascade_eye_tree_eyeglasses.xml') parser.add_argument ('- camera') Help='Camera divide number.', type=int Default=0) args = parser.parse_args () face_cascade_name = args.face_cascadeeyes_cascade_name = args.eyes_cascadeface_cascade = cv.CascadeClassifier () eyes_cascade = cv.CascadeClassifier () #-1. Load the cascadesif not face_cascade.load (cv.samples.findFile (face_cascade_name)): print ('- (!) Error loading face cascade') exit (0) if not eyes_cascade.load (cv.samples.findFile (eyes_cascade_name)) ): print ('--(!) Error loading eyes cascade') exit (0) camera_device = args.camera#-- 2. Read the video streamcap = cv.VideoCapture (camera_device) if not cap.isOpened: print ('- (!) Error opening video capture') exit (0) while True: ret Frame = cap.read () if frame is None: print ('- (!) No captured frame-Breakdown') Break detectAndDisplay (frame) if cv.waitKey (10) = 27: break at this point, the study on "how to use opencv to achieve face detection and eye detection" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report