Object Detection and Counting
Object detection, especially recognition can be done using different technics, like a combination of OpenCV functions. For me, it was rather interesting to build a quick model in R than to spend weeks writing long C++ or .NET code for it. I started with a people counter as a practical application for object detection and took footage of people passing by the office.
The first thing needed is to prepare images from video using FFmpeg. Then choose a background image and create a matrix of difference between an image with an object on it and the background one. As it can be found on my blog I have created an R library for raster image processing and vectorization – fasteraster, this can be used for object detection, thus the idea was to vectorize the matrix of differences by some gradient-detected zones.
On this picture, there is a variant #1, where the matrix of pictures was represented by simple RGB values. Their comparison gave me a strong object detection of the person’s shadow (see in the course code):
Thus next idea (variant #2 in code) was to calc differentials between colors (Red / Green, Green / Blue) and then compare to the background. This cleaned shadow detection but introduced another issue with a lot of detection of dark areas that probably caused by poor CMOS-camera color detection capabilities:
Then I decided to subtract the colors (Red – Green, Green – Blue) and it worked just fine. I also added filtering for detected zones weight and shown these on the video. As you can see, there is another problem when a black object moves through the black background – it is being split into two or three parts:
In this case, I just added code to join the areas and calculate the new center of the joined object. Added track line and two green margins to detect that object passed both in the same direction:
As one can see the model itself took a page of code, most of it was for the visualization. However following items was not included in the model:
- background image – it has to adjust to the weather, daytime and other conditions (like somebody left a bag in the middle of the observation area).
- count the objects – simply check the vectors crossed the green margins
- multiple objects detection – needs identification algorithm based on the path approximation.
- joined objects recognition – needs clusterization of shape medians to split the joined area into smaller ones by average weight and path approximation.
Object detection R source code
Author:
Andy Bosyi, CEO/Lead Data Scientist MindCraft
Information Technology & Data Science
Also Read: Vectorization of Raster to Polygons