Implementing Face Tracking for WALL-E
🤖 Guide: Implementing Face Tracking for WALL-E
This guide outlines how to enable Face Tracking on your WALL-E robot using OpenCV. This feature allows WALL-E to detect a human face and automatically rotate its head to follow the movement.
1. Prerequisites
Ensure that the OpenCV data files (Haar Cascades) are installed on your Raspberry Pi.
Bash
sudo apt install opencv-data -y
2. Locate Data File
Find the absolute path of the haarcascade_frontalface_default.xml file, as the system might not locate it automatically.
Bash
find /usr -name "haarcascade_frontalface_default.xml"
# Common path: /usr/share/opencv4/haarcascades/haarcascade_frontalface_default.xml
3. Modify Backend (app.py)
Update the USBCameraStreamer class in web_interface/app.py to include the face detection and tracking logic.
A. Initialization (__init__)
Load the Haar Cascade model using the absolute path found in Step 2 and initialize the head position variable.
Python
def __init__(self):
self.camera = cv2.VideoCapture(0)
# Load the face detection model using the absolute path
self.face_cascade = cv2.CascadeClassifier('/usr/share/opencv4/haarcascades/haarcascade_frontalface_default.xml')
self.is_running = False
self.head_pos = 50 # Initial head position (Center)
B. Tracking Logic (get_frame)
Implement the logic to detect faces and send servo commands to the Arduino.
Python
def get_frame(self):
if self.is_running:
success, frame = self.camera.read()
if success:
# 1. Convert to grayscale for performance
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 2. Detect faces
faces = self.face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
# Draw a rectangle around the face
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
# 3. Calculate the center of the face
x_center = x + (w / 2)
# 4. Control Motors (Center is 320, Deadzone is 40px)
global arduino
# If face is on the left -> Rotate head left (Decrease angle)
if x_center < 280:
if self.head_pos > 0:
self.head_pos -= 1
arduino.send_command("G" + str(self.head_pos))
# If face is on the right -> Rotate head right (Increase angle)
elif x_center > 360:
if self.head_pos < 100:
self.head_pos += 1
arduino.send_command("G" + str(self.head_pos))
ret, buffer = cv2.imencode('.jpg', frame)
return buffer.tobytes()
return None
4. Run & Test
Restart the WALL-E service to apply the changes.
Bash
sudo systemd restart walle.service
sudo systemd status walle.service
-
Note: Verify that the service status is
active (running).

redclip 님의 최근 댓글