-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
pyzbar_opencv_comparison.py
36 lines (29 loc) · 1.22 KB
/
pyzbar_opencv_comparison.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import cv2
from pyzbar.pyzbar import decode
from qreader import QReader
SAMPLE_IMG_1 = os.path.join(
os.path.dirname(__file__), "documentation", "resources", "test_draw_64x64.jpeg"
)
SAMPLE_IMG_2 = os.path.join(
os.path.dirname(__file__), "documentation", "resources", "64x64.png"
)
if __name__ == "__main__":
# Initialize the three tested readers (QRReader, OpenCV and pyzbar)
qreader_reader, cv2_reader, pyzbar_reader = QReader(), cv2.QRCodeDetector(), decode
for img_path in (SAMPLE_IMG_1, SAMPLE_IMG_2):
# Read the image
img = cv2.imread(img_path)
# Try to decode the QR code with the three readers
qreader_out = qreader_reader.detect_and_decode(image=img)
cv2_out = cv2_reader.detectAndDecode(img=img)[0]
pyzbar_out = pyzbar_reader(image=img)
# Read the content of the pyzbar output (double decoding trick is needed to solve possible encoding issues)
pyzbar_out = tuple(
out.data.data.decode("utf-8").encode("shift-jis").decode("utf-8")
for out in pyzbar_out
)
# Print the results
print(
f"Image: {img_path} -> QReader: {qreader_out}. OpenCV: {cv2_out}. pyzbar: {pyzbar_out}."
)