python code for Face Tracking Using arduino IDE

It is possible to Face Tracking Using arduino with the help of python code . so in this article we will see step by step procedure for Face Tracking using Arduino python code project.
so friends let’s start :

Face Tracking Using arduino with the help of python code

Step 1: Some pre-requirements For Face Tracking Using Arduino IDE:

Hardware :
Servos x 2
Breadboard
Arduino UNO
Web Cam
Servo Pan Tilt Kit

Software :
OpenCV
Python 2.7
pyserial
numpy.
Haarcascade.

Step 2: You need to check python version

for checking the current python version use the following command line
python –version
python -version

Step 3: Setting Up Python Environment

Installing Python:
So first we need Python 2.7 up and running. To do this first download and Install python 2.7.14. To check if it is installed correctly Goto : Windows Search >> Type “IDLE” >> Hit Enter. A Python Shell should pop up.

Step 3: After the seup python environment you need to import following library :

Installing ‘pyserial’, ‘OpenCV” and “numpy” in Python:

To install these modules we will use use pip install,

First open Command prompt and then type the following codes:-


>pip install numpy
>pip install serial
>pip install opencv-python

After the import the library then next step is write Python Script

#import all the required modules
import numpy as np
import serial
import time
import sys
import cv2
#Setup Communication path for arduino (In place of 'COM5' put the port to which your arduino is connected)
arduino = serial.Serial('COM5', 9600) 
time.sleep(2)
print("Connected to arduino...")
#importing the Haarcascade for face detection
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#To capture the video stream from webcam.
cap = cv2.VideoCapture(0)
#Read the captured image, convert it to Gray image and find faces
while 1:
    ret, img = cap.read()
    cv2.resizeWindow('img', 500,500)
    cv2.line(img,(500,250),(0,250),(0,255,0),1)
    cv2.line(img,(250,0),(250,500),(0,255,0),1)
    cv2.circle(img, (250, 250), 5, (255, 255, 255), -1)
    gray  = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3)
#detect the face and make a rectangle around it.
    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),5)
        roi_gray  = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]
        arr = {y:y+h, x:x+w}
        print (arr)
        
        print ('X :' +str(x))
        print ('Y :'+str(y))
        print ('x+w :' +str(x+w))
        print ('y+h :' +str(y+h))
# Center of roi (Rectangle)
        xx = int(x+(x+h))/2
        yy = int(y+(y+w))/2
        print (xx)
        print (yy)
        center = (xx,yy)
# sending data to arduino
        print("Center of Rectangle is :", center)
        data = "X{0:d}Y{1:d}Z".format(xx, yy)
        print ("output = '" +data+ "'")
        arduino.write(data)
#Display the stream.
    cv2.imshow('img',img)
#Hit 'Esc' to terminate execution 
    k = cv2.waitKey(30) & 0xff
    if k == 27:
       break

Step 4: After the python script we write the code for Arduino IDE

For the Arduino code please visit to Arduino official site

Summary :

In this Article we saw python code for the Face Tracking Using arduino with the help of python code. so About this python code you have any problem then free to ask me.

Thank you.

I am the founder of Pythonslearning, a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

Leave a Comment