Ever tried counting "multiple" fast moving objects? Well, it's very difficult and chances are high, that you might lose count. This project will enable you to keep a track of such moving objects and maintain their count, in this case Cars !
Problem statementThe problem with many detection models are that they are either too slow to be used in real time or are not able to track fast moving objects with a good accuracy. One alternative for this, is to apply detection model after every "n frames" and then use any tracking algorithm on majority of the frames as detections are costlier (time consuming) than tracking. But the problem is that such methods are less accurate as tracking algorithms easily can lose track of our actual target object and end up tracking unwanted objects, hence making it impractical and unreliable to be used for applications that require quick inference. This project addresses the problem by using OpenVINO Toolkit for detecting and tracking fast moving cars in real-time (even without GPU) and uses a centroid-tracking algorithm for maintaining a distinct count of the cars detected, all with high accuracy!
InstallationBefore installing OpenVINO, we need to make sure we have our prerequisites met. The list are as follows:
- 6th to 10th generation Intel® Core™ processors and Intel® Xeon® processors
- Python (between 3.5 to 3.7)
- Microsoft Visual Studio C++ 2017 or 2019 with MSBuild
- CMake 2.8.12 or higher
Once these are installed you can install OpenVINO. For more instructions, refer this.
Downloading the IR modelsIn this project we will be using car detection IR model (Intermediate Representation) for real time detecting and tracking of cars. For that, we will be downloading vehicle-detection-adas-0002 model, which is a pre-trained model from Intel OpenVINO.
To download it, first you need initialize the OpenVINO environment by going to <OpenVINO_Installation_PATH>/bin
. Open your command prompt and run setupvars.bat file.
If you get some unexpected error, check whether you are in the correct folder or make sure you have set up everything as per this documentation.
Then, change your directory in the command prompt to <OpenVINO
Installation_PATH>/deployment_tools\tools\model_downloader
using cd command. You should get something like this:
Then run command downloader.py --name vehicle-detection-adas-0002 --precisions FP32
-o
<your_project_folder_PATH>
.
After downloading the model, one can switch to the project directory and run the program as followed
python cars_count.py -v test_video.mp4 -b ./FP32/vehicle-detection-adas-0002.bin -x ./FP32/vehicle-detection-adas-0002.xml
Here, in the program we have three arguments:
- -v : Input video
- -b : The.bin file which contains weights and biases
- -x : The.xml file which contains the network topology
In our code, first we will initialize our centroid-tracking algorithm object ct
. This object will contain unique identity of our detected objects and their centroid location. We will then read our pre-trained intel model by using cv2.dnn.readNet()
function and store it in an object net
. This will be then used for performing detections on the input video frames.
Before feeding our input frames to our detection object, we need to make sure that each frame has the dimensions our model expects, which in this case is [1x3x384x672] where 1 is the batch size, 3 are the number of channels, 384 is the height and 672 is the width. We will convert our input video frame into required format using cv2.dnn.blobFromImage()
function.
We can then perform our detection on "each" frame and get location of the bounding boxes of all the detections and store it in a list rect
, which will be used by the update
method of ct
one by one. It will then calculate their centroids & compare the position of the detected objects to their centroid location in the previous frame and ascertain if it's the 'same car' or a 'different car', by comparing it to a maximum threshold maxDistance
. We also have another threshold called maxDisappeared
, which stores the maximum consecutive frames, a car is allowed to be marked as disappeared. The maxDisappeared
is set to 8 frames and maxDistance
is 50 pixels.
The below video will show you a working demo of the project:
PerformanceThe vehicle-detection-adas-0002 model used has an AP of 90.6%. The FPS calculated i.e. frame/seconds was found to be around 30 FPS (again without any GPU), which is excellent for a real-time application.
Using MobileNet-SSD model gives a frame rate of around 15 FPS, hence OpenVINO implementation gives 100% better performance.
It should be noted that the above mentioned MobileNet-SSD model was trained to detect 20 other objects as well, apart from cars which could have contributed to a lesser frame rate. But even if we assume that, still it was not able to accurately identify cars compared to vehicle-detection-adas-0002 model as shown below.
This project can be used to capture data related to traffic in real time which can be used for further analysis to get actionable insights for traffic management. It can also be used by drones for aerial monitoring of vehicles for security.
Comments