Original Project https://github.com/nicholaskajoh/Vehicle-Counting
Note: The difference between these two projects:
- Current project could count different types of vehicles (YOLO).
- You could specify the position of your counter line:
./counter.py
- How to get the positions of Region of Interest?
By MATLAB:
Original Image:
roipoly:
- TODO: And more trackers: cv2.TrackerBoosting_create(), cv2.TrackerMIL_create(), cv2.TrackerTLD_create(), cv2.TrackerMedianFlow_create(), cv2.TrackerGOTURN_create(), cv2.TrackerMOSSE_create() Tutorial: https://www.learnopencv.com/object-tracking-using-opencv-cpp-python/
- TODO: Integrate tensorflow_object_counting_api(https://github.com/ahmetozlu/tensorflow_object_counting_api)
- TODO: Make Tracker: CamShift work properly. There might be something wrong with this tracker.
- Clone this repo
git@github.com:DarrenRuan/Vehicle-Counting.git
. - Create and/or use a virtual environment.
- Run
pip install -r requirements.txt
to install dependencies.
usage: Vehicle_Counting.py [-h] [--iscam] [--droi DROI] [--showdroi]
[--mcdf MCDF] [--mctf MCTF] [--di DI]
[--detector DETECTOR] [--tracker TRACKER]
[--record] [--headless] [--clposition CLPOSITION]
video
positional arguments:
video relative/absolute path to video or camera input of
traffic scene
optional arguments:
-h, --help show this help message and exit
--iscam specify if video capture is from a camera
--droi DROI specify a detection region of interest (ROI) i.e a set
of vertices that represent the area (polygon) where
you want detections to be made (format:
1,2|3,4|5,6|7,8|9,10 default: 0,0|frame_width,0|frame_
width,frame_height|0,frame_height [i.e the whole video
frame])
--showdroi display/overlay the detection roi on the video
--mcdf MCDF maximum consecutive detection failures i.e number of
detection failures before it's concluded that an
object is no longer in the frame
--mctf MCTF maximum consecutive tracking failures i.e number of
tracking failures before the tracker concludes the
tracked object has left the frame
--di DI detection interval i.e number of frames before
detection is carried out again (in order to find new
vehicles and update the trackers of old ones)
--detector DETECTOR select a model/algorithm to use for vehicle detection
(options: yolo, haarc, bgsub, ssd, tfoda | default:
yolo)
--tracker TRACKER select a model/algorithm to use for vehicle tracking
(options: csrt, kcf, camshift | default: kcf)
--record record video and vehicle count logs
--headless run VCS without UI display
--clposition CLPOSITION
position of counting line (options: top, bottom, left,
right | default: bottom)
- To use the
yolo
detector, download the YOLO v3 weights and place it in the detectors/yolo folder. - To use the
ssd
detector, download this pre-trained model and place it in the detectors/ssd folder. - To use the
tfoda
detector (i.e Tensorflow Object Detection API), copy detectors/tfoda/.env.example to detectors/tfoda/.env and edit as appropriate. You can try out this detector with these pre-trained models.
Use defaults:
python Vehicle_Counting.py "./videos/sample_traffic_scene.mp4"
Custom configuration:
python Vehicle_Counting.py "./videos/sample_traffic_scene.mp4" --droi "750,400|1150,400|1850,700|1850,1050|500,1050" --showdroi --detector "haarc" --tracker "csrt" --di 5 --mctf 15
YOLOv3 + KCF + Bottom Counter Line + Record
python Vehicle_Counting.py "./videos/sample_traffic_scene.mp4" --clposition bottom --detector yolo --record
With camera input:
python Vehicle_Counting.py 1 --iscam
NB: You can press the s
key when the program is running to capture a screenshot. The images are saved in the screenshots folder.
The vehicle counting system is made up of three main components:
- a detector
Input: a frame from a certain video or an image
Output: the locations of bounding boxes and the types of those bounding boxes - a tracker
Input: a new frame
Output: a new bounding box for each object - a counter
Input: centroid and the position of the counting line
Output: whether a bounding box passed the line or not
The detector identifies vehicles in a given frame of video and returns a list of bounding boxes and a list of vehicles types around the vehicles to the tracker. The tracker uses the bounding boxes to track the vehicles in subsequent frames. The detector is also used to update trackers periodically to ensure that they are still tracking the vehicles correctly. The counter draws a counting lines across the road. When a vehicle crosses the line, the vehicle count is incremented.