How to Quickly Create and Run a Vehicle Counting Computer Vision App
In this guide, we’ll make an app that can count vehicles in real-time utilizing object counting, a technique used in computer vision that combines object detection and object tracking. Our final computer vision application will tell us how many objects of a specific kind are currently being detected in a video stream.
Our platform at alwaysAI provides a set of open source pre-trained models in our Model Catalog. The following example uses one of the starter models with a simple algorithm in order to achieve our goal. Let's walk through how we'll get there.
We can create our vehicle counting application by building upon the alwaysAI simple_object_counter starter app, which already has a lot of the functionality that we need.
First, install the alwaysAI CLI onto your development computer and then download all of the starter apps:
$ aai get-starter-apps
Now let’s try running the object counting app to see what it produces. If you're using Linux or Windows, you can run the app right on your computer, and if you use MacOS, you can run it on a supported edge device. We’re using MacOS with a Raspberry Pi for this example, so we’ll choose “Remote Edge Device” for the deployment option, and the default settings for the rest. Run the following commands and then click on the http://localhost:5000 link that appears in your terminal (after successfully starting the app) to open the Streamer window:
$ cd alwaysai-starter-apps/simple_object_counter
$ aai app deploy
$ aai app start
Point the camera at something the model has been trained to recognize (you can see this list in the app.py file of the starter app) such as “chair” or “person.” You should be able to see the counter detecting and tracking items in real time, and telling us how many items are currently being viewed by the camera.
Choosing a computer vision model
Although the current model has a few labels that could work for our purposes, we'll search for a computer vision model in the alwaysAI model catalog that has been trained to recognize additional types of vehicles. To do this, we can simply visit the catalog and enter various things that we want our model to recognize in the search box. Let’s type in “truck bus car motorcycle” to see what we get.
The first model that appears is the ssd_mobilenet_v1_coco_2018_01_28 model, and when I click “See more” I can see that the labels match our use case, since it can detect all four types of vehicles that we specified.
Adding the new model
Next, we’ll switch the model that we’re using in our app. To add the new model, run this command in the terminal:
$ aai app models add alwaysai/ssd_mobilenet_v1_coco_2018_01_28
And to remove the old model, run:
$ aai app models remove alwaysai/mobilenet_ssd
The second part of changing the model is to open the app.py file in the simple_object_counter starter app, and paste the new model in the code at the beginning of the function.
def main(): obj_detect = edgeiq.ObjectDetection("alwaysai/mobilenet_ssd") obj_detect.load(engine=edgeiq.Engine.DNN)
def main(): obj_detect = edgeiq.ObjectDetection("alwaysai/ssd_mobilenet_v1_coco_2018_01_28") obj_detect.load(engine=edgeiq.Engine.DNN)
We’ll also change the list of objects to the labels we want our application to recognize.
OBJECTS = ["person", "chair", "sofa", "pottedplant"] def main(): obj_detect = edgeiq.ObjectDetection("alwaysai/mobilenet_ssd") obj_detect.load(engine=edgeiq.Engine.DNN)
OBJECTS = ["car", "truck", "bus", "motorcycle"] def main(): obj_detect = edgeiq.ObjectDetection("alwaysai/mobilenet_ssd") obj_detect.load(engine=edgeiq.Engine.DNN)
After making these changes, we’ll need to set up our Pi camera so that it’s pointing at some vehicles, and then deploy and run the app again to see what we get.
$ aai app deploy
$ aai app start
You should now be able to see our updated app correctly identifying and reporting the number of vehicles in view of the camera.
We’ve walked through a simple example of building upon the alwaysAI object counter starter app by changing the model and the application code. This example application could be repurposed to count many different types of objects, depending on the intended use case.
Creating a working application that incorporates computer vision is easier than ever before with the software provided by alwaysAI, which gives developers access to pre-trained computer vision models along with basic starter applications for various tasks, including object detection, image classification, pose estimation and more — simplifying what was previously an arduous and lengthy process for developers. We can’t wait to see what you build with alwaysAI!