Hello, everyone. I'm Xiaowu.
This is the third installment of the Python Changing Lives series and a case study of a problem that concerns you.
synopsis (of previous events)
Those of you who know me may know that Little Five often gives away books. In the last year, not counting the joint raffle giveaways, alone I've given away 1,000 books from my own purchases + publisher sponsorships.
If you are purchasing your own, you will also need to contact the fast guy to send out the books yourself.
The courier guy would give me a screenshot for feedback after sending it out, however I had a problem when I wanted the single number.
Every time I send a book, I only get n screenshot (with courier info inside).
In order to provide timely feedback on everyone's logistics, I need to get the courier number extracted as soon as possible.
Thinking about solutions
There are probably dozens to dozens of screenshots at a time, and it's really too much of a hassle to go through and recognize them manually.
Why don't you take a look at what each screenshot probably looks like before you think about batch processing?
Mainly to batch fetch the delivery note number in the image, I came up with two solutions:
- Use python to recognize the barcode to get the exact courier order number directly.
- Call ocr in python to recognize the text of the delivery note number in the screenshot
Which one does everyone think is simpler and more accurate?
Today I'll start by talking about the process and pitfalls experienced with the first method.
Iterate over the image
First of all, the first step needs to be to get all the screenshots in the folder first, and then barcode recognition in turn.
You can refer to the notes for details on how to do this
import os def get_jpg(): jpgs = [] path = () for i in (path): # Get the list of files if (".")[-1] == "jpg": # Screening jpg files (screenshots) oldname=(path,i) # Old filenames i = ('microsoft picture_','') newname=(path,i) #New filename (oldname,newname) #Rename (i) return jpgs
The above code involves a renaming operation in addition to iterating through the filtered images.
This is because when I used opencv later on, the paths I opened would always give me an error as long as they contained Chinese, so I simply removed the Chinese from the screenshot names.
Execute the constructedget_jpg()
function to get the
These are the four screenshot files in the demo file, and their identification begins below.
Recognition of barcodes
Third-party modules for pythonpyzbar
It can easily handle the recognition of 2D barcodes. If we use it to recognize 1D barcodes this time, the usage is more or less the same. However, it is also used with cv2, mainly to utilize the()
to read the image file.
Note: For the cv2 module, the installation requires the entry of thepip3 install opencv-python
but imported using theimport cv2
。
The specific statements for recognizing bar codes are shown below:
import as pyzbar import cv2 def get_barcode(img): image = (img) barcodes = (image) barcode = barcodes[0] barcode_data = ("utf-8") return barcode_data
the above-builtget_barcode()
function can be realized to identify the bar code and return the result data.
We can use a for loop to iterate through all the images obtained in the previous section, and then in turn use theget_barcode()
function to recognize barcodes.
data_m =[] for i in jpgs: data = get_barcode(i) data_m.append(data) data_m
It can be found that the barcodes in the four screenshots were successfully recognized and the corresponding express order numbers were obtained.
wrap-up
Reviewing today's problem case, I first came up with two solutions by thinking about it. The first one has the advantage of recognizing barcodes more accurately than OCR, but its only acquiring the delivery slip number. Subsequently, when giving feedback to the students who got the complimentary book, I still need to manually correspond the name with the single number, which is not lazy enough. Follow-up will give you the process and advantages and disadvantages of the second method.
Above is Python to achieve easy identification of hundreds of courier single number of details, more about Python to identify the courier single number of information please pay attention to my other related articles!