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 realize face recognition and focus person Detection based on Python

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

Share

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

This article mainly explains "how to achieve face recognition and focus person detection based on Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve face recognition and focus person detection based on Python".

Words written in the front

Based on the model of dlib library, face recognition and focus person detection are realized. The final effect is that the color of the identification box of the focus character is different from that of other character boxes.

Preparatory work

Need to install the python environment, install dlib, opencv-python libraries, etc., you can read the error message (you can use PyCharm to run and edit the py file), and then complete the required library, there will be complete code at the end of the article, but you need to be in the same path with the shape_predictor_68_face_landmarks.dat model file, and then enable it. (Baidu can download it)

Design process

Because it is completed in their own computer must-do design, so the early also experienced the corresponding Python installation and environmental configuration, the corresponding resource library installation, such as dlib, opencv-python and so on.

Then run the reference file opencv_webcam_face_detection.py which synthesizes (68 facial feature point detection models to complete face detection and labeling in still images) and (completes face detection and location in real-time video), and finds that it can realize face detection in real-time video.

Analyze the code of the reference file and understand the meaning of each sentence of code. Compare and find the functional modules needed by the design to achieve 1280x720 video output and focus character recognition of win10-like cameras.

Find and learn relevant materials on the Internet, refer to the algorithm of win10 camera, create your own focus character algorithm based on distance and area, and add and modify the source code according to your own needs.

Finally, the code is tested and constantly modified to the most appropriate version.

Focus person algorithm

Inherent logic: imitate the win10 camera, when there is more than one person, give priority to the focus person, but if the face area in other places is more than 4 times the central face area, then choose the other place as the focus person.

Actual code

Import dlibimport cv2import math# camera parameters set cam = cv2.VideoCapture (0) # parameter 0, call the computer's camera cam.set (3, 1280) # parameter 3, set the width resolution to 1280cam.set (4720) # parameter 4, set the height resolution to 72 seconds, set the frame color and width of the human face frame, and make it easy to distinguish the focus person color_focus = (255,0,255) # set the color of the focus face frame Fuchsia color_other = (255,255,255) # sets the color of the rest of the face frames White lineWidth_focus = 2 # set the width of the focus face box lineWidth_other = 1 # set the width of other face boxes # set some parameters calculated w = cam.get (3) / 2 # set the Abscissa of the center of the screen Xh = cam.get (4) / 2 # set the ordinate of the center of the screen Yd_center = 10000 # preset the distance from the face box to the center of the screen index_ Center = 0 # face frame sequence number index_area = 0 # face frame sequence number area_center =-1 # preset the area of the nearest face frame to the center area =-1 # # default face frame area detector = dlib.get_frontal_face_detector () # load the face detector included in this library predictor_path = "shape" _ predictor_68_face_landmarks.dat "# set the path location of the face prediction model predictor = dlib.shape_predictor (predictor_path) # instantiate while True: # ret_val when the video input is obtained Img = cam.read () # read every frame of the video Color format is BGR format, rgb_image = cv2.cvtColor (img, cv2.COLOR_BGR2RGB) # Color BGR format is converted to RGB format faces = detector (rgb_image) # return RGB format face capture box # logical algorithm: when there is more than one person, the focus person is preferred, but if the face area of other places is greater than 4 times the central face area, the focus person is selected. # this for loop first finds the sequence number and priority area of the face frame closest to the center of the screen, for I, det in enumerate (faces): # iterates through all face frames. I is the sequence number of the face frame. Det is each face frame d = math.sqrt ((w-(det.left () + (det.right ()-det.left ()) / 2)) * * 2 + (h-(det.top () + (det.bottom ()-det.top ()) / 2)) * * 2) # calculate the distance from the face frame to the center of the screen if d

< d_center: # 对比刚计算出的距离与设定的最近距离,达成选择更小 index_center = i # 更新距离最近时的人脸框序号 d_center = d # 更新最近距离 area_center = abs((det.right() - det.left()) * (det.bottom() - det.top())) # 算出该人脸框的面积(距离更近优先) # 这个for循环求出面积最大的人脸框的序号和面积优先面积 for i, det in enumerate(faces): # 遍历所有人脸框,i是人脸框序号,det是每个人脸框 if abs((det.right() - det.left()) * (det.bottom() - det.top())) >

Area: # compare the face area with the set maximum area Realize the choice of larger index_area = I # update larger face frame serial number area = abs ((det.right ()-det.left ()) * (det.bottom ()-det.top () # calculate the face frame area (larger area first) if area > 5*area_center: # judgment basis If the area priority area is more than 5 times the distance priority area, the focus person will be selected first, otherwise the distance will be given priority. Index_center = index_area # when the area is priority, use the largest face box sequence number for I, det in enumerate (faces): # traverse all face frames if I = = index_center: # determine the sequence number of the focus face frame print (d_center, I) # output the distance to the center of the focus person Easy to debug cv2.rectangle (img, (det.left (), det.top ()), (det.right (), det.bottom ()), color_focus, lineWidth_focus) # draw the focus face frame shape = predictor (img, det) # from the prediction model Get 68 character feature points for p in shape.parts (): # traverse 68 character feature points cv2.circle (img, (p. X, p. Y), 2, (124,252,0),-1) # set the shape and color of 68 focus points Tea green, solid else: cv2.rectangle (img, (det.left (), det.top ()), (det.right (), det.bottom ()), color_other, lineWidth_other) # draw other people's face frames shape = predictor (img, det) # from the prediction model Get 68 character feature points for p in shape.parts (): # traverse 68 character feature points cv2.circle (img, (p. X, p. Y), 2, (255,255,255),-1) # set the shape and color of 68 points of other characters White, solid cv2.imshow ('my webcam', img) # output frame animation if cv2.waitKey (1) = = 27: # set a residence time and wait for the user to trigger an event. If the user presses ESC (ASCII code is 27), then execute if body break # (if body) to exit cv2.destroyAllWindows () # destroy all output image windows

In order to easily distinguish the focus people from other characters, the width of the focus character box is set to 2, the color is set to fuchsia, and the 68 recognition points are set to tea green; the width of the other character boxes is set to 1, the color is set to white, and the 68 recognition points are set to white.

Then many tests are carried out, and by sorting out the test results, it is found that there is no error in the algorithm, and the focus figure is determined according to the two factors of distance and area. The successful operation diagram is as follows:

The picture is not shown, but the main character is a purplish red box, and the other characters are white circles. As expected.

Thank you for reading, the above is the content of "how to achieve face recognition and focus person detection based on Python". After the study of this article, I believe you have a deeper understanding of how to achieve face recognition and focus person detection based on Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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