-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
122 lines (93 loc) · 3.5 KB
/
main.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# USAGE
# python barcode_scanner_video.py
# import the necessary packages
import pyautogui
import serial #for adding arduino support in the Raspi (master slave connection)
from imutils.video import VideoStream #imutils basically helps in maintaining vedio frames for our camera
from pyzbar import pyzbar #python wrapper for zbar library for decoding info from QR code
import argparse
import datetime
import imutils
import time
import cv2
from fuzzywuzzy import fuzz #used for fuzzy string matching
# Serial connection using arduino uno
ArduinoSerial=serial.Serial('/dev/ttyUSB0',9600)
time.sleep(3)
print('start')
while 1:
print('Enter Source')
incoming = str(ArduinoSerial.readline())
source = incoming
print(incoming)
incoming=""
incoming = str(ArduinoSerial.readline())
destination = incoming
print(incoming)
print('next')
# construct the argument parser and parse the arguments
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())
# initialize the video stream and allow the camera sensor to start
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
#vs = VideoStream(usePiCamera=True).start()
time.sleep(2.0)
# open the output CSV file for writing and initialize the set of
# barcodes found thus far
csv = open(args["output"], "w")
found = set()
# loop over the frames from the video stream
while True:
# resize the frame from the threaded video stream and resize to a maximum width of 420 pixels
frame = vs.read()
frame = imutils.resize(frame, width=420)
# search for the barcodes in the frame and decode each of the barcodes
barcodes = pyzbar.decode(frame)
# loop over the decoded barcodes frames
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(frame, (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(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
print(barcodeData)
# wE use fuzzywuzzy package to match the string if it has a probability/similarity of more than 65%
Token_Sort_Ratio = fuzz.token_sort_ratio(barcodeData,destination)
if(Token_Sort_Ratio > 65):
print("Name Matched")
barcodeData1= barcodeData+'#'
ArduinoSerial.write(barcodeData1.encode())
else:
print("continue Searching")
barcodeData1= barcodeData+'#'
ArduinoSerial.write(barcodeData1.encode())
if barcodeData not in found:
csv.write("{},{}\n".format(datetime.datetime.now(),
barcodeData))
csv.flush()
found.add(barcodeData)
# show the output frame
cv2.imshow("Barcode Scanner", frame)
key = cv2.waitKey(1) & 0xFF
#Ratio = fuzz.ratio(destination.lower(),barcodeData.lower())
#Partial_Ratio = fuzz.partial_ratio(destination.lower(),barcodeData.lower())
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
# close the output CSV file
print("[INFO] cleaning up...")
csv.close()
cv2.destroyAllWindows()
vs.stop()