Building a Car Counter with Python and OpenCV
This is a beginner computer vision project. It is the accompanying code for videos that can be found here:
This project is intended as a demonstration, and there will be no support.
You need to install the following.
- Python - Installation
- OpenCV, Matplotlib
pip install opencv-python
pip install matplotlib
- Detectron2 - Installation
- This repository!
git clone https://github.com/eric-yim/building-a-car-counter.git
The object detection mechanism requires a GPU set up with appropriate drivers. If you don't have one set up, you can use google colab.
Then, instead of running the code on a webcam, you would instead run the code on a video file. You'll have to use colabs built in visualizer instead of cv2.imshow.
The main entry to the code is view_camera.py
. There is a variable in the file called NAME_OF_CAMERA
that you should edit. This can be the name of a video file like MY_FILE.mp4
or a webcam (be sure to put your video file in repository folder or use the absolute path). Your webcam maybe called something like /dev/video0
on Linux or "0"
on Windows. Once that is set, you should be able to run the file.
This opens the camera, runs the system, and shows a visualization on screen. Meanwhile, it appends counts to a file called totals.csv
. Press "q" to exit.
python view_camera.py
You can also run only the Object Detector on the sample image provided.
python detector.py
You should get an image like this.
There are sample counts stored in totals.csv
. You can display them.
python analyze.py
You will get a display like this.
- aggregator.py - measures time increments for outputting counts
- analyze.py - displays totals.csv using matplotlib
- colors.py - a list of colors for visualizing tracked objects
- counter.py - checks line crossing and keeps a count of tracked objects
- detector.py - runs object detection on image
- tracker.py - tracks objects
- view_camera.py - main entry file to system
sample.png
and totals.csv
are samples and can be deleted once you have your own system running.