-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
275 lines (255 loc) · 7.43 KB
/
main.py
File metadata and controls
275 lines (255 loc) · 7.43 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
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
import cv2
import time
import imutils
import logging
import argparse
import datetime
import numpy as np
import tensorflow as tf
from utils import detector_utils as detector_utils
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('detector')
detection_graph, sess = detector_utils.load_inference_graph()
# Argument Parser
parser = argparse.ArgumentParser()
parser.add_argument(
'-wd',
'--width',
dest='width',
type=int,
default=1080,
help='Width of the frames in the video stream.')
parser.add_argument(
'-ht',
'--height',
dest='height',
type=int,
default=720,
help='Height of the frames in the video stream.')
parser.add_argument(
'-x1_ROI',
'--x1_ROI',
dest='x1_ROI',
type=int,
default=580,
help='ROI of detection p1_x.')
parser.add_argument(
'-x2_ROI',
'--x2_ROI',
dest='x2_ROI',
type=int,
default=780,
help='ROI of detection p2_x.')
parser.add_argument(
'-y1_ROI',
'--y1_ROI',
dest='y1_ROI',
type=int,
default=500,
help='ROI of detection p1_y.')
parser.add_argument(
'-y2_ROI',
'--y2_ROI',
dest='y2_ROI',
type=int,
default=720,
help='ROI of detection p2_y.')
parser.add_argument(
'-x1_CB',
'--x1_CB',
dest='x1_CB',
type=int,
default=575,
help='ROI of cashbox p1_x.')
parser.add_argument(
'-x2_CB',
'--x2_CB',
dest='x2_CB',
type=int,
default=707,
help='ROI of cashbox p2_x.')
parser.add_argument(
'-y1_CB',
'--y1_CB',
dest='y1_CB',
type=int,
default=582,
help='ROI of cashbox p1_y.')
parser.add_argument(
'-y2_CB',
'--y2_CB',
dest='y2_CB',
type=int,
default=661,
help='ROI of cashbox p2_y.')
parser.add_argument(
'-sth',
'--scorethreshold',
dest='score_thresh',
type=float,
default=0.5,
help='Score threshold for displaying bounding boxes')
parser.add_argument(
'-th',
'--threshold',
dest='threshold',
type=int,
default=100,
help='Threshold value for cashbox detection')
parser.add_argument(
'-LB',
'--LowerB',
dest='LOWERB',
nargs='+',
type=int,
default=[100, 0, 0],
help='Lower Boundary colour value for cashbox detection')
parser.add_argument(
'-UB',
'--UpperB',
dest='UPPERB',
nargs='+',
type=int,
default=[200, 80, 80],
help='Upper Boundary colour value for cashbox detection')
parser.add_argument(
'-src',
'--source',
dest='video',
type=str,
default='http://192.168.2.32:53258/videostream.cgi?user=admin&pwd=lauretta1',
help='Path to input video file.')
parser.add_argument(
'-hd',
'--hand(s)',
dest='num_hands_detect',
type=int,
default=2,
help='Max number of hands we want to detect/track.')
args = parser.parse_args()
cap = cv2.VideoCapture(args.video)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
# Variables
im_width, im_height = (args.width, args.height)
start = time.time()
t = time.time()
statusInt = -1
currentStatus = -1
transactionCount = 0
recordOnce = True
lowerStatus = True
lowestStatus = True
handUp = False
record = False
boxOpen = False
startCount = False
isRecodring = False
nearlyEndedTransaction = False
arr = [True, False, False, False]
out = None
tnow = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
out = cv2.VideoWriter('transaction-' + tnow + '.avi',
fourcc, 30.0, (args.width, args.height))
##################################### MAIN #####################################
while True:
ret, image_np = cap.read()
ori = image_np.copy()
ori = cv2.resize(ori, (args.width, args.height))
image_np = cv2.resize(image_np, (args.width, args.height))
frameCB = image_np[args.y1_CB:args.y2_CB, args.x1_CB:args.x2_CB]
maskCB = cv2.inRange(frameCB, np.array(args.LOWERB), np.array(args.UPPERB))
image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
image_np1 = np.zeros_like(image_np)
image_np1[args.y1_ROI:args.y2_ROI,
args.x1_ROI:args.x2_ROI] = image_np[args.y1_ROI:args.y2_ROI, args.x1_ROI:args.x2_ROI]
boxes, scores = detector_utils.detect_objects(image_np1,
detection_graph, sess)
#print('s', scores)
condCB = np.count_nonzero(maskCB) > args.threshold
condHand = any([s > args.score_thresh for s in scores])
for i in range(args.num_hands_detect):
detector_utils.draw_box_on_image(i, boxes, im_width,
im_height, image_np)
# Cashbox CLOSED
if nearlyEndedTransaction & (np.count_nonzero(maskCB) < args.threshold):
start = time.time()
status = 'CASH BOX CLOSED!'
statusInt = 3
arr[3] = True
startCount = False
lowerStatus = False
else:
arr[3] = False
# Cashbox OPENED & hand DETECTED
if boxOpen & condCB & condHand:
if lowerStatus:
status = 'HAND DETECTED!'
statusInt = 2
for i in range(args.num_hands_detect):
detector_utils.draw_box_on_image(i, boxes, im_width,
im_height, image_np)
arr[2] = True
handUp = True
record = True
startCount = False
lowestStatus = False
nearlyEndedTransaction = True
else:
arr[2] = False
handUp = False
# Cashbox OPENED
if condCB:
if not(startCount):
start = time.time()
startCount = True
if lowestStatus:
status = 'CASH BOX OPENED!'
statusInt = 1
arr[1] = True
#record = True
boxOpen = True
else:
arr[1] = False
boxOpen = False
# No event happened
if arr == [True, False, False, False]:
status = 'None'
statusInt = 0
elif arr == [True, False, False, True]:
arr = [True]*4
# Transaction COMPLETED
if statusInt != currentStatus:
currentStatus = statusInt
logger.info(tnow + ' ' + status + ' ref: %s, STATUS: %s',
np.count_nonzero(maskCB), [x*1 for x in arr])
if statusInt == 3:
record = False
lowerStatus = True
lowestStatus = True
nearlyEndedTransaction = False
arr = [True, False, False, False]
transactionCount += 1
logger.info('-----------------------------------------')
logger.info('----------TRANSACTION ENDED--------------')
logger.info('-----------------------------------------')
cv2.putText(ori, 'Transaction: ' + str(transactionCount), (40, 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2, cv2.LINE_AA)
cv2.putText(ori, str(datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")), (40, 90),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2, cv2.LINE_AA)
# Video Recording
if record:
out.write(ori)
# Warning if cashbox OPENED for more than 50 seconds
if startCount and (time.time() - start > 50):
logger.warning('Cash box opened for more than 50 seconds!')
record = False
cv2.rectangle(image_np, (args.x1_ROI,args.y1_ROI),
(args.x2_ROI,args.y2_ROI), (255, 0, 0), 3, 1)
cv2.rectangle(image_np, (args.x1_CB,args.y1_CB),
(args.x2_CB,args.y2_CB), (0, 0, 255), 3, 1)
cv2.imshow('Video Stream', cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR))
cv2.imshow('ori', ori)
if cv2.waitKey(10) == 27:
break
cap.release()
cv2.destroyAllWindows()