forked from changkevin51/Object-Detection-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
354 lines (271 loc) · 12.2 KB
/
utils.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import tensorflow as tf
import matplotlib.pyplot as plt
import os
from PIL import Image
import argparse
import numpy as np
from keras.layers import Conv2D, Input, BatchNormalization, LeakyReLU, ZeroPadding2D, UpSampling2D
from keras.layers import concatenate, add
from keras.models import Model
import struct
import cv2
from copy import deepcopy
import tempfile
import subprocess
import streamlit as st
import time
anchors = [[[116,90], [156,198], [373,326]], [[30,61], [62,45], [59,119]], [[10,13], [16,30], [33,23]]]
DATA_ROOT = './'
model_path = os.path.join(DATA_ROOT, 'yolo.h5')
darknet = tf.keras.models.load_model(model_path, compile=False)
labels = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", \
"boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", \
"bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", \
"backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", \
"sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", \
"tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", \
"apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", \
"chair", "sofa", "pottedplant", "bed", "diningtable", "toilet", "tvmonitor", "laptop", "mouse", \
"remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", \
"book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"]
class BoundBox:
def __init__(self, xmin, ymin, xmax, ymax, objness = None, classes = None):
self.xmin = xmin
self.ymin = ymin
self.xmax = xmax
self.ymax = ymax
self.objness = objness
self.classes = classes
self.label = -1
self.score = -1
def get_label(self):
if self.label == -1:
self.label = np.argmax(self.classes)
return self.label
def get_score(self):
if self.score == -1:
self.score = self.classes[self.get_label()]
return self.score
def _interval_overlap(interval_a, interval_b):
x1, x2 = interval_a
x3, x4 = interval_b
if x3 < x1:
if x4 < x1:
return 0
else:
return min(x2,x4) - x1
else:
if x2 < x3:
return 0
else:
return min(x2,x4) - x3
def _sigmoid(x):
return 1. / (1. + np.exp(-x))
def bbox_iou(box1, box2):
intersect_w = _interval_overlap([box1.xmin, box1.xmax], [box2.xmin, box2.xmax])
intersect_h = _interval_overlap([box1.ymin, box1.ymax], [box2.ymin, box2.ymax])
intersect = intersect_w * intersect_h
w1, h1 = box1.xmax-box1.xmin, box1.ymax-box1.ymin
w2, h2 = box2.xmax-box2.xmin, box2.ymax-box2.ymin
union = w1*h1 + w2*h2 - intersect
if union == 0:
return 0
return float(intersect) / union
def preprocess_input(image_pil, net_h, net_w):
image = np.asarray(image_pil)
# Remove the alpha channel if it exists
if image.shape[2] == 4:
image = image[:, :, :3]
new_h, new_w, _ = image.shape
if (float(net_w)/new_w) < (float(net_h)/new_h):
new_h = (new_h * net_w)/new_w
new_w = net_w
else:
new_w = (new_w * net_h)/new_h
new_h = net_h
new_w = int(new_w)
new_h = int(new_h)
resized = cv2.resize(image/255., (int(new_w), int(new_h)))
new_image = np.ones((net_h, net_w, 3)) * 0.5
new_image[int((net_h-new_h)//2):int((net_h+new_h)//2), int((net_w-new_w)//2):int((net_w+new_w)//2), :] = resized
new_image = np.expand_dims(new_image, 0)
return new_image
def decode_netout(netout_, obj_thresh, anchors_, image_h, image_w, net_h, net_w):
netout_all = deepcopy(netout_)
boxes_all = []
for i in range(len(netout_all)):
netout = netout_all[i][0]
anchors = anchors_[i]
grid_h, grid_w = netout.shape[:2]
nb_box = 3
netout = netout.reshape((grid_h, grid_w, nb_box, -1))
nb_class = netout.shape[-1] - 5
boxes = []
netout[..., :2] = _sigmoid(netout[..., :2])
netout[..., 4:] = _sigmoid(netout[..., 4:])
netout[..., 5:] = netout[..., 4][..., np.newaxis] * netout[..., 5:]
netout[..., 5:] *= netout[..., 5:] > obj_thresh
for i in range(grid_h*grid_w):
row = i // grid_w
col = i % grid_w
for b in range(nb_box):
# 4th element is objectness score
objectness = netout[row][col][b][4]
#objectness = netout[..., :4]
# last elements are class probabilities
classes = netout[row][col][b][5:]
if((classes <= obj_thresh).all()): continue
# first 4 elements are x, y, w, and h
x, y, w, h = netout[row][col][b][:4]
x = (col + x) / grid_w # center position, unit: image width
y = (row + y) / grid_h # center position, unit: image height
w = anchors[b][0] * np.exp(w) / net_w # unit: image width
h = anchors[b][1] * np.exp(h) / net_h # unit: image height
box = BoundBox(x-w/2, y-h/2, x+w/2, y+h/2, objectness, classes)
#box = BoundBox(x-w/2, y-h/2, x+w/2, y+h/2, None, classes)
boxes.append(box)
boxes_all += boxes
# Correct boxes
boxes_all = correct_yolo_boxes(boxes_all, image_h, image_w, net_h, net_w)
return boxes_all
def correct_yolo_boxes(boxes_, image_h, image_w, net_h, net_w):
boxes = deepcopy(boxes_)
if (float(net_w)/image_w) < (float(net_h)/image_h):
new_w = net_w
new_h = (image_h*net_w)/image_w
else:
new_h = net_w
new_w = (image_w*net_h)/image_h
for i in range(len(boxes)):
x_offset, x_scale = (net_w - new_w)/2./net_w, float(new_w)/net_w
y_offset, y_scale = (net_h - new_h)/2./net_h, float(new_h)/net_h
boxes[i].xmin = int((boxes[i].xmin - x_offset) / x_scale * image_w)
boxes[i].xmax = int((boxes[i].xmax - x_offset) / x_scale * image_w)
boxes[i].ymin = int((boxes[i].ymin - y_offset) / y_scale * image_h)
boxes[i].ymax = int((boxes[i].ymax - y_offset) / y_scale * image_h)
return boxes
def do_nms(boxes_, nms_thresh, obj_thresh):
boxes = deepcopy(boxes_)
if len(boxes) > 0:
num_class = len(boxes[0].classes)
else:
return
for c in range(num_class):
sorted_indices = np.argsort([-box.classes[c] for box in boxes])
for i in range(len(sorted_indices)):
index_i = sorted_indices[i]
if boxes[index_i].classes[c] == 0: continue
for j in range(i+1, len(sorted_indices)):
index_j = sorted_indices[j]
if bbox_iou(boxes[index_i], boxes[index_j]) >= nms_thresh:
boxes[index_j].classes[c] = 0
new_boxes = []
for box in boxes:
label = -1
for i in range(num_class):
if box.classes[i] > obj_thresh:
label = i
# print("{}: {}, ({}, {})".format(labels[i], box.classes[i]*100, box.xmin, box.ymin))
box.label = label
box.score = box.classes[i]
new_boxes.append(box)
return new_boxes
from PIL import ImageDraw, ImageFont
import colorsys
def draw_boxes(image_, boxes, labels):
image = image_.copy()
image_w, image_h = image.size
# Load the new font (Open Sans ExtraBold)
try:
font_path = "C:\\Users\chang\\Documents\\Python_Projects\\Object_Detection\\.conda\\fonts\\open-fonts\\OpenSans-SemiBold.ttf"
font = ImageFont.truetype(font=font_path, size=np.floor(3e-2 * image_h + 0.5).astype('int32'))
except IOError:
print("Font not found. Using default font.")
font = ImageFont.load_default() # Fallback to default if font not found
thickness = (image_w + image_h) // 300
# Generate colors for drawing bounding boxes
hsv_tuples = [(x / len(labels), 1., 1.) for x in range(len(labels))]
colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors))
np.random.seed(10101) # Fixed seed for consistent colors across runs
np.random.shuffle(colors) # Shuffle colors to decorrelate adjacent classes
np.random.seed(None) # Reset seed to default
for i, box in reversed(list(enumerate(boxes))):
c = box.get_label()
predicted_class = labels[c]
score = box.get_score()
top, left, bottom, right = box.ymin, box.xmin, box.ymax, box.xmax
label = '{} {:.2f}'.format(predicted_class, score)
draw = ImageDraw.Draw(image)
# Calculate label size with the chosen font
label_size = draw.textbbox((0, 0), label, font)
label_size = (label_size[2], label_size[3])
top = max(0, np.floor(top + 0.5).astype('int32'))
left = max(0, np.floor(left + 0.5).astype('int32'))
bottom = min(image_h, np.floor(bottom + 0.5).astype('int32'))
right = min(image_w, np.floor(right + 0.5).astype('int32'))
print(label, (left, top), (right, bottom))
if top - label_size[1] >= 0:
text_origin = np.array([left, top - label_size[1]])
else:
text_origin = np.array([left, top + 1])
# Draw bounding boxes and labels
for i in range(thickness):
draw.rectangle([left + i, top + i, right - i, bottom - i], outline=colors[c])
draw.rectangle([tuple(text_origin), tuple(text_origin + label_size)], fill=colors[c])
draw.text(text_origin, label, fill=(0, 0, 0), font=font)
del draw
return image
def detect_image(image_pil, obj_thresh=0.4, nms_thresh=0.45, darknet=darknet, net_h=416, net_w=416, anchors=anchors, labels=labels):
# Preprocessing
image_w, image_h = image_pil.size
new_image = preprocess_input(image_pil, net_h, net_w)
# DarkNet Prediction
yolo_outputs = darknet.predict(new_image)
# Decode the output of the network
boxes = decode_netout(yolo_outputs, obj_thresh, anchors, image_h, image_w, net_h, net_w)
# Suppress non-maximal boxes
boxes = do_nms(boxes, nms_thresh, obj_thresh)
# If no boxes are detected, return a message
if not boxes:
return "No objects related to transportation detected"
# Draw bounding boxes on the image using labels
image_detect = draw_boxes(image_pil, boxes, labels)
return image_detect
def detect_video(video_path, output_path, obj_thresh=0.4, nms_thresh=0.45, darknet=darknet, net_h=416, net_w=416, anchors=anchors, labels=labels):
vid = cv2.VideoCapture(video_path)
if not vid.isOpened():
raise IOError("Couldn't open webcam or video")
video_FourCC = cv2.VideoWriter_fourcc(*'mp4v')
total_frames = int(vid.get(cv2.CAP_PROP_FRAME_COUNT))
video_fps = vid.get(cv2.CAP_PROP_FPS)
video_size = (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)))
out = cv2.VideoWriter(output_path, video_FourCC, video_fps, video_size)
num_frame = 0
progress_text = "Processing video frames. Please wait."
my_bar = st.progress(0, text=progress_text)
start_time = time.time()
while vid.isOpened():
ret, frame = vid.read()
num_frame += 1
print("=== Frame {} ===".format(num_frame))
if ret:
### YOUR CODE HERE
image_pil = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
result_pil = detect_image(image_pil, obj_thresh, nms_thresh, darknet, net_h, net_w, anchors, labels)
new_frame = cv2.cvtColor(np.asarray(result_pil), cv2.COLOR_RGB2BGR)
### END CODE
out.write(new_frame)
if time.time() - start_time >= 1:
progress = int((num_frame / total_frames) * 100)
my_bar.progress(progress, text=f"{progress}% completed: Detecting frame {num_frame}/{total_frames}")
start_time = time.time() # Reset the timer
else:
break
my_bar.progress(100, text="Processing complete: All frames processed.")
time.sleep(1)
my_bar.empty()
vid.release()
out.release()
return output_path