Welcome to JetCobot repository
4. Face location experiment
4. Face positioning experiment
The principle of face positioning experiment is to determine the distance and position information of the face from the camera, and to judge by calculating the coordinates of the center point of the face in the camera screen, so as to realize face positioning. The experimental results show that it will always find the center point of the face, print the coordinates of the center point, and draw a box on the face.
Code path: ~/jetcobot_ws/src/jetcobot_face_follow/face_detection.ipynb
4.1. Main code
Import header file
xxxxxxxxxximport cv2 as cvimport threadingfrom time import sleepimport ipywidgets as widgetsfrom IPython.display import displayfrom face_pose import face_followCreate an instance and initialize parameters
xxxxxxxxxx# Create an instancefollow = face_follow()# Initialize modemodel = 'General'Main process
xxxxxxxxxxdef camera(): global model # 打开摄像头 Open camera capture = cv.VideoCapture(0) while capture.isOpened(): try: _, img = capture.read() img = cv.resize(img, (640, 480)) img = follow.follow_function(img) if model == 'Exit': cv.destroyAllWindows() capture.release() break imgbox.value = cv.imencode('.jpg', img)[1].tobytes() except KeyboardInterrupt:capture.release()start
xxxxxxxxxxdisplay(controls_box,output)threading.Thread(target=camera, ).start()
4.2. Run the program
Click the Run the entire program button on the jupyterlab toolbar, and then scroll to the bottom.

After the program starts, put the face image into the camera screen, and you can see the face in the green box. Move the face image and the box will move with the face. At the same time, the center coordinates of the face will be printed below.
