-
Notifications
You must be signed in to change notification settings - Fork 4
/
barcode.py
45 lines (34 loc) · 1.63 KB
/
barcode.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
37
38
39
40
41
42
43
# import the necessary packages
from pyzbar import pyzbar
import argparse
import cv2
# construct the argument parser and parse the arguments
# ap = argparse.ArgumentParser()
# ap.add_argument("-i", "--image", required=True,help="path to input image")
# args = vars(ap.parse_args())
# load the input image
# image = cv2.imread(args["image"])
def file(img):
image = cv2.imread(img)
# find the barcodes in the image and decode each of the barcodes
barcodes = pyzbar.decode(image)
barcode_dict={}
# loop over the detected barcodes
for barcode in barcodes:
# extract the bounding box location of the barcode and draw the
# bounding box surrounding the barcode on the image
(x, y, w, h) = barcode.rect
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
# the barcode data is a bytes object so if we want to draw it on
# our output image we need to convert it to a string first
barcodeData = barcode.data.decode("utf-8")
barcodeType = barcode.type
# draw the barcode data and barcode type on the image
text = "{} ({})".format(barcodeData, barcodeType)
cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 0, 255), 2)
# print the barcode type and data to the terminal and aappending data to dictionary
barcode_dict[barcodeData] = barcodeType
print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))
# show the output image
return barcode_dict #returning data dictionary