Face tracking using Raspberry Pi.
Hey guys, In this blog we will be making a simple face tracking device using Raspberry Pi.
Hey guys, In this blog we will be making a simple face tracking device using Raspberry Pi.
This device will help track your face i.e:- it will move wherever your face moves. Whenever you are designing a mechatronic system. The fundamental way to start is the structure. I have used a pan-tilt mechanism. the pan-tilt module by waveshare is amazing for that..you can also 3d print your own structure if you want from robu's website.
Here is the project output for reference. this is how the project looks after it is completed.
Now when we have taken a look at how the project output will look like. let's take a look at the components we need for the project.
- Raspberry pi 4b x 1
1 x Waveshare Pan tilt hat
1x Raspberry pi based camera
Code for Face Tracking
import numpy as np
import cv2
import os
import time
import picamera
import RPi.GPIO as GPIO
from PCA9685 import PCA9685
pwm = PCA9685()
pwm.setPWMFreq(50)
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height
current_PAN = 90
current_TILT = 20
pwm.setRotationAngle(1, 180) #PAN
pwm.setRotationAngle(0, current_TILT) #TILT
x=230
y=110
while True:
ret, img = cap.read()
#img = cv2.flip(img, -1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize=(20,20)
)
for (x,y,w,h) in faces:
print(x,y,w,h)
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
if x in range(220,240):
time.sleep(0.0001)
elif x> 240:
pwm.setRotationAngle(1, current_PAN) #PAN
current_PAN=current_PAN-2
time.sleep(0.0001)
elif x<220:
pwm.setRotationAngle(1, current_PAN) #PAN
current_PAN=current_PAN+2
time.sleep(0.0001)
if y in range(60,140):
time.sleep(0.0001)
elif y> 140:
pwm.setRotationAngle(0, current_TILT) #PAN
current_TILT=current_TILT+2
time.sleep(0.0001)
elif y<60:
pwm.setRotationAngle(0, current_TILT) #PAN
current_TILT=current_TILT-2
time.sleep(0.01)
cv2.imshow('video',img)
k = cv2.waitKey(30) & 0xff
if k == 27: # press 'ESC' to quit
break
cap.release()
cv2.destroyAllWindows()
Stay safe. Stay tuned at robu.in