We’re in a pandemic for approximately 2 years by now and sinc the beginning my couny administration is publishing a map that shows the Covid-19 cases per municipality on a regular basis. So I creted a Home-Assistant component that downloads that map together with some data from their website and saves the images. I had the plan to create a slideshow video every now an then from these images to show the change in infections.
The administration staff are obviously bureaucrats which means that they suck at automating things 😏 They seem to create the above mentioned map by hand or at least in a semi-automatic proccess. The overlay texts are always in the same position, they changed over time every now an then but appart from that they are constant. The map on the other hand is almost always in a different region. That made the map jumping around in the video quite a bit and made the overall look and feel very unsatisfying as you can see in this animation (I reduced the number of frames to get the filesize down)
After discussing this problem with some folks of my hackerspace I decided to go for a OpenCV approach to fix this issue.
First of all I create a working directory with a set of subfolders
|
|
I then copied all of my original images into the original
folder, they all have the same name structure YYYY-MM-DD-Covid19-LK-Waldshut.png
.
Using Gimp I created a image that only contained the map as a search template.
And an overlay that is commletely white with the map area transparent. I use that later on to get rid of the left over text.
Then I started creating the python script to do the work.
I needed to install some dependencies first using pip pip install opencv pillow
.
I’ll go over the steps and show the entire script at the end.
Import the dependencies and figure out the script path 🔗
|
|
Read the template image, convert it to black and white and get its size 🔗
|
|
Iterate over all original images and get the date from the filename 🔗
|
|
Read the original image, create a black and white copy 🔗
|
|
Search for the black and white template in the black and white original 🔗
|
|
Get the best match from the set of serach results 🔗
|
|
Get the coordinates of the search result bounding box 🔗
|
|
Crop the original to the bounding box and save the image 🔗
|
|
Overlay the cropped image with the overlay to get rid of the left over text 🔗
|
|
Write the date in the top left corner and save the final image 🔗
|
|
The full script 🔗
|
|
And last but not least, the resulting animation (reduced frame count) 🔗
There are still a few frames that are blury but thats because they published images with a low resolution on some days. The result is not a 100% perfect but I’m pretty statisfied with it over all 😄