Story
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.
Full Writup
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+
a. https://www.pyimagesearch.com/2017/09/04/raspbian-stretch-install-opencv-3-python-on-your-raspberry-pi/
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[0].angle = 90
v. kit.servo[0].angle = 180
vi. kit.servo[0].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
Download Raspbian Streach and flesh it on 32 GB memory card.
URL:-https://downloads.raspberrypi.org/raspbian_full_latest.
After download Raspbian image, unzip it and store on desktop (or suitable place).
To write image on SD card, download Eatcher.
URL: https://www.balena.io/etcher/
Connect HDMI to VGA cable in to RPI and LCD monitor.
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.
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.
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.
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.
Reference Document:- https://learn.adafruit.com/adafruit-16-channel-servo-driver-with-raspberry-pi?view=all
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.
Reference Document:- https://learn.adafruit.com/adafruit-16-channel-servo-driver-with-raspberry-pi?view=all
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.
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.
0.py, 90.py & 180.py is python code for cheking Servo positon.