Object Tracking Camera using Raspberry Pi and OpenCV

by Jun 20, 2020Raspberry Pi projects

Introduction:

In this project we will see how we can use the power of image processing and simple mechanics (Pan and Tilt Mechanism ) to track any Face so that the face is always at the centre of the camera feed.

We can certainly track any desired object instead of a face (shown in this post). Also we can program to track only a particular persons face using Face recognition Feature.

Let’s have a look at the Demo working of the project:

CODE:

The entire code for this project is available at this GitHub Repository: https://github.com/htgdokania/FaceTrack_PiCam

Parts required:

  • Raspberry pi
  • Pi Camera
  • Two micro Servos
  • Any Pan and Tilt Mechanism.(Refer Here)

Structure/Work Flow:

  • Step1: Setup up Pi camera along with Pan and Tilt Mechanism.
  • Step2: Do the Servo connections along with Pi camera cable attachment.
  • Step3: Write a code to control the servo movement servomove.py
  • Step4: Write the main.py code
    • Start Reading Frames from Pi Camera.
    • Detect Face in the current frame and get its coordinates
    • Calculate the deviation of the face from the centre and set it as error
    • implement a Proportional controller to calculate value to change the servo position
    • send the values to the servo movement function to get the face back at centre.

Step1: Setup up Pi camera along with Pan and Tilt.

  • First mount the camera on the pan and tilt mechanism along with the servos.It should look like this.
  • Make Sure the camera is moving freely on both the x and y axis.Something like this:

Step2: Do the Servo connections along with Pi camera cable.

  • connect the Two servo signal pins to any of the two GPIO pins of your choice.
  • Power the Servos using 5v supply of pi . Alternatively we can power it using a usb power supply.

Step3: Write a code to control the servo movement (servomove.py)

  • Lets Define servomove.py
  • First import libraries
import RPi.GPIO as GPIO          
from time import sleep
  • Next define the signal pins of the the servo motors and initialize them at 50hz (as per servo documentation):
ypin = 11
xpin = 13

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(xpin,GPIO.OUT)
GPIO.setup(ypin,GPIO.OUT)

x=GPIO.PWM(xpin,50)
y=GPIO.PWM(ypin,50)
Note: Generally Servo motors can move  from 0 to 180 degrees.
 > 0 at 1ms PWM signal at Duty Cycle 5 for 50 Hz.
 > 180 at 2ms PWM signal at Duty Cycle 10 for 50 Hz.

Next we define a class within which we define few functions to control x and y position.

Initially we set the servo duty cycle so that it is at the centre at 90 degree .Adjust the duty cycle to bring at centre.

class servopos():
    def __init__(self):
        self.currentx,self.currenty=7,4
        x.start(self.currentx)
        y.start(self.currenty)
        sleep(1)
        x.ChangeDutyCycle(0)
        y.ChangeDutyCycle(0)
            
    def setposx(self,diffx):
        self.currentx+=diffx
        self.currentx=round(self.currentx,2)
        if(self.currentx<15 and self.currentx>0):
            x.ChangeDutyCycle(self.currentx)  
    def setposy(self,diffy):
        self.currenty+=diffy
        self.currenty=round(self.currenty,2)
        if(self.currenty<15 and self.currenty>0):
            y.ChangeDutyCycle(self.currenty)  
    def setdcx(self,dcx):
        x.ChangeDutyCycle(dcx)  
    def setdcy(self,dcy):
        y.ChangeDutyCycle(dcy)

Step4: Write the main.py code

  • First import the required libraries.
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time 
import cv2
from servomove import servopos
ser=servopos() # declare object of class servopos()
  • load the Haar cascade file to detect face
face_cascade= cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  • Set the PID values for x and y axis movement. we set I,D values as 0 for proportional controller.
Px,Ix,Dx=-1/160,0,0
Py,Iy,Dy=-0.2/120,0,0

#initialize some other required vaqriables
integral_x,integral_y=0,0
differential_x,differential_y=0,0
prev_x,prev_y=0,0
  • Set the Pi camera parameters to load frames in an optimised way:
width,height=320,240
camera = PiCamera()
camera.resolution = (width,height)
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(width,height))
time.sleep(1)

Start Reading Frames from Pi Camera.

  • Inside a for loop read frames continuously using camera.capture_continuous()
  • Store image array as image variable and flip it to correct camera orientation.
  • Also set both the servos duty cycle to 0 (This will ensure servos are stable and not vibrate).
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    image = frame.array
    frame=cv2.flip(image,1)
    ser.setdcx(0)
    ser.setdcy(0)

Detect Face in the current frame and get its coordinates

  • Detect Faces using the haar Cascade file we loaded earlier.
  • Get the face coordinates of the first face detected.
    gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)    
    #detect face coordinates x,y,w,h
    faces=face_cascade.detectMultiScale(gray,1.3,5) # face_cascade loaded earlier.
    c=0
    for(x,y,w,h) in faces:
        c+=1
        if(c>1):  # we just take care of the first face detected.
            break

Calculate the deviation of the face from the centre and set it as error

  • The deviation of the face centre coordinates from the centre of the frame it the error.
  • Next,calculate integral,differential values and set the new face coordinates as previous coordinates for next iteration.
        #centre of face
        face_centre_x=x+w/2
        face_centre_y=y+h/2
        
        #calculate pixels to move 
        error_x=160-face_centre_x  # X-coordinate of Centre of frame is 160
        error_y=120-face_centre_y # Y-coordinate of Centre of frame is 120
        
        integral_x=integral_x+error_x
        integral_y=integral_y+error_y
        
        differential_x= prev_x- error_x
        differential_y= prev_y- error_y
        
        prev_x=error_x
        prev_y=error_y

Implement a Proportional controller to calculate value to change the servo position

  • Finally Calculate the value for x movement and y movement using PID logic.(valx,valy)
Arduino UNO Robotics Part 2: PID Control – Trinirobotics
        valx=Px*error_x +Dx*differential_x + Ix*integral_x
        valy=Py*error_y +Dy*differential_y + Iy*integral_y
        
        valx=round(valx,2) #round off to 2 decimel points.
        valy=round(valy,2)

Send the values to the servo movement function to get the face back at centre.

  • Once we calculate the above values we move the servos accordingly by callying the setposx() and setposy() functions declared inside servopos() class.
  • We set a condition check and move servos only if the error is more than 20 pixels to avoid unnecessary movement.
        if abs(error_x)<20:
            ser.setdcx(0)
        else:
            if abs(valx)>0.5:
                sign=valx/abs(valx)
                valx=0.5*sign
            ser.setposx(valx)

        if abs(error_y)<20:
            ser.setdcy(0)
        else:
            if abs(valy)>0.5:
                sign=valy/abs(valy)
                valy=0.5*sign
            ser.setposy(valy)

Display:

  • Finally we draw a box around the detected face and display the frame for visualization purpose.
        if(c==1):
            frame=cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),6)

    cv2.imshow('frame',frame) #display image
  • Set a logic to exit fromt the loop and close all open windows, if user presses ‘q’ on keyboard.
    key = cv2.waitKey(1) & 0xFF
    rawCapture.truncate(0)
    if key == ord("q"):
        break

cv2.destroyAllWindows()

That’s All .Now run the main.py code to see it in action.All you need to do is moe your face and see how your bot keeps tracking it continuosly.

Creating a multiplication Skill in Alexa using python

Written By Monisha Macharla

Hi, I'm Monisha. I am a tech blogger and a hobbyist. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning!

RELATED POSTS

Magic Wand using Raspberry Pi and OpenCV

Magic Wand using Raspberry Pi and OpenCV

What if I could do things with a swing of a wand? Practically impossible but with technology maybe not. In this tutorial, we will use a raspberry pi and OpenCV to try and recreate something similar to a wand. It will perform specific functions when it recognizes...

IBM Watson IoT Platform with Raspberry Pi

IBM Watson IoT Platform with Raspberry Pi

IBM Watson is one of the leading cloud computing platforms there is. It has almost all the features one could expect from a modern-day cloud platform, from machine learning support to IoT, even till edge computing. In this tutorial, we will use raspberry pi and an...

Home Automation With Telegram and Raspberry Pi

Home Automation With Telegram and Raspberry Pi

In this Article we will see how we can use Telegram for Home Automation. Telegram is an extremely popular multi-platform messaging service. Just like any other messaging platform, it is used to send messages and exchange photos, videos, stickers, audio, and files of...

Home Automation System with Raspberry Pi and Flask

Home Automation systems are becoming increasingly popular for the level of convenience they offer. Imagine sitting on your couch and turning on/off lights or fans in the room without having to get up. You could also control blinds or even door locks. This project is a...

Smart Alarm Clock with Raspberry Pi

Smart Alarm Clock with Raspberry Pi

Smart Alarms are very common today yet the good ones expensive. What if you wanted to make a smart alarm clock on your own using as little hardware and cost as possible? You may ask isn't the RPI costly, yes it is but, the only purpose of using an RPI is easy to net...

Spy Bot with Raspberry Pi

Spy Bot with Raspberry Pi

Spy bot is a raspberry pi bot that uses its camera to take secret pictures, encrypt them using RSA encryption and upload it safely to a database/server. Today, security and surveillance have become very important. To take inspiration from world scenario we start off...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....