-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbarcode_detection_image.py
More file actions
49 lines (40 loc) · 1.58 KB
/
barcode_detection_image.py
File metadata and controls
49 lines (40 loc) · 1.58 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
import cv2
import numpy as np
from pyzbar import pyzbar
image = cv2.imread("image.png")
image = cv2.resize(image, (640, 850))
red = (0, 0, 255)
blue = (255, 0, 0)
qrcode_color = (255, 255, 0)
barcode_color = (0, 255, 0)
# decode and detect the QR codes and barcodes
barcodes = pyzbar.decode(image)
# initialize the total number of QR codes and barcodes
qr_code = 0
code = 0
for barcode in barcodes:
# extract the points of th polygon of the barcode and create a Numpy array
pts = np.array([barcode.polygon], np.int32)
pts = pts.reshape((-1, 1, 2))
# check to see if this is a QR code or a barcode
if barcode.type == "QRCODE":
qr_code += 1
cv2.polylines(image, [pts], True, qrcode_color, 3)
elif barcode.type == "CODE128":
code += 1
cv2.polylines(image, [pts], True, barcode_color, 3)
# decode the barcode data and draw it on the image
text = "{}".format(barcode.data.decode("utf-8"))
cv2.putText(image, text, (barcode.rect[0] + 10, barcode.rect[1] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, red, 2)
# Display the number of QR codes and barcodes detected
if len(barcodes) == 0:
text = "No barcode found on this image"
cv2.putText(image, text, (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, blue, 2)
else:
text = "{} QR code(s) found on this image".format(qr_code)
cv2.putText(image, text, (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, blue, 2)
text = "{} barcode(s) found on this image".format(code)
cv2.putText(image, text, (20, 43), cv2.FONT_HERSHEY_SIMPLEX, 0.75, blue, 2)
cv2.imshow("Image", image)
cv2.waitKey(0)