Welcome to DOFBOT SE repository
4.Face positioning
Face Positioning
The main purpose of face positioning is to achieve face tracking as a functional advancement. The principle is to determine the distance and position information of the face relative to the camera, and to judge by calculating the center point coordinates of the face in the camera view, thereby achieving face positioning. In the experimental results, you can see that it will always follow the center point of the face, displaying the center point.
Source code path: /home/yahboom/dofbot_ws/src/dofbot_face_follow/scripts/Face_position.ipynb
Main Code
- Import header files
import cv2 as cvimport threadingfrom time import sleepimport ipywidgets as widgetsfrom IPython.display import displayfrom face_pose import face_follow- Create instance, initialize parameters
xxxxxxxxxx# Create instancefollow = face_follow()# Initialize modemodel = 'General'- Main implementation function, print center coordinates.
xdef follow_function(self, img): img = cv.resize(img, (640, 480)) # Copy the original image to avoid interference during processing img = img.copy() # Convert image to grayscale gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # Face detection faces = self.faceDetect.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) if len(faces) != 0: face = self.face_filter(faces) # Face filtering (x, y, w, h) = face # Draw a rectangle on the original color map cv.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 4) cv.putText(img, 'Person', (280, 30), cv.FONT_HERSHEY_SIMPLEX, 0.8, (105, 105, 105), 2) point_x = x + w / 2 point_y = y + h / 2 print("x= ",point_x) print("y= ",point_y) return img- 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()After the program starts, you can see the video display and face selection.
