Python tips and tricks — don’t spend your time, automate it

Privalov Vladimir
1 min readSep 1, 2019

--

Sometimes you have to do hard manual work. e.g. label training data for ML pipelines. That could be very tedious, time-consuming and routine work. You have always a chance to automate that making your life easier. Use your programming skills and let a machine make the hard work . And grab a cup of coffee :).

For instance, I had a tedious task to manually correct ground truth for OCR on price tags. We had a batch of photos from retail shop and our task was to obtain as much precise information from price tags as possible. At some moment we needed to correct ground truth from test results manually.

Having spent much time figuring out where roughly price tags locate on the image based on given coordinates I decided to implement a simple script which crops image using given coordinates and output only the price tag

import cv2

img = cv2.imread("images/store_2/DSC04560.JPG")

coords_str = "563 7647 406 225"
coords_str = coords_str.replace('\t', ' ')
coords = coords_str.split(' ')
# print(coords)
xmin = int(coords[0])
ymin = int(coords[1])
xmax = int(coords[0]) + int(coords[2])
ymax = int(coords[1]) + int(coords[3])
crop = img[ymin:ymax, xmin:xmax]
cv2.imwrite("images/store_2/DSC04560_crop.JPG", crop)

Very concise and useful code making routine stuff done more quickly without headache and doubts whether everything is done correctly with no mistakes.

That’s it. Make your life easier with small automating tools doing your work for you.

--

--

No responses yet