-
Notifications
You must be signed in to change notification settings - Fork 0
/
Face_tracking.py
62 lines (47 loc) · 1.81 KB
/
Face_tracking.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import pyrealsense2 as rs
import numpy as np
import cv2
import serial
# file for face detection
faceCascade = cv2.CascadeClassifier('haar/haarcascade_frontalface_default.xml')
# Setup serial connection
ser = serial.Serial('/dev/ttyACM0', 9600) # Adjust '/dev/ttyACM0' to your Arduino's port
width, height = 640, 480
fps = 30
# Configure color streams
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.color, width, height, rs.format.bgr8, fps)
# Start streaming
pipeline.start(config)
try:
while True:
# Wait for a coherent pair of frames: depth and color
frames = pipeline.wait_for_frames()
color_frame = frames.get_color_frame()
if not color_frame:
continue
# Convert images to numpy arrays
color_image = np.asanyarray(color_frame.get_data())
# Circle at the center
cv2.circle(color_image, (int(width/2), int(height/2)), radius=3, color=(255, 0, 0), thickness=-1)
# Face detection
faces = faceCascade.detectMultiScale(color_image, 1.3, 5)
for face in faces:
x, y, w, h = face
cv2.rectangle(color_image, (x, y), (x+w, y+h), (255, 0, 0), 3)
x_center = int(x + w / 2)
y_center = int(y + h / 2)
cv2.circle(color_image, (x_center, y_center), radius=3, color=(0, 0, 0), thickness=-1)
error_yaw = x_center - (width/2)
error_pitch = y_center - (height/2)
# Send center_x and center_y to Arduino
ser.write(f"{error_yaw},{error_pitch}\n".encode())
# Show images
cv2.imshow('RGB Image', color_image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
# Stop streaming
pipeline.stop()
cv2.destroyAllWindows()