-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBarcode_qrcode_scanner_live_stream.py
More file actions
85 lines (61 loc) · 1.91 KB
/
Copy pathBarcode_qrcode_scanner_live_stream.py
File metadata and controls
85 lines (61 loc) · 1.91 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""reading barcodes and qr codes from live stream"""
#importing libraries
from imutils.video import VideoStream
from pyzbar import pyzbar
import imutils
import datetime
import time
import cv2
import argparse
#parsing arguments to take command line values
ap = argparse.ArgumentParser()
#output to a csv file all the informations scanned
ap.add_argument("-o", "--output", type = str, default="barcodes.csv", help = "path to output CSV file containing barcodes")
args = vars(ap.parse_args())
#indication of stram starting
print("[INFO] starting video stream ...")
#starting videostream through webcam
vs = VideoStream(src=0).start()
#waiting time for warming up the camera, this can be eliminated
time.sleep(2.0)
#opening the csv file
csv = open(args["output"], "w")
#to keep the file unique
found = set()
while True:
#reading video stream
frame = vs.read()
#resizing the window
frame = imutils.resize(frame, width = 700)
#decoding the codes scanned
codes = pyzbar.decode(frame)
#iterating each code scanned
for code in codes:
#drawing a rectangle round the codes
(x,y,w,h) = code.rect
cv2.rectangle(frame, (x,y), (x+w, y+h), (255,255,0), 2)
#decoding the contents into string
codeData = code.data.decode("utf-8")
#type of code
codeType = code.type
#putting into texts
text = "{} ({})".format(codeData, codeType)
cv2.putText(frame, text, (x,y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 2)
#hecking if data found is unique or previously scanned
if codeData not in found:
#if not previously scanned then adding the data in csv
csv.write("{}, {}\n".format(datetime.datetime.now(),codeData))
csv.flush()
#also adding into found set
found.add(codeData)
#showing the result
cv2.imshow("Scanner", frame)
key = cv2.waitKey(1) & 0xFF
#quit if pressed q
if key == ord("q"):
break
#cleaning and closing
print("[INFO] cleaning...")
csv.close()
cv2.destroyAllWindows()
vs.stop()