Here is a project for facial recognition + tracking for arduino
This will locate and track your face
Windows only
(located and track your face on the program silly. a.k.a A rectagle is put around your face in program and servo will move camera in the direction of your face.
You Need..
Software:
- Python (version 2.7 or younger)
- Haarscade click for install (This is a pre trained file for detecting human faces)
- OpenCV click and scroll for download (Real time computer vision detection and other)
Type the following in cmd (type cmd in windows searchbar)
pip install pyserial
pip install numpy
Hardware:
- Bread Board
- Arduino
- Jumper wires
- Pan/tilt Mechanism (two servos somehow connected)
- Web cam
Coding
To start we are going to make a new python file
Python code
Type "Idle" in windows searchbar and press run. this should bring up Python and then past the following code:
import cv2
import time
import serial
from serial import Serial
face_cascade= cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap=cv2.VideoCapture(1)
ArduinoSerial=Serial('com3',9600,timeout=0.1)
time.sleep(1)
while cap.isOpened():
ret, frame= cap.read()
frame=cv2.flip(frame,1)
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces= face_cascade.detectMultiScale(gray,1.1,6)
for x,y,w,h in faces:
string='X{0:d}Y{1:d}'.format((x+w//2),(y+h//2))
print(string)
ArduinoSerial.write(string.encode('utf-8'))
cv2.circle(frame,(x+w//2,y+h//2),2,(0,255,0),2)
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),3)
cv2.rectangle(frame,(640//2-30,480//2-30),
(640//2+30,480//2+30),
(255,255,255),3)
cv2.imshow('img',frame)
'''for testing purpose
read= str(ArduinoSerial.readline(ArduinoSerial.inWaiting()))
time.sleep(0.05)
print('data from arduino:'+read)
'''
if cv2.waitKey(10)&0xFF== ord('q'):
break
cap.release()
cv2.destroyAllWindows()
After all this is pasted and put it in the folder that you created
Arduino Code
This code with control the servos
//code created by Luis Mat
#include<Servo.h>
Servo x, y;
int width = 640, height = 480;
int xpos = 90, ypos = 90;
void setup() {
Serial.begin(9600);
x.attach(9);
y.attach(10);
x.write(xpos);
y.write(ypos);
}
const int angle = 2;
void loop() {
if (Serial.available() > 0)
{
int x_mid, y_mid;
if (Serial.read() == 'X')
{
x_mid = Serial.parseInt();
if (Serial.read() == 'Y')
y_mid = Serial.parseInt();
}
if (x_mid > width / 2 + 30)
xpos += angle;
if (x_mid < width / 2 - 30)
xpos -= angle;
if (y_mid < height / 2 + 30)
ypos -= angle;
if (y_mid > height / 2 - 30)
ypos += angle;
if (xpos >= 180)
xpos = 180;
else if (xpos <= 0)
xpos = 0;
if (ypos >= 180)
ypos = 180;
else if (ypos <= 0)
ypos = 0;
x.write(xpos);
y.write(ypos);
}
}
*I apologize as I couldnt find any software to create a schematic for servos and my orginization is sub-par; I must write them. This may be hard to understand so ask questions down below😞
SIGNAL PINS
Horizontal - pin 9
Verticle - pin 10
Signal wires from servo can be, white, orange, yellow, or blue
GND/NEGATIVE
Connect what is labeled GND on the arduino to the negative column on the bread board
Connect the black or brown servo pin to the negative column
5v/POSITIVE
Connect what is labeled 5v on arduino to the positive column on the bread board
Connect Red servo pin to positive column
UPLOAD and have fun :)
I will try to answer questions
Dheirya_Tyagi_CEO ~ November 27
wish I could try... I don't have a windows laptop. Also in your post, you say "Signal wires from servo can be, white, orange, yellow, or blue. " The colors of the wires don't matter, it's just for organization how you color them. And for a schematic maker you can try https://circuito.io