Introduction: Color Detection Based Object Tracking
I did this project to learn image processing using Raspberry PI and open CV. To make this project more interesting i used two SG90 Servo motors and mount camera on it. One motor used to move horizontally and second motor used to move vertically.
Color detection based Object Tracking
1. Install Raspbian Strech on Raspberry 3B+
a. Checkout my YouTube video from :- to 15:10 to 16:42 ->> https://youtu.be/XfPXu4UAZwQ
b. Use HDMI-VGA converter to connect RPI with monitor & USB Keyboard & mouse.
c. Boot RPI desktop and follow next step.
d. Recommend to use monitor if your are beginner as direct access of PI is easy for newbie.
2. Install Open CV on RPI3B+
b. Time Taken :- Approx 8+ Hours
c. I spend two days for to complete this process (20 Hours) so be passionate & calm.
3. Install PCM9685 library on Raspberry PI.
a. Reference Documentation:- https://learn.adafruit.com/adafruit-16-channel-servo-driver-with-raspberry-pi?view=all
b. Check PCM9685 i2c connectivity withRPI
i. Run :- sudo apt-get install python-smbus
ii. Run :- sudo apt-get install i2c-tools
iii. Run :- sudo i2cdetect -y 1
1. Image of successful communication with PCM9685
c. Open Terminal & run: - source ~/.profile #to enter in virtual environment.
d. Open Terminal & run:- pip3 install adafruit-circuitpython-servokit
e. Never Use “sudo” otherwise you will face problem as using “sudo” will not install library in your virtual environment.
f. Checking servo
i. Open python3 & enter below commands.
ii. from adafruit_servokit import ServoKit
iii. kit = ServoKit(channels=16)
iv. kit.servo.angle = 90
v. kit.servo.angle = 180
vi. kit.servo.angle = 0
4. Connection Detail:-
a. Connect 5VDC to PCM9685 (For Servo operation External 5V required)
b. \Connect PC9685 I2C & logic supply pins with RPI pins.
c. Connect Two Servo to PCM9685
5. Checking of servo
a. I have prepared 4 files for servo checking (180.py, 90.py, 0.py).
i. For 0 degree. (Both Servo at 0 degree).
ii. For 90 degree. (Both Servo at 90 degree).
iii. For 180 degree. (Both Servo at 180 degree).
iv. Source code ()
6. Install PI camera on Camera connector & mount servo as explain in tutorial video.
a. Tutorial URL:- https://www.raspberrypi.org/documentation/usage/camera/README.md
7. Run object tracking code (download from :- )
8. Open Terminal
a. Run: - source ~/.profile.
b. Run: - workon cv.
c. Check “(CV)” in front of terminal command.
d. Run Object tracking Code :- ‘path of your file location’/python3.’file name’
e. To exit press :- Esc
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Working of Project:-
- Image captured by RPI Camera and processed in python using openCV.
- Captured image is converted from RGB to HSV.
- Apply mask for Specific color (in my final code i used RED color & special code is used to find accurate masking value which is also attached hearwith).
- Finding Contours for all red objects in frame.
- Finally sorting and selecting first Contours will give bogest red object in frame.
- Draw rectangle on object and find horizontal & vertical centre of rectangle.
- Check difference between Frame horizontal centre and object rectangel horizontal centre.
- If difference is greater then set value then start moving Horizontal servo to minimize difference.
- Same way we can move vertical axis and finally object tracking is working for 180 Degree.
Step 2: Prepare RPI :- Setup Guide Time Is :- 15:10 to 16:42 in Youtube Video.
Download Raspbian Streach and flesh it on 32 GB memory card.
After download Raspbian image, unzip it and store on desktop (or suitable place).
To write image on SD card, download Eatcher.
Connect USB Keyboard and Mouse and power up PI using power adaptor(2.5 amp.)
Check out youtube tutorial video from time :- 15:10 to 16:42 for Raspberry pi setup process.
Step 3: Install Open CV on PI.
This is most tedious job for me.
I spend 2 full night for this so dont get penic during this setup and get cup of coffee or beer.
I have followed below mentioned written guide for Open CV installation & suggest you to follw the same as it is very easy and very vell written.
Step 4: Wiring Diagram
Raspberry PI and PCM9685 connection detail
Raspberry PI Pin 1(3.3V) >> PCM9685 Pin VCC
Raspberry PI Pin 3(SDA) >> PCM9685 Pin SDA
Raspberry PI Pin 1(SCL) >> PCM9685 Pin SCL
Raspberry PI Pin 1(Ground) >> PCM9685 Pin Ground
V+ and OE pin of PCM9685 is not used.
Servo motor connection
- Servo 01 is connected to Channel0 of PCM9685
- This servo is used for vertical axis control
- Servo 02 is connected to Channel1 of PCM9685
- This servo is used for Horizontal axis control. and camera is mounted on this camera.
Step 5: Checking PI to PCM9685 I2c Interfacing.
Open terminal & enter below commands to install I2C testing utility.
sudo apt-get install python-smbus this will install requird packages on pi.
sudo apt-get install i2c-tools this will install requird packages on pi.
sudo i2cdetect -y 1 this command will check I2C bus & respond with successfully connected slave device address.
Step 6: Install Adafruit-circuitpython-servokit Library
adafruit-circuitpython-servokit Library used to control SG90 Servo motors.
As OpenCV library installed on Virtual environment, we have to install servo control library in same Virtual environment othervise it will not work with OpenCV.
Open Terminal & enter source ~/.profile and press enter. by this we entered in to virtual environment.
Enter pip3 install adafruit-circuitpython-servokit and press enter.Do not use SUDO before pip3.
Step 7: Check Servo Motors.
- Connect all devices as per below mentioned wiring diagram.
- Open python3 & enter below commands.
- from adafruit_servokit import ServoKit # this will Import library
- kit = ServoKit(channels=16) #Defining variable object "kit"kit.
- kit.servo.angle = 90 Set 1st motor position at 90 Degree.
- kit.servo.angle = 90 Set 2st motor position at 90 Degree.
- By changing value "90" we can move servo motor postion from 0 to 180.
- Set both motor positoin to 90 and disconnect motor as 90 is center positon of full range.
Step 8: Mounting of Servo and Camera.
Set both moto position at 90 deg.
Mount both servo and mount camera on Second servo.
Step 9: Checking Camera.
Insert camera belt cable in to PI camera connector. ensure belt direction. Try to keep blue tape side part of cable at Black locking clip side of camera connector.
Open terminal and enter Sudo raspi-config & press enter. this will open raspberry pi configuration window.
Select 5th option which is "Interfacing Option" & then select 1st opetion which is camera and enabe camera.
Open termianl and enter belwo command
raspistill -o cam.jpg This will capture image named "cam" & save it as .jpg format.
Captured file will save in /home/pi directory & image is upside down.
Referance Document:- https://www.raspberrypi.org/documentation/usage/camera/README.md.
Step 10: Checking Final Code
Now we can run final code which can track biggest Red object in frame. (Final code is Red_Track01.py) which i have uploded.
Befor run the code switch in to virtual environment by applying below mentioned command
Sourec ~/.profile and enter.
next command is workon CV and press enter.
to run final code :- "path of your file location"/python3 Red_Track01.py
Code execution will stop by pressing Esc key.
Checkign of Servo
0.py, 90.py & 180.py is python code for cheking Servo positon.
1 Person Made This Project!
- ariefn10 made it!