Today, one of my colleagues located a nearby traffic camera that monitored a particularly busy road near where we work. This road can make the journey home a real pain in the ass, and it is much easier to just stay in the office and work later until it calms down a bit.
Here is a sample image from the camera:
There is a lot of content in this image that is not relevant to the amount of traffic in it. A reasonable first task is to extract the important section of road from the background. The Python library Pillow makes this very easy.
The only fiddly bit is finding the coordinates of the polygon that contains the important section of road.
The next task is to somehow calculate how much traffic there is in this image. Counting cars is beyond the skill level of this author, but counting pixels is the kind of nasty hack that is right up his street (get it?).
OpenCV provides an excellent edge detection algorithm that outputs a monochrome image with white pixels for identified edges and black pixels otherwise.
A useful metric for traffic would simply be the ratio of white pixels to black pixels.
A slightly better metric would be the ratio of white pixels to pixels that could have been white, remember that some of the pixels from the image were black already from the polygon mask.
Running this algorithm on the camera near my work gives reasonable results for just a few hours of experimentation.
One of the weaknesses of this algorithm is that more edges will be detected in direct sunlight because of the increased contrast. A solution to this would be to use tensorflow or a more intelligent form of processing to actually count the vehicles.