Skip to content
using OpenCV to count vehicles
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
rspi
README.md Update README.md Oct 22, 2016
flow.avi Add files via upload Aug 2, 2016
flow.mp4
fps.py detect the frame rate of a video from cam Aug 19, 2016
main.py fix for name changing Aug 18, 2016
record.py record the processed video stream Oct 22, 2016
stream_test.py To test the rtsp video stream Aug 2, 2016
vehicle_counter.py Rename vehicle_counter_fei.py to vehicle_counter.py Aug 18, 2016

README.md

Vehicle_Counting

Using OpenCV to detect and count moving vehicles.

Prepare

Install OpenCV for OS X

You definetly could install OpenCV from source, but it will be more complicated. Using homebrew is a seay way to go.

  1. Install homebrew (if you don't have it)
`/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"`

or you could find it [here](http://brew.sh)
  1. Install Python (if you'd like to use system Python, skip this step)
`brew install python`

Open up the `~/.bash_profile` file 

`cd ~`

`open -e .bash_profile`

(if it does not exist, create it use `touch .bash_profile`)

and append the following lines to the file:

`export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH`

after that, use `which python` to detect the path of current Python

if the output is `/usr/local/bin/python` , then you are indeed using the Homebrew version of Python. And if your output

is `/usr/bin/python` , then you are still using the system version of Python.

actually I think it is OK to use system Python.
  1. Install numpy
`brew install numpy`  or

`brew install homebrew/python/numpy`

then link numpy

`brew link numpy` 
  1. Install OpenCV
`brew tap homebrew/science`

`brew install opencv --with-contrib --with-ffmpeg --with-tbb`

it is important to add the option `--with-ffmpeg --with-tbb` for decoding some format of video

Then it's done!(I'm not sure if there are useless steps here, if so I'll update it)

To test the installation:

`python`
  
`>>> import cv2`
  
`>>> cv2.__version__`

it should output `'2.4.13'`

You could slso install OpenCV3 use `brew install opencv3`
  1. If you can't import cv2, try ln -s -f /usr/local/Cellar/opencv/2.4.13/lib/python2.7/site-packages/cv2.so /usr/local/lib/python2.7/site-packages/cv2.so
you may need `sudo`

Install OpenCV for a Raspberry system

  1. Update installed packages
`sudo apt-get update`

`sudo apt-get upgrade`

`sudo rpi-update`
  1. Install OpenCV
`sudo apt-get install libopencv-dev python-opencv`

If you want the latest version of OpenCV you should make it from source. (but I'm lazy :)

To test the installation:

`python`

`>>> import cv2`

`>>> cv2.__version__`

Main steps of the image processing:

1.Read the video frame by frame.

2.Apply some fileters to the frame(dilation, etc.).

3.Use BackgroundSubtractor to split the foreground from background(white-foreground, black-background).

4.Detect the contours of the foreground(moving objects).

5.Calculate the centroid of each moving object.

6.For each centroid, detect if there's a nearby centroid of the last frame. If so, assign them to the same vehicle.

7.For each vehicle, detect whether it crossed the target line.

Simply run main.py.

...

Camshift is not good for tracking vehicle due to the sunlight is too strong at day time.

Increase the performace:

http://www.pyimagesearch.com/2015/12/21/increasing-webcam-fps-with-python-and-opencv/

Problems:

1. May apply other image filters to increase the accuracy
2. To enhance the performance on pi: resize the size of frames, use multiprocessing...
3. Multithread is not working on Python(espicially CPU bound program), should use multiprocessing to get advantage of ulticore.
4. I'll update how to switch between OopeCV 2 and 3 later

The accuracy of the near lane is around 95 - 100 %, the further lane's accuracy is lower due to the shelter effect.

Usefull links: https://www.learnopencv.com/how-to-find-frame-rate-or-frames-per-second-fps-in-opencv-python-cpp/

You can’t perform that action at this time.