|
| 1 | +from pyzbar import pyzbar |
| 2 | +import cv2 |
| 3 | +from playsound import playsound |
| 4 | + |
| 5 | +wavF = "beep.wav" |
| 6 | +objects_scanned =[] |
| 7 | + |
| 8 | +def userinput(zero): |
| 9 | + |
| 10 | + return image |
| 11 | + |
| 12 | +def draw_barcode(decoded, image): |
| 13 | + # n_points = len(decoded.polygon) |
| 14 | + # for i in range(n_points): |
| 15 | + # image = cv2.line(image, decoded.polygon[i], decoded.polygon[(i+1) % n_points], color=(0, 255, 0), thickness=5) |
| 16 | + image = cv2.rectangle(image, (decoded.rect.left, decoded.rect.top), |
| 17 | + (decoded.rect.left + decoded.rect.width, decoded.rect.top + decoded.rect.height), |
| 18 | + color=(0, 255, 0), |
| 19 | + thickness=5) |
| 20 | + |
| 21 | + return image |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +def decode(image): |
| 26 | + # decodes all barcodes from an image |
| 27 | + decoded_objects = pyzbar.decode(image) |
| 28 | + for obj in decoded_objects: |
| 29 | + |
| 30 | + if obj.data not in objects_scanned: |
| 31 | + |
| 32 | + # draw the barcode |
| 33 | + image = draw_barcode(obj, image) |
| 34 | + # print barcode type & data |
| 35 | + playsound(wavF) |
| 36 | + #print("Name:", obj.name) |
| 37 | + print("Type:", obj.type) |
| 38 | + print("Data:", obj.data) |
| 39 | + print("Scan successful") |
| 40 | + objects_scanned.append(obj.data) |
| 41 | + break |
| 42 | + |
| 43 | + return image |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == "__main__": |
| 47 | + cap = cv2.VideoCapture(0) |
| 48 | + while True: |
| 49 | + # read the frame from the camera |
| 50 | + _, frame = cap.read() |
| 51 | + # decode detected barcodes & get the image |
| 52 | + # that is drawn |
| 53 | + decoded_objects = decode(frame) |
| 54 | + # show the image in the window |
| 55 | + cv2.imshow("frame", frame) |
| 56 | + |
| 57 | + if cv2.waitKey(1) == ord("q"): |
| 58 | + break |
| 59 | + |
0 commit comments