How busy is the road outside?
We are counting passing vehicles with automation
DOING DATA SCIENCE FROM SCRATCH TASK BY TASK
My project to count passing traffic would be a lot easier if I had terrific photographs such as the one above. Instead, I have low quality blurred images to work with. This article is a continuation of my personal journey on a road trip to a Computer Vision & Deep Learning Nirvana. Often times it seems more like "The road to hell" by Chris Rea. The song goes "On your journey across the wilderness from the desert to the well". My wilderness appears to be the field of Computer Science, Deep Learning, and Computer Vision. The desert represents my thirst for knowledge with the well, perhaps meaning the books, tutorials, and articles that I drink from regularly. If that all be so, then I invite you to drink from my well and enjoy the next article in my series. Join me on the quest to count passing traffic.
In case you are joining, mid quest, you can always catch-up on the whole series here. Previously, I build the cameras, deployed Motion Eye, and positioned the cameras to record movement but yet protect the privacy and safety of road users. Finally, I hacked out quite a bit of code. Now I have a full week of data and am keen to update you all on the exercise.
Retrieving the images
Once the data collectors are designed and tuned in, data retrieval is or ought to seem automatic. At least that is how it is for me now at this stage of my project. Figure 1, below, shows a screenshot of Motion Eye OS โ Camera 1. Each day is represented on the left-hand side, and I can retrieve a โZippedโ file of all the images for any given day.
There is a whole set of data for January 22 through January 28. January 29 was in progress at the time of capture. The system works from 7am GMT to 5pm GMT each day. Before 7am, and after 5pm, it is dark, and the device does not record activity. Each day is a separate folder and each image file is named as hour-minute-second.jpg with 10โ49โ24.jpg representing a detection trigger at 10:49:24 am.
Retrieval merely involved logging into the Motion Eye device and downloading each day as a ZIP file. Since Motion Eye is a web-based service, I could log in directly from my Data Science Workstation. I did create a UPS backed Raspberry Pi file server, but we need to keep things simple. With the zip files downloaded and decompressed, in the target folder, our data retrieval is completed. However, downloading several hundred photos is not data! Right? We need to refine the images like an oil company that moves crude to the refineries to generate GAS.
The Data Refinery
Much, I guess, like those wildcatters, out in the wilderness drilling for oil, weโve struck oil here too. Instead of barrels of thick crude oil, we have folders of blurry images to refine. Turning the crude oil into products that powered society turned out to be big money. Data Science, Machine Learning, and Deep Learning, appear to me, to be oil workers moving tools, and crude to the refinery applications of life.
Thankfully messy data is a lot cleaner than a barrel of crude oil. I added my refinery steps to a GIST and let us discuss the main points. Previously, I discussed the myYolo class I created, and I mentioned that the codeโs origin is from www.pyimagesearch.com.
Lines 5 and 6 help describe where those barrels of crude, the folders of images, are stored. Line 14 creates an instance of the myYolo class and, because I configured my workstation with a GPU, we can set the target device to โGPUโ. Lines 16 through 21 provide a helper function, allowing me to persist any given object to disk. Lines 24 through 29 are the actual refinery process. Line 27 calls a setter method on the instance and really only sets up the target folder or crude barrel. Line 28 sends the entire folder forward through the YOLO Network with line 33 spilling out the data to disk.
Figure 2, above, provides you with a screenshot of the terminal output. I am using Visual Code, and you can see that I have a virtual environment called โopencv_cudaโ. Building the OpenCV binary with Cuda was a tremendous amount of pain for me. Each folder has been processed, and we can see that each image took .037 seconds to traverse the Yolo network.
So now we have data, but we are still midstream, we processed hundreds of photos into a data object and now need to deliver a product to an end consumer.
Consuming the data
Did you ever wish that you could plug your brain into a data pipeline and just consume it directly? I used to joke, with younger individuals, by asking if they had a "USB Port" in their brain? If so, they could plug into my brain, downloading and upload my data! Clearly, I watched all the Matrix films.
The raw data contains a list of dictionaries.
[{'image': '/home/david/Documents/projects/yolo-object-detection/images/2021-01-22/13-30-33.jpg', 'result': ['car: 0.8965'], 'timing': '[INFO] YOLO took 0.391402 seconds'}
..
]
Each dictionary has a limited set of keys. Those are:-
- image: providing the original image file and full path;
- result: A python list of the objects detected in the image. There could be one or more objects.
- timing: contains a text which records how long the image took to traverse the YOLO network. Potentially not useful.
For my first time ever, I used Visual Code, as my notebook environment. It is hard to say how I feel about Visual Code versus the Web Browser as it felt weird. Visual Code is potentially a better notebook environment. Figure 3 shows a great feature โ a variable viewer.
I exported the notebook as PDF and added that to GITHUB. Most of the code is really standard and only serves to provide a couple of CSV files that I can work further on. Figure 4 shows the data. Figure 4(left) shows an hourly count of passing traffic. Figure 4(right) displays the individual events, and objects detected. To make some charts I decided to use Power BI
Visuals with Power BI
Using Power BI, I did some further manipulations and then generated some charts. Figures 5 and 6 are included below, demonstrating some of the images I created.
The Yolo model didnโt detect anything in 198 photos (Fig 5). Cars are the most frequent vehicle to pass, but we also had a โtrainโ which is unlikely. The Yolo model certainly needs to be trained to detect local vehicles for IRELAND.
#
Figure 6 gives us an answer to the research question. Given the Global Pandemic, it is unlikely that this data is representative of typical patterns. Starting from 7am and stopping at 17H for 7 days, we see the average passing vehicles. The road is quiet at 7am. Generally, during the day there are at least 12 vehicles per hour passing on the street outside.
Is the road outside busy? I guess some roads have 12 cars per second, so it really comes down to perspective. I will continue my personal journey on the road trip to that Computer Vision & Deep Learning Nirvana I seek. More often than not, it seems more like "The road to hell" by Chris Rea. We will see!
Share This Article
Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.
Write for TDS