A Raspberry Pi Vehicle (object) Speed Camera Demo
using a Raspberry Pi computer, picamera module, python and openCV
written by Claude Pageau email@example.com
6-Feb-2016 upgraded to release .99 see release notes here viewtopic.php?p=813535#p813535
NEW I have a newer Faster version of my Object Speed Tracking python script that uses threading for streaming
https://github.com/pageauc/motion-track ... ed-track-2
Let me know what you think. Note this is still just a demo
YouTube Demo and code walk through here https://youtu.be/eRi50BbJUro
GitHub Repo here https://github.com/pageauc/motion-track ... peed-track
Here is a previous YouTube video demonstrating a motion tracking test program using a Raspberry Pi B2.
this was the rough starting point for speed_track.py https://youtu.be/09JS7twPBsQ
This is a raspberry pi computer openCV vehicle speed camera demo program. It is written in python and uses openCV2 to detect and track object motion. The results are recorded on speed photos and in a CSV log file that can be imported to another program for additional processing.
The program will detect and track motion in the field of view and use openCV to calculate the largest contour and return its x,y coordinate. Motion detection is restricted between y_upper and y_lower variables (road area). If a track is longer than track_len_trig variable then average speed will be calculated (based on IMAGE_VIEW_FT variable) and a speed photo will be taken and saved in an images folder. If log_data_to_file=True then a speed_track.log file will be created/updated with event data stored in CSV (Comma Separated Values) format.
Some of this code is based on a YouTube tutorial by Kyle Hounslow using C here https://www.youtube.com/watch?v=X6rPdRZzgjg
Requires a Raspberry Pi computer with a RPI camera module installed, configured and tested to verify it is working. I used a RPI model B2 but a B+ or earlier should work OK.
Login via SSH or use a desktop terminal session and perform the following code block commands to install the required dependencies and program files.
Code: Select all
sudo apt-get update sudo apt-get upgrade sudo apt-get install -y python-opencv python-picamera python-image python-pyexiv2 cd ~ mkdir speed-track cd speed-track wget https://raw.github.com/pageauc/motion-track/master/speed-track/speed_track.py wget https://raw.github.com/pageauc/motion-track/master/speed-track/speed_settings.py wget https://raw.github.com/pageauc/motion-track/master/speed-track/speed_track.md chmod +x speed_track.py python ./speed_track.py
Code: Select all
cd ~ git clone https://github.com/pageauc/motion-track.git mv motion-track/speed-track ./
this folder if you wish per the mv command otherwise exclude the last command.
Note an images folder will be created to store jpg speed photos. There is an image_path variable in the speed-settings.py file. Use nano editor to change variables in this file as desired. It will default to create a subfolder in the current folder that speed_track.py is launched from.
Use the calibrate option and follow instructions below to calculate an accurate value for IMAGE_VIEW_FT variable in the speed_settings.py
Calibrate IMG_VIEW_FT variable
speed_track.py needs to be calibrated in order to display a correct speed (mph or kph). Once calibrated the size of the vehicle does not matter since this calibration only sets the pixel per feet conversion factor.
- - Setup the RPI camera to point to the view to be monitored.
- - Login to RPI using SSH or desktop terminal session and cd to speed-track folder
- - Use nano to edit speed_settings.py. Edit variable calibrate=True ctl-x y to save
- - Start speed_track.py eg python ./speed_track.py
- - Calibration images will be generated automatically when a motion track is complete. Files will be placed in images folder with a calib- prefix.
- - Follow the on screen instructions for calibrating an accurate value for the IMAGE_VIEW_FT. You will need to measure appropriate vehicle lengths in feet.
- - Adjust the y_upper and y_lower variables to cover the road area. Note image 0,0 is the top left hand corner and values are in pixels. Do not exceed the CAMERA_HEIGHT default 240 value
- - Use an image viewer program to view the calib- files and use hash mark to record pixels for vehicle length Note each division is 10 pixels. I use filezilla to transfer files to/from my PC and the RPI using sftp protocol and the RPI IP address.
- - Use formula below to calculate a value for IMG_VIEW_FT variable
- - You should take several photos to confirm and average results.
- - Use nano to edit the speed_settings.py and change IMG_VIEW_FT variable value to new calculated value. Also change variable calibrate = False
- - Restart speed_track.py and monitor console messages. Perform a test using a vehicle at a known speed to verify calibration.
- - Make sure y_upper and y_lower variables are correctly set for the area to monitored. This will restrict motion detection to area between these variable values. Make sure top of vehicles is included.
Use this formula to calculate a value for IMG_VIEW_FT
IMG_VIEW_FT = (CAMERA_WIDTH * Ref_Obj_ft) / num_px_for_Ref_Object
eg (320 * 18) / 80 = 72
Variable values are stored in the speed_settings.py file and are imported when speed_track.py is run. Use the nano editor to modify these settings per the comments. Most settings should be OK and need not need to be changed. Others may need to be fine tuned depending on the distance from the camera to the road to be monitored. The openCV settings most likely won't need to be changed unless you are familiar with them.
Claude Pageau ....
YouTube Channel https://www.youtube.com/user/pageaucp
GitHub Repo https://github.com/pageauc