I. Case studies
Let's first think about what operations need to be written to implement a code scanning tool. During the code scanning process we need to turn on the camera and how to recognize the QR code by the phone or computer. So we need to implement two key steps: call the camera, recognize the QR code.
Each of these two operations corresponds to two modules, which areopencv
cap (a poem)pyzbar
whichopencv
is Intel's computer vision processing module, while pyzbar is the module used to parse QR codes.
II. Environment
The environment includespython
Environment and modules. My environment is as follows:
systems:Windows 10 python:python 3.7.9 opencv:opencv-python-4.4.0.44 pyzbar:pyzbar-0.1.8
Module installation is easy, we directly use thepip
Installation, firstopencv
Module:
pip install opencv-python
And then there'spyzbar
Module:
pip install pyzbar
The system will automatically install the latest version when no installation version is specified. After installing the module, we can come to the implementation of the sweep tool.
III. Recognizing the QR code
With the pyzbar module, our work of recognizing QR codes is very simple, first of all you need to prepare a QR code. With the QR code you can start parsing it, the steps are as follows:
1. Read the QR code image
2. Parse the data in the QR code
3. Extract data information in the parsed data
The realization code is as follows:
import cv2 from pyzbar import pyzbar # 1. Read the QR code image qrcode = ('') # 2. Parsing the data in the QR code data = (qrcode) print(data) # 3. Parsing out the QR code's DATA information in the data text = data[0].('utf-8') print(text)
In the above we parsed it twice, the first time getting adata
Let's take a look.data
What it looks like:
[Decoded(data=b'/r/vC_fhynEKnRVrW3k93qu', type='QRCODE', rect=Rect(left=140, top=113, width=390, height=390), polygon=[Point(x=140, y=113), Point(x=140, y=503), Point(x=530, y=503), Point(x=530, y=113)])]
You can see that it's a list, and that the first data in the list containsurl
information. So we need to parse it again with the following code:
text = data[0].('utf-8')
This way we can get the information contained in the QR code. For ease of subsequent use, the above code can be written as a function:
def scan_qrcode(img_path): qrcode = (img_path) data = (qrcode) return data[0].('utf-8')
Next we'll look at how to call the camera.
IV. Calling the camera
existopencv
There is aVideoCapture
class is used to read the video, the same can be used to call the camera. The steps to call the camera are as follows:
1. Call the camera
2. Circulation
3. Read a frame within the loop
4. Display the current reading screen
5. Waiting for keyboard input
6. Determine whether to press the exit key q
7. Press the push button to exit, but don't press it to continue the cycle.
The specific code is as follows:
import cv2 # Calling the camera cap = (0) while True: # Read a frame ret, frame = () # Display the current frame ('scan qrcode', frame) # Waiting for keyboard input key = (10) # Turn off the camera when the q key is pressed if key == ord('q'): break # Destroy all windows ()
You guys can try running the above code yourselves, the effect is like turning on your own front facing camera.
Now that the camera is called, we can combine the two parts of the code.
V. Realization of code-sweeping tools
The main part of our scanning tool is to call the operation of the camera, we need to parse each frame read, when the result is parsed output and exit. The specific code is as follows:
import cv2 from pyzbar import pyzbar def scan_qrcode(qrcode): data = (qrcode) return data[0].('utf-8') cap = (0) while True: ret, frame = () ('scan qrcode', frame) # Parse the QR code text = None try: text = scan_qrcode(frame) except Exception as e: pass if text: print(text) break key = (10) if key == ord('q'): break ()
Above we putscan_qrcode
The function is modified a bit, from the original pass in the image path to pass in the image object directly. Because passing theVideoCapture
object and the image frames obtained through theThe acquired image is of the same data type.
The key step above is in the operation of parsing the QR code. First define atext
, because the parsing process will throw an exception if there is no QR code, so it is handled with a statement. How to judge by iftext
content, and only when we have actually parsed the data does the program output the result and exit the program.
By this point, we have implemented the sweep tool.
To this point this article on the Python implementation of the code-sweeping tool is actually so good! The article is introduced to this, more related Python code-sweeping tool content please search for my previous posts or continue to browse the following related articles I hope you will support me more in the future!