Building a Car Counter with Python and OpenCV: A Beginner's Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
a computer vision project for beginners in this video I'm going to show you how to build a car counter using Python and object detection AI I set up a camera outside my bedroom window this tree going up leads to the freeway I start a new file I call it View Camera and I'm going to write python code import CV2 I'm going to use opencv to read the camera and display it on screen I'm going to have this while loop which says Hey read this camera forever unless you don't have an image or unless I press the letter Q to exit the program alright it's working as we build each piece incrementally we'll want to visually confirm each piece here I make another file called detector this will be for my AI object detection I take a still image from my camera because I'm going to use it for my detector I'm going to use this AI model called detectron 2 it's made by Facebook every AI model has a little bit different syntax if they happen to have this thing called a notebook we can go in there and find some of the code samples I copy the code over and I run detectron on my still image let's see it thinks this is a train here it thinks this is a TV but look it got most of the cars and we can filter out some of the noise later let's stop for a moment what this file does exactly is it loads up that one still image runs that image through the detectron visualize it and displays it on screen now our main file is already going to be reading and displaying an image so the only part of this new detector file that we want to take out is that running the image through the detector so let's refactor the code I'm going to make this a class called detector which I will later import into my main camera reading file yeah look at this now see how the detector is flickering all these different colors on the objects that's because we're not keeping track of each object across the different frames we need to build a tracking mechanism to do this I a simple way to track is to compare the boxes from one frame to the next frame and if the boxes are overlapping we'll say that's the same car there are other ways to track even opencv has tracking algorithms built in but let's start with the simple thing first and see if it works alright my next problem right now the Textron is just drawing all the detections on screen for me but I want access to the boxes so I can do my own calculations how we're going to get those boxes well we need to understand more of the Syntax for detectron we can either go through the documentation of detectron or the code of detect run or we can just do this hacky guess and check usually when we run this file the python interpreter is going to go through the whole file all at once we can use this IPython trick to interrupt the code and now we can play with the variables at this point in the code I'm going to print out this variable outputs it's a whole big blob but I bet my boxes are somewhere in there I use this dur command to show me some of the methods that this outputs has and get Fields that's probably what I want just guessing based on the name the fields is like a python dictionary here we have bread boxes these are the boxes for each of the objects on screen scores how confident the AI is in each box detection print classes this is like what type of object is on screen two is probably cars because we have lots of twos and 62 and 6 probably TV and train prad mask is probably used for coloring in those objects on screen out of all this we want the bread boxes right the boxes for the tracking and let's use this pred classes as well I can tell the program only give me the object if the object class is two i visualize the Box sunscreen to make sure that these spread boxes are indeed the boxes we expected most of these boxes pretty accurately contain the car but there's some cases of double or triple boxing but we can filter based on the size of the box and get rid of some of that noise I make a new file and a class called tracker which I can then import into my main file later here's where I use chat GPT for each of the boxes I basically have a top left corner and a bottom right corner I also happen to know that there's this commonly calculated thing in computer vision called intersection over Union I copied Chad tpt's code into my tracker class for each tracked object I can give it its own color now we can see that it's working now to count the cars I draw a line on screen why because I don't care about all the cars I just care about the cars going across this line up towards the freeway so I'm going to write a function imagine that I have two line segments one is for this line that I just drew and one is from the tracked car and I want the function to return true if the lines cross and false if the lines don't here's where we can use chat DPT again and I need a way to validate whether my program is working so I'm going to turn the cards black whenever they've crossed now I'm counting the cars you can either cross going up or you can cross going down I use chat GPT again to display the counts on screen again I'm trying to visually check when a car has crossed either up or down do the counts reflect that now we can collect data eventually we'll want to visualize that data so I'm going to Output the counts every 30 seconds it's basically outputting the timestamp along with up and down counts I use a python Library called matplotlib to visualize the data now we can see the total number of cars in each Direction up until the point where I turned off the system early in the day there's not too many cars 850 is when there are the most cars on the street this data can be used by the city for infrastructure planning or for construction scheduling I made the code available for you there's a link in the description
Info
Channel: Engineering AI
Views: 1,383
Rating: undefined out of 5
Keywords:
Id: Lo4g5Rbqmrs
Channel Id: undefined
Length: 5min 35sec (335 seconds)
Published: Fri Apr 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.