From 0c25c0ae93176f7fd2d597f1e99ef55bc4e87185 Mon Sep 17 00:00:00 2001 From: ploedige Date: Fri, 13 Jan 2023 09:17:02 +0000 Subject: [PATCH] first "working" version of the barcode scanner --- camera_test.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 camera_test.py diff --git a/camera_test.py b/camera_test.py new file mode 100644 index 0000000..db376b7 --- /dev/null +++ b/camera_test.py @@ -0,0 +1,49 @@ +from imutils.video import VideoStream +from pyzbar import pyzbar +import argparse +import datetime +import imutils +import time +import cv2 + +ap = argparse.ArgumentParser() +ap.add_argument("-o", "--output", type=str, default="barcodes.csv", +help="path to output CSV file containing barcodes") +args = vars(ap.parse_args()) +vs = VideoStream(src=0).start() #Uncomment this if you are using Webcam +#vs = VideoStream(usePiCamera=True).start() # For Pi Camera +time.sleep(2.0) +csv = open(args["output"], "w") +found = set() + +while True: + frame = vs.read() + frame = imutils.resize(frame, width=400) + barcodes = pyzbar.decode(frame) + for barcode in barcodes: + (x, y, w, h) = barcode.rect + cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) + barcodeData = barcode.data.decode("utf-8") + barcodeType = barcode.type + text = "{} ({})".format(barcodeData, barcodeType) + print (text) + cv2.putText(frame, text, (x, y - 10), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) + +# if the barcode text is currently not in our CSV file, write +# the timestamp + barcode to disk and update the set + if barcodeData not in found: + csv.write("{},{}\n".format(datetime.datetime.now(), + barcodeData)) + csv.flush() + found.add(barcodeData) + cv2.imshow("Barcode Reader", frame) + key = cv2.waitKey(1) & 0xFF + +# if the `s` key is pressed, break from the loop + if key == ord("s"): + break + print("[INFO] cleaning up...") + csv.close() + cv2.destroyAllWindows() + vs.stop() \ No newline at end of file