-
Notifications
You must be signed in to change notification settings - Fork 18
/
inference_superpixel.py
373 lines (300 loc) · 12 KB
/
inference_superpixel.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
##########################################################################
# Example : perform live fire detection in image/video/webcam using superpixel localisation
# and the superpixel trained version of the NasNet-A-OnFire,
# ShuffleNetV2-OnFire CNN models.
# Copyright (c) 2020/21 - William Thompson / Neelanjan Bhowmik / Toby
# Breckon, Durham University, UK
# License :
# https://github.com/NeelBhowmik/efficient-compact-fire-detection-cnn/blob/main/LICENSE
##########################################################################
import cv2
import os
import sys
import math
from PIL import Image
import argparse
import time
import numpy as np
##########################################################################
import torch
import torchvision.transforms as transforms
from models import shufflenetv2
from models import nasnet_mobile_onfire
##########################################################################
def data_transform(model):
# transforms needed for shufflenetonfire
if model == 'shufflenetonfire':
np_transforms = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
# transforms needed for nasnetonfire
if model == 'nasnetonfire':
np_transforms = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
return np_transforms
##########################################################################
# read/process superpixel
def proc_sp(small_frame, np_transforms):
# small_frame = cv2.resize(frame, (224, 224), cv2.INTER_AREA)
small_frame = Image.fromarray(small_frame)
small_frame = np_transforms(small_frame).float()
small_frame = small_frame.unsqueeze(0)
small_frame = small_frame.to(device)
return small_frame
##########################################################################
def pil_crop(img):
sp_pil = Image.fromarray(img)
imageBox = sp_pil.getbbox()
sp_crop = sp_pil.crop(imageBox)
np_sp_crop = np.array(sp_crop)
# sp_crop_cv = cv2.cvtColor(np_sp_crop, cv2.COLOR_RGB2BGR)
return np_sp_crop
##########################################################################
# model prediction on superpixel
def run_model_img(args, frame, model):
output = model(frame)[0]
pred = torch.round(torch.sigmoid(output))
return pred
##########################################################################
# drawing prediction on image
def draw_pred(args, frame, contours, prediction):
# height, width, _ = frame.shape
if prediction == 1:
cv2.drawContours(frame, contours, -1, (0, 0, 255), 1)
else:
cv2.drawContours(frame, contours, -1, (0, 255, 0), 1)
return frame
##########################################################################
def process_sp(args, small_frame, np_transforms, model):
# apply SLIC superpixel
slic = cv2.ximgproc.createSuperpixelSLIC(small_frame, region_size=22)
slic.iterate(10)
segments = slic.getLabels()
for (i, segVal) in enumerate(np.unique(segments)):
mask = np.zeros(small_frame.shape[:2], dtype='uint8')
mask[segments == segVal] = 255
if (int(cv2.__version__.split(".")[0]) >= 4):
contours, hierarchy = cv2.findContours(
mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
else:
im2, contours, hierarchy = cv2.findContours(
mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# contour_list.append(contours)
superpixel = cv2.bitwise_and(small_frame, small_frame, mask=mask)
superpixel = cv2.cvtColor(superpixel, cv2.COLOR_BGR2RGB)
# PIL centre crop and data transformation
# superpixel = pil_crop(superpixel)
superpixel = proc_sp(superpixel, np_transforms)
# model prediction
prediction = run_model_img(args, superpixel, model)
# draw prediction on superpixel
draw_pred(args, small_frame, contours, prediction)
##########################################################################
# parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("--image",
help="Path to image file or image directory")
parser.add_argument("--video",
help="Path to video file or video directory")
parser.add_argument(
"--webcam",
action="store_true",
help="Take inputs from webcam")
parser.add_argument(
"--camera_to_use",
type=int,
default=0,
help="Specify camera to use for webcam option")
parser.add_argument("--trt",
action="store_true",
help="Model run on TensorRT")
parser.add_argument(
"--model",
default='shufflenetonfire',
help="Select the model {shufflenetonfire, nasnetonfire}")
parser.add_argument("--weight", help="Model weight file path")
parser.add_argument(
"--cpu",
action="store_true",
help="If selected will run on CPU")
parser.add_argument(
"--output",
help="A directory to save output visualizations."
"If not given , will show output in an OpenCV window.")
parser.add_argument(
"-fs",
"--fullscreen",
action='store_true',
help="run in full screen mode")
args = parser.parse_args()
print(f'\n{args}')
##########################################################################
# define display window name
WINDOW_NAME = 'Detection'
# uses cuda if available
if args.cpu:
device = torch.device('cpu')
else:
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
if args.cpu and args.trt:
print(f'\n>>>>TensorRT runs only on gpu. Exit.')
exit()
print('\n\nBegin {fire, no-fire} superpixel localisation :')
# model load
if args.model == "shufflenetonfire":
model = shufflenetv2.shufflenet_v2_x0_5(
pretrained=False, layers=[
4, 8, 4], output_channels=[
24, 48, 96, 192, 64], num_classes=1)
if args.weight:
w_path = args.weight
else:
w_path = './weights/shufflenet_sp.pt'
model.load_state_dict(torch.load(w_path, map_location=device))
elif args.model == "nasnetonfire":
model = nasnet_mobile_onfire.nasnetamobile(num_classes=1, pretrained=False)
if args.weight:
w_path = args.weight
else:
w_path = './weights/nasnet_sp.pt'
model.load_state_dict(torch.load(w_path, map_location=device))
else:
print('Invalid Model.')
exit()
# apply data transform
np_transforms = data_transform(args.model)
print(f'|__Model loading: {args.model}')
model.eval()
model.to(device)
# TensorRT conversion
if args.trt:
from torch2trt import TRTModule
from torch2trt import torch2trt
data = torch.randn((1, 3, 224, 224)).float().to(device)
model_trt = torch2trt(model, [data], int8_mode=True)
model_trt.to(device)
print(f'\t|__TensorRT activated.')
# load and process input image directory or image file
if args.image:
# list image from a directory or file
if os.path.isdir(args.image):
lst_img = [os.path.join(args.image, file)
for file in os.listdir(args.image)]
if os.path.isfile(args.image):
lst_img = [args.image]
if args.output:
os.makedirs(args.output, exist_ok=True)
fps = []
# start processing image
for im in lst_img:
print('\t|____Image processing: ', im)
frame = cv2.imread(im)
height, width, _ = frame.shape
small_frame = cv2.resize(frame, (224, 224), cv2.INTER_AREA)
# Prediction on superpixel
if args.trt:
process_sp(args, small_frame, np_transforms, model_trt)
else:
process_sp(args, small_frame, np_transforms, model)
small_frame = cv2.resize(small_frame, (width, height), cv2.INTER_AREA)
# save prdiction visualisation in output path
if args.output:
f_name = os.path.basename(im)
cv2.imwrite(f'{args.output}/{f_name}', small_frame)
# display prdiction if output path is not provided
# press space key to continue/next
else:
cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)
cv2.imshow(WINDOW_NAME, small_frame)
cv2.waitKey(0)
# load and process input video file or webcam stream
if args.video or args.webcam:
# define video capture object
try:
# to use a non-buffered camera stream (via a separate thread)
if not(args.video):
from models import camera_stream
cap = camera_stream.CameraVideoStream()
else:
cap = cv2.VideoCapture() # not needed for video files
except BaseException:
# if not then just use OpenCV default
print("INFO: camera_stream class not found - camera input may be buffered")
cap = cv2.VideoCapture()
if args.output:
os.makedirs(args.output, exist_ok=True)
else:
cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)
if args.video:
if os.path.isdir(args.video):
lst_vid = [os.path.join(args.video, file)
for file in os.listdir(args.video)]
if os.path.isfile(args.video):
lst_vid = [args.video]
if args.webcam:
lst_vid = [args.camera_to_use]
# read from video file(s) or webcam
for vid in lst_vid:
keepProcessing = True
if args.video:
print('\t|____Video processing: ', vid)
if args.webcam:
print('\t|____Webcam processing: ')
if cap.open(vid):
# get video information
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
if args.output and args.video:
f_name = os.path.basename(vid)
out = cv2.VideoWriter(
filename=f'{args.output}/{f_name}',
fourcc=cv2.VideoWriter_fourcc(*'mp4v'),
fps=float(fps),
frameSize=(width, height),
isColor=True,
)
while (keepProcessing):
# start a timer (to see how long processing and display takes)
start_tik = cv2.getTickCount()
# if camera/video file successfully open then read frame
if (cap.isOpened):
ret, frame = cap.read()
# when we reach the end of the video (file) exit cleanly
if (ret == 0):
keepProcessing = False
continue
small_frame = cv2.resize(frame, (224, 224), cv2.INTER_AREA)
# Prediction on superpixel
if args.trt:
process_sp(args, small_frame, np_transforms, model_trt)
else:
process_sp(args, small_frame, np_transforms, model)
small_frame = cv2.resize(
small_frame, (width, height), cv2.INTER_AREA)
# save prdiction visualisation in output path
# only for video input, not for webcam input
if args.output and args.video:
out.write(small_frame)
# display prdiction if output path is not provided
else:
cv2.imshow(WINDOW_NAME, small_frame)
cv2.setWindowProperty(WINDOW_NAME, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN & args.fullscreen)
stop_tik = ((cv2.getTickCount() - start_tik) /
cv2.getTickFrequency()) * 1000
key = cv2.waitKey(
max(2, 40 - int(math.ceil(stop_tik)))) & 0xFF
# press "x" for exit / press "f" for fullscreen
if (key == ord('x')):
keepProcessing = False
elif (key == ord('f')):
args.fullscreen = not(args.fullscreen)
if args.output and args.video:
out.release()
else:
cv2.destroyAllWindows()
print('\n[Done]\n')