-
Notifications
You must be signed in to change notification settings - Fork 0
/
0912_digit_dual.py
291 lines (260 loc) · 9.73 KB
/
0912_digit_dual.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import cv2
import serial
import numpy as np
from time import sleep
from threading import Thread
import portusb
import cameraindex
import subprocess
from openpyxl import load_workbook, Workbook
import datetime
import os
import shutil
import glob
from zoom import Zoom
from imutils.video import FPS
#!####################### checking MAC address #########################
# mac=open('/sys/class/net/%s/address' %'eth0').read().strip()
# while( mac!= 'b8:27:eb:df:bd:66'):
# continue
#!########################### Training KNN Model ############################
samples = np.loadtxt('generalsamples4.data', np.float32)
responses = np.loadtxt('generalresponses4.data', np.float32)
responses = responses.reshape((responses.size, 1))
model = cv2.ml.KNearest_create()
model.train(samples, cv2.ml.ROW_SAMPLE, responses)
#!############################ Init Vars #########################
dctChar3 = dctChar4 = dict() # [vi_tri_x:value]
preRet3 = preRet4 = iFrame3 = iFrame4 = iFail3 = iFail4 = finalReceive = screenRet4 = iInit = lowfinalRec4 = upfinalRec4 = finalRec3 = 0
final3 = final4 = False
flagPass = flagFail = flagSpeed = flagInit = False
seriFrame = 20
# sentFlag = False
# cam4Flag = True
#!############################ XLS Generator Function #########################
def newWorkbook():
global wb, ws
wb = Workbook()
ws = wb.active
# title bar
timeCol = ws.cell(row=1, column=2, value='Time')
timeCol.style = 'Title'
sttCol = ws.cell(row=1, column=3, value='Status')
sttCol.style = 'Title'
errRow = ws.cell(row=4, column=6, value='Error')
errRow.style = 'Calculation'
totalRow = ws.cell(row=5, column=6, value='Total')
totalRow.style = 'Calculation'
def insertRow(status):
global wb, ws
ws.append([None, datetime.datetime.now().strftime(
'%d-%m-%Y %H:%M:%S'), status])
if (status == 'Good'):
ws['C{}'.format(ws.max_row)].style = 'Good'
else:
ws['C{}'.format(ws.max_row)].style = 'Bad'
wb.save('logs.xlsx')
def filter():
ws.auto_filter.ref = 'A1:C{}'.format(ws.max_row)
wb.save('logs.xlsx')
#!############################ Init I/O #########################
if (os.path.isfile('./logs.xlsx')):
wb = load_workbook(filename='logs.xlsx')
ws = wb.active
else:
newWorkbook()
#port = portusb.lstPort()
port = portusb.portNew()
print('[INFO] Openning ' + port)
ser = serial.Serial(port, 9600)
vs3 = Zoom(400, cameraindex.index('Camera'), 169, 266, 189, 377).start()
vs4 = Zoom(400, cameraindex.index('CAMERA'), 263, 372, 179, 438).start()
sleep(2.0)
fps = FPS().start()
#!############################ Loop #########################
while True:
try:
serInWait = ser.in_waiting
except OSError:
vs3.stop()
vs4.stop()
ser.close()
cv2.destroyAllWindows()
os.system('python3 ./0908_digit_dual.py')
#!===================== If Serial is available =====================!#
if (serInWait):
recieveOrd = [] # reset
for i in range(13):
capture = ord(ser.read(1))
if (capture == 110):
print('[INFO] Arduino is RESET_ING ...')
screenRet4 = iInit = 0
final3 = final4 = flagInit = flagFail = flagPass = False
flagSpeed = True
### chi test thu o day ####
# insertRow('Bad')
if (capture == 111):
print('[INFO] Detecting valid USB')
strUSB = str(subprocess.check_output('lsusb', shell=True))
#strUSB = glob.glob('/media/pi/[A-Za-z]*')[0]
if strUSB.rfind('0951:1666') != -1:
print('[INFO] USB access grandted')
ser.write('USB'.encode())
# auto filter
ws.auto_filter.ref = 'A1:C{}'.format(ws.max_row)
wb.save('logs.xlsx')
# cut it out
if(os.path.isfile('./logs.xlsx')):
shutil.copy('./logs.xlsx',
glob.glob('/media/pi/[A-Za-z]*')[0])
sleep(5) # waiting for copy
os.remove('./logs.xlsx')
# then create new wb
newWorkbook()
ser.write('usb'.encode())
else:
print('[ERROR] USB access denied')
recieveOrd.append(capture)
# check error nếu bit 3 != 30, 31 và != tổng 3 bit còn lại -> lỗi
if(recieveOrd[0]+recieveOrd[1]+recieveOrd[2]+recieveOrd[4]+recieveOrd[5]+recieveOrd[6]+recieveOrd[7]+recieveOrd[8]+recieveOrd[9]+recieveOrd[10]+recieveOrd[11]) != recieveOrd[12]:
ser.write("RESEND\r".encode())
sleep(0.5)
finalRec3 = float((recieveOrd[0])*100 +
(recieveOrd[1])*10 + (recieveOrd[2]))/100
lowfinalRec4 = recieveOrd[4]*1000 + recieveOrd[5] * \
100 + recieveOrd[6]*10 + recieveOrd[7]
upfinalRec4 = recieveOrd[8]*1000 + recieveOrd[9] * \
100 + recieveOrd[10]*10 + recieveOrd[11]
print('[INFO] Thresh: {}'.format(finalRec3))
print('[INFO] Lower Speed: {} \t Upper Speed: {}'.format(
lowfinalRec4, upfinalRec4))
#!===========================================================================!#
vs4.cont()
vs3.pause()
dst4 = vs4.dst
roi4 = vs4.zoom
roiBar4 = vs4.roiBar
cv2.imshow('Zoom{}'.format(vs4.src), dst4)
#cv2.imshow('Panel{}'.format(vs4.src), roi4)
contours4, _ = cv2.findContours(
dst4, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
dctChar4 = dict() # reset
for cnt in contours4:
[x4, y4, w4, h4] = cv2.boundingRect(cnt)
if (w4 > roiBar4 and h4 > roiBar4):
_, results, _, _ = model.findNearest(np.float32(cv2.resize(
dst4[y4:y4+h4, x4:x4+w4], (10, 10)).reshape((1, 100))), k=5)
dctChar4[x4] = int(results[0][0])
cv2.rectangle(roi4, (x4, y4), (x4+w4, y4+h4), (0, 0, 255), 2)
if(len(dctChar4) == 4):
multi = []
for xCoor in sorted(dctChar4):
multi.append(dctChar4[xCoor])
screenRet4 = multi[0]*1000 + multi[1]*100 + multi[2]*10 + multi[3]
# if (screenRet4 != preRet4 ):
#preRet4 = screenRet4
# print('\t{}'.format(screenRet4))
if ((screenRet4 != preRet4) and (not flagInit)):
preRet4 = screenRet4
iInit += 1
if (iInit > 4):
iInit = 0
flagInit = True
flagSpeed = False
print('\t{}'.format(screenRet4))
if (screenRet4 >= lowfinalRec4 and screenRet4 <= upfinalRec4):
iFrame4 += 1
#iFail4 = 0
if (iFrame4 > seriFrame):
# cam4Flag = False
# sleep(3)
# print('[INFO] speed ok')
# ser.write('SPEED\r'.encode())
# sleep(0.5)
final4 = True # red speed ok
iFrame4 = 0
else:
iFrame4 = 0
if ((screenRet4 > 1500 and screenRet4 < lowfinalRec4) or screenRet4 > upfinalRec4):
iFail4 += 1
#iFrame4 = 0
if (iFail4 > seriFrame):
final4 = False
iFail4 = 0
else:
iFail4 = 0
vs3.cont()
vs4.pause()
dst3 = vs3.dst
roi3 = vs3.zoom
roiBar3 = vs3.roiBar
cv2.imshow('Zoom{}'.format(vs3.src), dst3)
# cv2.imshow('Panel{}'.format(vs3.src),roi3)
contours3, _ = cv2.findContours(
dst3, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
dctChar3 = dict() # reset
for cnt in contours3:
[x3, y3, w3, h3] = cv2.boundingRect(cnt)
if (w3 > roiBar3 and h3 > roiBar3):
_, results, _, _ = model.findNearest(np.float32(cv2.resize(
dst3[y3:y3+h3, x3:x3+w3], (10, 10)).reshape((1, 100))), k=5)
dctChar3[x3] = int(results[0][0])
cv2.rectangle(roi3, (x3, y3), (x3+w3, y3+h3), (0, 0, 255), 2)
if (len(dctChar3) == 3):
multi = []
for xCoor in sorted(dctChar3):
multi.append(dctChar3[xCoor])
screenRet3 = float(multi[0]*100 + multi[1]*10 + multi[2])/100
if (screenRet3 != preRet3):
preRet3 = screenRet3
print(screenRet3)
if (screenRet3 <= finalRec3):
iFail3 = 0
iFrame3 += 1
if (iFrame3 > seriFrame):
# print('[INFO] PASS')
# insertRow('Good')
# ser.write("PASS\r".encode()) # green light
# sleep(0.5)
final3 = True
iFrame3 = 0
else:
iFrame3 = 0
iFail3 += 1
if (iFail3 > seriFrame):
final3 = False
iFail3 = 0
if (final3 and final4 and not flagPass):
flagPass = True
flagSpeed = False
print('[INFO] Pass')
ser.write('PASS\r'.encode())
elif (screenRet4 <= 1500 and not flagFail):
flagFail = flagSpeed = True
flagPass = flagInit = False
print('[INFO] WHITE')
ser.write('WHITE\r'.encode())
elif (not final3 and final4 and not flagSpeed):
flagSpeed = True
flagPass = False
print('[INFO] Failed 1')
ser.write('SPEED\r'.encode())
elif (final3 and not final4 and not flagSpeed):
flagSpeed = True
flagPass = False
print('[INFO] Failed 2')
ser.write('SPEED\r'.encode())
elif (not final3 and not final4 and not flagSpeed):
flagSpeed = True
flagPass = False
print('[INFO] Failed 3')
ser.write('SPEED\r'.encode())
if cv2.waitKey(1) == 27:
break
fps.update()
fps.stop()
print('[INFO] FPS: {:.2f}'.format(fps.fps()))
vs3.stop()
vs4.stop()
ser.close()
cv2.destroyAllWindows()