-
Notifications
You must be signed in to change notification settings - Fork 15
/
cart.py
227 lines (184 loc) · 4.82 KB
/
cart.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
from gpiozero import LED
from time import sleep
from gpiozero import MotionSensor
from signal import pause
import serial
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import sys
import cv2
import zbar
import Image
import pandas as pd
import numpy as np
led=LED(18)
pir=MotionSensor(23)
led.on()
print("Waiting for pir to settle")
pir.wait_for_no_motion()
print("ready")
led.off()
port="/dev/ttyACM0"
s1=serial.Serial(port,9600)
data=pd.read_csv('itemdata.csv')
print(data)
# Debug mode
DEBUG = False
if len(sys.argv) > 1:
DEBUG = sys.argv[-1] == 'DEBUG'
# Configuration options
FULLSCREEN = not DEBUG
if not DEBUG:
RESOLUTION = (1280, 720)
else:
RESOLUTION = (1280,720)
# Initialise Raspberry Pi camera
camera = PiCamera()
camera.resolution = RESOLUTION
#camera.framerate = 10
#camera.vflip = True
#camera.hflip = True
#camera.color_effects = (128, 128)
# set up stream buffer
rawCapture = PiRGBArray(camera, size=RESOLUTION)
# allow camera to warm up
#time.sleep(0.1)
#print "PiCamera ready"
# Initialise OpenCV window
if FULLSCREEN:
cv2.namedWindow("#iothack15", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("#iothack15", cv2.WND_PROP_FULLSCREEN, cv2.cv.CV_WINDOW_FULLSCREEN)
else:
cv2.namedWindow("#iothack15")
#print "OpenCV version: %s" % (cv2.__version__)
print "Press q to exit ..."
#weight_init
#s1.flushInput() #flush buffer
#pre_loop=0;
#k=0;
#for n in range(10):
# if s1.inWaiting()>0:
# line = s1.readline()
# k=k+float(line)
#print(type(k))
#weigh_avg=k/10;
#print(weigh_avg);
s1.flushInput()
scanner = zbar.ImageScanner()
scanner.parse_config('enable')
codes=[]
i=0
# Capture frames from the camera
cart = []
try:
while True:
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
k=0.0;
s1.reset_input_buffer()
# time.sleep(1)
#s1.flushInput()
#for m in range(10):
# s1.flushInput()
while not s1.inWaiting()>0:
continue
while s1.read() != '*' :
continue
line = s1.readline()
# print(line)
# k=k+float(line)
#print(type(k))
prev_avg=float(line);
print(prev_avg);
i=len(codes)
# as raw NumPy array
output = frame.array.copy()
# raw detection code
gray = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY, dstCn=0)
pil = Image.fromarray(gray)
width, height = pil.size
raw = pil.tobytes()
# create a reader
image = zbar.Image(width, height, 'Y800', raw)
scanner.scan(image)
# extract results
for symbol in image:
# do something useful with results
print( symbol.data)
#print(type(symbol.data))
codes.append(symbol.data)
j = len(codes)
if i!=j:
s1.flushInput()
time.sleep(4.0)
s1.reset_input_buffer()
while not s1.inWaiting()>0:
continue
while s1.read() != '*' :
continue
line = s1.readline()
current=float(line)
print(current);
#current="weight current"
for bar in data['barcode']:
if str(bar) == symbol.data:
name = (data.loc[data['barcode']==bar, 'item'].item())
cost = (data.loc[data['barcode']==bar, 'price'].item())
weight = (data.loc[data['barcode']==bar, 'weight'].item())
differ=current-prev_avg
# print(differ)
# print(1200*weight)
# print(800*weight)
#if((differ<(1200*weight)) and (differ>(800*weight))):
# cart.append(name)
# print("item added - ", name, cost)
# print(cart)
#ADDDD TO THE LIST####
if(differ<10):
name = (data.loc[data['barcode']==bar, 'item'].item())
###DELETE THE VALUE
cost = (data.loc[data['barcode']==bar, 'price'].item())
cart.remove(name)
print("deleted item - " , name , cost)
print(list)
if(differ>1100*weight):
print("ERROR")
else:
cart.append(name)
print("item added - ", name, cost)
print(cart)
#ADDDD TO THE LIST####
#time.sleep(2.0)
# show the frame
# cv2.imshow("#iothack15", output)
# clear stream for next frame
rawCapture.truncate(0)
print("--!")
# Wait for the magic key
keypress = cv2.waitKey(1) & 0xFF
if keypress == ord('q'):
break
except KeyboardInterrupt:
camera.close()
cv2.destroyAllWindows()
tcost=[]
for bar in data['barcode']:
n=0
for code in codes:
if str(bar) == code :
n = n+1
name = (data.loc[data['barcode']==bar, 'item'].item())
cost = n*(data.loc[data['barcode']==bar, 'price'].item())
tcost.append(cost)
total = np.array(tcost)
totalcost = total.sum()
print(cart)
print(totalcost)
pir.wait_for_motion()
led.on()
print("Motion detected")
time.sleep(3)
pir.wait_for_motion()
led.on()
print("Motion detected")
time.sleep(3)