-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapstone_video.py
304 lines (241 loc) · 10 KB
/
capstone_video.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
292
293
294
295
296
297
298
299
300
301
302
303
304
# import os
from timeit import default_timer as timer
import os
import json
import urllib.request as urllib2 # VoIP camera
import logging
import logging.config
import numpy as np
import cv2
import tensorflow as tf
from keras import backend as K
from keras.models import load_model
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
from XAI.keras_loss_function.keras_ssd_loss import SSDLoss
from XAI.keras_layers.keras_layer_AnchorBoxes import AnchorBoxes
from XAI.keras_layers.keras_layer_DecodeDetections import DecodeDetections
from XAI.keras_layers.keras_layer_L2Normalization import L2Normalization
from XAI.ssd_encoder_decoder.ssd_output_decoder import decode_detections_fast
from utils.utils import configure_icons
from db_utils.Xaidb import Xaidb
from centroidtracker.centroidtracker import CentroidTracker
# TF debug
# 0 = all messages are logged (default behavior)
# 1 = INFO messages are not printed
# 2 = INFO and WARNING messages are not printed
# 3 = INFO, WARNING, and ERROR messages are not printed
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
tf.Session(config=tf.ConfigProto(log_device_placement=True))
# GLOBALS
db = None
ICONS = {}
def setup_log(log_config):
'''Setup logging
'''
with open(log_config, encoding='utf-8-sig') as conf_file:
jc = json.load(conf_file)
logging.config.dictConfig(jc["main_app"])
def calculate_fps(accum_time, curr_fps, prev_time, fps, image):
'''Calculate current FPS
'''
curr_time = timer()
exec_time = curr_time - prev_time
prev_time = curr_time
accum_time = accum_time + exec_time
curr_fps = curr_fps + 1
if accum_time > 1:
accum_time = accum_time - 1
fps = "FPS: " + str(curr_fps)
curr_fps = 0
# Draw FPS in top left corner
cv2.rectangle(image, (0, 0), (50, 17), (255, 255, 255), -1)
cv2.putText(image, fps, (3, 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 0), 1)
return accum_time, curr_fps, prev_time, fps
def get_color(classes):
# TODO: implement through DB
# ["background", "cyclist", "risk"],
# BGR colors
BACKGROUND = (0, 0, 0)
DANGER = (0, 0, 255) # RED
WARNING = (0, 128, 255) # ORANGE
CAUTION = (0, 255, 255) # YELLOW
NO_IMMEDIATE_DANGER = (255, 0, 0) # BLUE
return [(0, 0, 0), DANGER, WARNING]
def load_ssd_model(model_path="SSD_MODEL.h5"):
'''Load Pretrained SSD model
'''
logging.info("Loading SSD model [%s]", model_path)
# We need to create an SSDLoss object in order to pass that to the model loader.
ssd_loss = SSDLoss(neg_pos_ratio=3, n_neg_min=0, alpha=1.0)
K.clear_session() # Clear previous models from memory.
model = load_model(model_path, custom_objects={'AnchorBoxes': AnchorBoxes,
'L2Normalization': L2Normalization,
'DecodeDetections': DecodeDetections,
'compute_loss': ssd_loss.compute_loss})
return model
def get_prediction(model, frame, confidence_thresh, iou_threshold, img_height=300, img_width=300):
'''Get model prediction
return decoded prediction
'''
height, width, _ = frame.shape
img = cv2.resize(frame, (img_height, img_width))
inp_img = [image.img_to_array(img)]
tmp_inp = np.array(inp_img)
preprocessed_inp = preprocess_input(tmp_inp)
prediction = model.predict(preprocessed_inp)
return decode_detections_fast(prediction,
confidence_thresh=confidence_thresh,
iou_threshold=iou_threshold,
top_k=200,
normalize_coords=True,
img_height=height,
img_width=width)
def transparentOverlay(src, overlay, pos=(0, 0), scale=1):
"""
:param src: Input Color Background Image
:param overlay: transparent Image (BGRA)
:param pos: position where the image to be blit.
:param scale : scale factor of transparent image.
:return: Resultant Image
"""
overlay = cv2.resize(overlay, (0, 0), fx=scale, fy=scale)
h, w, _ = overlay.shape # Size of foreground
rows, cols, _ = src.shape # Size of background Image
y, x = pos[0], pos[1] # Position of foreground/overlay image
# loop over all pixels and apply the blending equation
for i in range(h):
for j in range(w):
if x+i >= rows or y+j >= cols:
continue
alpha = float(overlay[i][j][3]/255.0) # read the alpha channel
src[x+i][y+j] = alpha*overlay[i][j][:3]+(1-alpha)*src[x+i][y+j]
return src
def draw_objects(prediction, frame, classes):
'''Draw objects in image frame
'''
class_colors = [[0, 0, 0], [0, 128, 255], [0, 0, 255]]
height, width, _ = frame.shape
boxes = []
# Low, Medium, High, Extreme
classes = ['low', 'medium', 'high']
# Blending the images with 0.3 and 0.7
for obj in prediction[0]:
# Transform the predicted bounding boxes for the 300x300 image to the original image dimensions.
# [0]class/risk [1]conf [2]xmin [3]ymin [4]xmax [5]ymax [6]distance [7] ratio in screen
# RAW: int(round(obj[2] * width / img_width))
xmin = int(round(obj[2]))
ymin = int(round(obj[3]))
xmax = int(round(obj[4]))
ymax = int(round(obj[5]))
risk = analyse_risk(obj[0], obj[6], obj[7])
icon = ICONS[risk]
color = db.get_signal(risk)
boxes.append([xmin, ymin, xmax, ymax]) # record obj box
logging.debug("Class[%s] Conf[%.2f] xmin[%d] ymin[%d] xmax[%d] ymax[%d]", risk, obj[1], xmin, ymin, xmax, ymax)
logging.debug("Distance [%f] Portion[%f]", obj[6], obj[7])
conf = float("{:.2f}".format(obj[1]))
label = '{}: {:.2f}'.format(
risk, obj[1])
cv2.rectangle(frame, (xmin, ymin), (xmax, ymax),
color, 3)
text_top = (xmin, ymin-10)
text_bot = (xmin + 80, ymin + 5)
text_pos = (xmin + 5, ymin)
cv2.rectangle(frame, text_top, text_bot, color, -1)
cv2.putText(frame, label, text_pos,
cv2.FONT_HERSHEY_SIMPLEX, 0.40, (0, 0, 0), 1)
if risk == 'low':
continue
x_offset = xmax - 50
y_offset = ymin - 50
frame = transparentOverlay(frame, icon, (x_offset, y_offset))
return boxes
def take_action():
raise NotImplementedError("Stub")
def analyse_risk(id_class, distance, ratio, speed=50):
# Low, Medium, High, Extreme
classes = ['low', 'medium', 'high']
# [0]class [1]conf [2]xmin [3]ymin [4]xmax [5]ymax [6]distance [7] ratio in screen
risk = 'medium'
if speed == 50:
stp_dist = 300
if stp_dist > distance:
return 'high'
if float(ratio) > 0.20:
return 'high'
return risk
def process_video(model, config, video_path=0, skip=1):
'''Process video or camera stream
video_path: file or (0 or -1) for video stream
'''
confidence_thresh = config["confidence_thresh"]
iou_threshold = config["iou_threshold"]
img_height = config["img_height"]
img_width = config["img_width"]
class_labels = config["labels"]
# Setup values to calculate FPS
accum_time, curr_fps = 0, 0
fps = "FPS: ??"
prev_time = timer()
vid = cv2.VideoCapture(video_path)
if not vid.isOpened():
raise IOError("Couldn't open webcam or video")
logging.info("Starting Video Stream")
# ==============================
# Multi tracker init
# trackers = cv2.MultiTracker_create()
ct = CentroidTracker()
# ==================================
# vid.set(1, 100) # Skip first few frames cause they are garbage
while True:
return_value, frame = vid.read()
# frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5) # resize both axis by half
if not return_value:
break
frame = cv2.resize(frame, (1200, 900))
if int(vid.get(cv2.CAP_PROP_POS_FRAMES)) % skip == 0: # every 3 frames
predictions = get_prediction(
model, frame, confidence_thresh, iou_threshold, img_height, img_width)
# TODO: implement tracking
boxes = draw_objects(predictions, frame, class_labels)
# get centroid point of box
# cX = int((startX + endX) / 2.0)
# cY = int((startY + endY) / 2.0)
# objects = ct.update(boxes)
# loop over the tracked objects
# for (objectID, centroid) in objects.items():
# # draw both the ID of the object and the centroid of the
# # object on the output frame
# text = "ID {}".format(objectID)
# cv2.putText(frame, text, (centroid[0] - 10, centroid[1] - 10),
# cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# logging.debug(centroid)
# cv2.circle(
# frame, (centroid[0], centroid[1]), 4, (0, 255, 0), -1)
# Calculate and draw FPS
accum_time, curr_fps, prev_time, fps = calculate_fps(
accum_time, curr_fps, prev_time, fps, frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
cv2.imshow("SSD results", frame)
vid.release()
cv2.destroyAllWindows()
def main(log_config="configuration/log_config.json", main_config="configuration/config.json"):
""" Main Function
"""
global db
setup_log(log_config)
config = {}
with open(main_config, encoding='utf-8-sig') as conf_file:
config = json.load(conf_file)
model = load_ssd_model(config["model_processing"]["file"])
# init database and icons
db = Xaidb(config["database"]["name"])
configure_icons(db, ICONS, config["icons_dimensions"])
# process_video(model, config["model_processing"])
process_video(model, config["model_processing"], config["video_path"])
if __name__ == "__main__":
main()