Blog Post

Counting cars with Azure’s Computer Vision service

,

When I was 11 I got my first computer, an 8088 with 4.5 mhz. I didn’t know much about computers at the time but I knew it was cool. It took me a while to figure out that there was a turbo mode to take that puppy up to 8.5 mhz. Dialing up to BBS hosts with 2400 baud… the nostalgia is in full effect. That’s right. The internet we know today wasn’t even a thing yet.

Back then the idea that computers could be used to identify objects was just an idea for the movies. Today is a different story. Cognitive services do what was the unthinkable of which identifying people, brands, and other objects, classifying videos, predicting outcomes with machine learning are just a few.

Let’s say you have a need to count cars going into and out of a parking garage. With Azure’s Computer Vision service this task is made easy.

What you’ll need

  • An Azure subscription
  • Computer vision service
  • Simple understanding of python
  • Visual Studio Code
  • An image of a vehicle

The code

from azure.cognitiveservices.vision.computervision import ComputerVisionClient

from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes

from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes

from msrest.authentication import CognitiveServicesCredentials

from array import array

import os

from PIL import Image

import sys

import time

subscription_key = “YOUR KEY HERE”

endpoint = “https://YOURSERVICEHERE.cognitiveservices.azure.com/”

computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))

#print(“===== Setup =====”)

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# check if object intersects line

def doOverlap(l1, r1, l2, r2):

if((l1.x >= r2.x or l2.x >= r1.x) or (r1.y <= l2.y or r2.y <= l1.y)):

return False

return True

#print(“===== Read File – remote =====”)

remote_image_url = “https://images.squarespace-cdn.com/content/v1/59e53e59fe54efe0c0aa77a1/1509049359179-SM0VROIMGTI9H1IYNHF5/GateLot-Car-Entering.png?format=1500w&#8221;

#print(“===== Detect Objects – remote =====”)

detect_objects_results_remote = computervision_client.detect_objects(remote_image_url)

#lines for vehicles driving in/out.

EnterLine = ((813, 460), (1159,460))

ExitLine = ((25, 592), (358, 592))

if len(detect_objects_results_remote.objects) == 0:

print(“No objects detected.”)

else:

for object in detect_objects_results_remote.objects:

if object.object_property == “car” or object.parent.object_property == “car”:

rect = ((object.rectangle.x, object.rectangle.y), (object.rectangle.x + object.rectangle.w, object.rectangle.y + object.rectangle.h))

EnterPoint1 = Point(EnterLine[0][0], EnterLine[0][1])

EnterPoint2 = Point(EnterLine[1][0], EnterLine[1][1])

ExitPoint1 = Point(ExitLine[0][0], ExitLine[0][1])

ExitPoint2 = Point(ExitLine[1][0], ExitLine[1][1])

VehiclePoint1 = Point(rect[0][0], rect[0][1])

VehiclePoint2 = Point(rect[1][0], rect[1][1])           

if(doOverlap(EnterPoint1, EnterPoint2, VehiclePoint1, VehiclePoint2)):

print(object.object_property + ” Entering”)

elif(doOverlap(ExitPoint1, ExitPoint2, VehiclePoint1, VehiclePoint2)):

print(object.object_property + ” Exiting”)

else:

print(“Nothing to report”)

WordPress doesn’t do a great job with formatting code but the images at the end of the post may help with readability.

The objective of this code is to count cars coming and going from a parking garage. Using the sample image I found searching Bing.com, we see an SUV entering a garage, but how can we define that the vehicle is entering?

To do this, I’ve added some code to draw a line named EnterLine and another named ExitLine.

Then we detect the vehicle using the comupter vision service by calling detect_objects and checking the object_property for each object to validate if the object is a “car”.

It’s really important to know that this code is using the default model, this means that partial cars may not be picked up as well as if you had a custom trained model. For instance, If you have two cars in line to go in and one is partially blocking the other, would the vision service pick up both, one, or none? Give it a try and find out! Smile

computervision_client.detect_objects(remote_image_url)

Finally, we run the doOverlap function we wrote above to see if any of the objects overlap the lines we drew and print the results.

# check if object intersects line

def doOverlap(l1, r1, l2, r2):

if((l1.x >= r2.x or l2.x >= r1.x) or (r1.y <= l2.y or r2.y <= l1.y)):

return False

return True

code

result

Original post (opens in new tab)

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating