If you want to find the number of vehicles crossing a street, you need to track the vehicles and count them. You need to track each vehicle in the video because you need to count a particular vehicle only once and not every time while it appears in different frames of the road. While getting tracked, if a particular vehicle crosses the line, we increase the counter variable storing the number of vehicles crossing the street by one.
It is highly recommended for you to download the notebook below and follow its code along with the lectures to understand the session better.
Note: At the start of the video, Anand talks about the Blob() class. This class stores a vehicle’s outline along with some other properties like area, centre, aspect ratio, etc. We will discuss Blob() in detail later in the session.
Now, let’s look at the code that will allow us to draw a line on top of the video.
Drawing the line is implemented using myMouseHandler() function and OpenCV cv2. setMouseCallback() function. cv2.setMouseCallback() monitors the activities of the mouse such as the time when the mouse button is clicked or released, mouse coordinates, etc. Two coordinates, (x1,y1) and (x2,y2) are needed to draw the line which is implemented using myMouseHandler() function. It is divided into 3 parts using 3 if conditions:
- Click the left mouse button This will the starting coordinate (x1,y1).
- Drag the mouse without releasing the left mouse button and draw the line.
- Release the left mouse button. This will set the end coordinate (x2,y2).
Now, let’s proceed with the lecture.
Now, you saw how to draw the line using myMouseHandler() function.
One of the main difference between imshow() function in OpenCV and matplotlib library is that OpenCV will pop up a window while displaying the image while matplotlib can display in the notebook itself. So, in OpenCV, we need to give the name of the pop-up window, which is the first argument of the imshow() and the second argument is the image itself.
Now that the line has been drawn, the next steps should be pre-processing the frame, extracting vehicles from the frame, tracking, counting and classifying them. In the next segment, you will see how to read frames from video and pre-process each of them.
Report an error