Hello, I am Xiao Wu 🐶

Today is the third installment in the Python Life Changing series, and it’s a problem case (dog head) for you.

Antecedents feed

My friends who know me probably know that xiaowu often sends books to everyone. In the last year, not counting the joint raffle, I have given away 1,000 books by myself and publishing house sponsorship alone.

If it is bought by yourself, you need to contact quick brother to send out books.

After the delivery, the Courier would give me screenshots for feedback, but I had a problem when I wanted the tracking number.

I only get n screenshots (with delivery info) after each book delivery.

In order to timely feedback everyone logistics information, I need to extract the tracking number as soon as possible.

Think of solutions

Each time there are probably a dozen to dozens of screenshots, manual identification is really too much trouble.

Take a look at what each screenshot looks like and then figure out how to batch it.

In order to obtain the tracking number in the picture in bulk, I came up with two solutions:

  1. Use Python to recognize bar codes to get the exact tracking number directly

  2. Call OCR with Python to identify the express order number text in the screenshot

Which do you think is easier and more accurate?

Today I would like to talk about the first method of the process and the experience of the pit.

Traverse images

First of all, the first step is to obtain all the screenshots in the folder, and then carry out barcode identification.

For details, see the notes

import os

def get_jpg() :
    jpgs = []
    path = os.getcwd()
    for i in os.listdir(path):  Get a list of files
        if i.split(".")[-1] = ="jpg":  # Filter JPG files
            oldname=os.path.join(path,i)  # old file name
            i = i.replace('wechat picture _'.' ')
            newname=os.path.join(path,i)  # new file name
            os.rename(oldname,newname)  # the name
            jpgs.append(i)
    return jpgs
Copy the code

In addition to traversing the filter image, the above code also involves the rename operation.

This is because when I later used OpencV, I kept getting errors whenever I opened a path that contained Chinese characters, so I simply removed the Chinese characters from the screenshot names.

Execute the built get_jpg() function, and get

These are the four screenshots from the demo file, and let’s start identifying them.

Identification bar code

Python’s third-party module PyzBar can easily handle qr code recognition. We’ll use it in much the same way for one-dimensional bar codes. It is also used with cv2 to read image files using cv2.imread().

Note: For the CV2 module, you need to type pip3 install Opencv-python during installation, but import Cv2 during import.

The specific statement to identify the bar code is as follows:

import pyzbar.pyzbar as pyzbar
import cv2

def get_barcode(img) :
    image = cv2.imread(img)
    barcodes = pyzbar.decode(image)
    barcode = barcodes[0]
    barcode_data = barcode.data.decode("utf-8")
    return barcode_data
Copy the code

The get_barcode() function built above recognizes the barcode and returns the resulting data.

We can use the for loop to iterate over all the images obtained above, and then use the get_barcode() function in turn to identify the barcode.

data_m =[]
for i in jpgs:
    data = get_barcode(i)
    data_m.append(data)
data_m
Copy the code

It can be found that the bar codes in the four screenshots were successfully identified and the corresponding tracking numbers were obtained.

summary

Reviewing today’s problem cases, I first came up with two solutions through thinking. The advantage of the first is that the barcode is more accurate than OCR, but it only takes the tracking number. Later, when giving feedback to the students who got the books as gifts, I also need to manually match the name with the tracking number, which is not lazy enough. I will introduce the process and advantages and disadvantages of the second method.

If you want to see more examples of real problems with Python that change lives, give this article a thumbs up at the bottom right.

If you have a problem you’ve been wanting to solve with Python, let me know in the comments section at 🚀