Skip to content

Commit 9110926

Browse files
authored
Fix bbox coordinates extraction
All commented in code, please see there
1 parent 52cb6d0 commit 9110926

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

general/generating-reading-qrcode/read_qrcode.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
filename = sys.argv[1]
55

66
# read the QRCODE image
7-
img = cv2.imread(filename)
7+
#in case if QR code is not black/white it is better to convert it into grayscale
8+
img = cv2.imread(filename, 0)# Zero means grayscale
9+
img_origin = cv2.imread(filename)
810

911
# initialize the cv2 QRCode detector
1012
detector = cv2.QRCodeDetector()
@@ -17,16 +19,19 @@
1719
print(f"QRCode data:\n{data}")
1820
# display the image with lines
1921
# length of bounding box
20-
n_lines = len(bbox)
22+
n_lines = len(bbox[0])#Cause bbox = [[[float, float]]], we need to convert fload into int and loop over the first element of array
23+
bbox1 = bbox.astype(int) #Float to Int conversion
2124
for i in range(n_lines):
2225
# draw all lines
23-
point1 = tuple(bbox[i][0])
24-
point2 = tuple(bbox[(i+1) % n_lines][0])
25-
cv2.line(img, point1, point2, color=(255, 0, 0), thickness=2)
26-
27-
28-
29-
# display the result
30-
cv2.imshow("img", img)
31-
cv2.waitKey(0)
32-
cv2.destroyAllWindows()
26+
point1 = tuple(bbox1[0, [i][0]])
27+
point2 = tuple(bbox1[0, [(i+1) % n_lines][0]])
28+
cv2.line(img_origin, point1, point2, color=(255, 0, 0), thickness=2)
29+
30+
31+
# display the result
32+
cv2.imshow("img", img)
33+
cv2.waitKey(0)
34+
cv2.destroyAllWindows()
35+
36+
else:
37+
print("QR code not detected")

0 commit comments

Comments
 (0)