Laboratory 7: Counting cars

Introduction

All of the programs you have worked on so far have processed individual images but OpenCV is also able to process videos. Here, you will review (but not modify) a program which processes videos of traffic to detect cars and then count them. This laboratory also helps you gain an important skill that developers and researchers need to have: understand what someone else's code does. You may find this more difficult than you expect!

Counting cars

The zip-file for this experiment contains a program, carcounter.py, a video clip, highway.mp4, and an associated configuration file, highway.cfg. As ever, the configuration file is textual and you are encouraged to review it as you figure out how the program works.

To run the program, use the command:

python carcounter.py highway.mp4

You will see that some red regions are marked on the output --- you should examine the program to identify what these represent. Vehicles detected are outlined in green as the program runs and the total car count appears in the top-left corner of the display.

How does the program work?

Try to figure out how carcounter.py works. You should ask yourself questions such as

Then take a look at commented.py, the same program but with comments. How much easier was the commented version to understand?

You might reflect on how easy it is for staff to mark students' submissions to assignments, which are typically more like carcounter.py than commented.py and how good so-called "self-documenting" code is in general. Feel free to discuss your findings with a demonstrator.

All the laboratory scripts


Web page maintained by Adrian F. Clark using Emacs, the One True Editor ;-)