-
Notifications
You must be signed in to change notification settings - Fork 1
/
inferenceFIDTM.py
338 lines (247 loc) · 9.86 KB
/
inferenceFIDTM.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
from __future__ import division
import warnings
from Networks.HR_Net.seg_hrnet import get_seg_model
import torch.nn as nn
import torch.nn.functional as F
from torchvision import datasets, transforms
import dataset
import math
import matplotlib.pyplot as plt
from image import *
from utils import *
from threading import Thread, Event
import logging
import nni
from nni.utils import merge_parameter
from config import return_args, args
warnings.filterwarnings('ignore')
import time
logger = logging.getLogger('mnist_AutoML')
print(args)
def generate_point_map(kpoint):
rate = 1
pred_coor = np.nonzero(kpoint)
point_map = np.zeros((int(kpoint.shape[0] * rate), int(kpoint.shape[1] * rate), 3), dtype="uint8") + 255 # 22
# count = len(pred_coor[0])
coord_list = []
for i in range(0, len(pred_coor[0])):
h = int(pred_coor[0][i] * rate)
w = int(pred_coor[1][i] * rate)
coord_list.append([w, h])
cv2.circle(point_map, (w, h), 3, (0, 0, 0), -1)
return point_map
def generate_bounding_boxes(kpoint, Img_data):
'''generate sigma'''
pts = np.array(list(zip(np.nonzero(kpoint)[1], np.nonzero(kpoint)[0])))
leafsize = 2048
if pts.shape[0] > 0: # Check if there is a human presents in the frame
# build kdtree
tree = scipy.spatial.KDTree(pts.copy(), leafsize=leafsize)
distances, locations = tree.query(pts, k=4)
for index, pt in enumerate(pts):
pt2d = np.zeros(kpoint.shape, dtype=np.float32)
pt2d[pt[1], pt[0]] = 1.
if np.sum(kpoint) > 1:
sigma = (distances[index][1] + distances[index][2] + distances[index][3]) * 0.1
else:
sigma = np.average(np.array(kpoint.shape)) / 2. / 2. # case: 1 point
sigma = min(sigma, min(Img_data.shape[0], Img_data.shape[1]) * 0.04)
if sigma < 6:
t = 2
else:
t = 2
Img_data = cv2.rectangle(Img_data, (int(pt[0] - sigma), int(pt[1] - sigma)),
(int(pt[0] + sigma), int(pt[1] + sigma)), (0, 255, 0), t)
return Img_data
def show_fidt_func(input):
input[input < 0] = 0
input = input[0][0]
fidt_map1 = input
fidt_map1 = fidt_map1 / np.max(fidt_map1) * 255
fidt_map1 = fidt_map1.astype(np.uint8)
fidt_map1 = cv2.applyColorMap(fidt_map1, 2)
return fidt_map1
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def counting(input):
input_max = torch.max(input).item()
keep = nn.functional.max_pool2d(input, (3, 3), stride=1, padding=1)
keep = (keep == input).float()
input = keep * input
input[input < 100.0 / 255.0 * torch.max(input)] = 0
input[input > 0] = 1
'''negative sample'''
if input_max<0.1:
input = input * 0
count = int(torch.sum(input).item())
kpoint = input.data.squeeze(0).squeeze(0).cpu().numpy()
return count, kpoint
transform=transforms.Compose([transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),])
img_transform = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
tensor_transform = transforms.ToTensor()
def get_prediction_webcam(event: Event):
device = torch.device('cuda')
model = get_seg_model()
model = nn.DataParallel(model, device_ids=[0])
model = model.cuda()
checkpoint = torch.load('/home/zaki/Documents/Master/Code/image/2022/FIDTM/weights/model_best_nwpu.pth')
model.load_state_dict(checkpoint['state_dict'], strict=False)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
#fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
#cap = cv2.VideoCapture(args.video_path)
#cap= cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
cap= cv2.VideoCapture(0)
ret, frame = cap.read()
print(frame.shape)
'''out video'''
width = frame.shape[1] #output size
height = frame.shape[0] #output size
out = cv2.VideoWriter('./demo.avi', fourcc, 30, (width, height))
while True:
try:
ret, frame = cap.read()
scale_factor = 0.5
frame = cv2.resize(frame, (0, 0), fx=scale_factor, fy=scale_factor)
ori_img = frame.copy()
except:
print("test end")
cap.release()
break
frame = frame.copy()
image = tensor_transform(frame)
image = img_transform(image).unsqueeze(0)
with torch.no_grad():
d6 = model(image)
count, pred_kpoint = counting(d6)
point_map = generate_point_map(pred_kpoint)
box_img = generate_bounding_boxes(pred_kpoint, frame)
show_fidt = show_fidt_func(d6.data.cpu().numpy())
#res = np.hstack((ori_img, show_fidt, point_map, box_img))
res1 = np.hstack((ori_img, show_fidt))
res2 = np.hstack((box_img, point_map))
res = np.vstack((res1, res2))
cv2.putText(res, "Count:" + str(count), (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
res = cv2.resize(res, (1500,720))
cv2.imwrite('./demo.jpg', res)
'''write in out_video'''
out.write(res)
cv2.imshow("dst",res)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
print("pred:%.3f" % count)
if event.is_set():
break
cv2.destroyAllWindows()
def gstreamer_pipeline(
capture_width=1280,
capture_height=720,
display_width=1280,
display_height=720,
framerate=60,
flip_method=0,
):
return (
"nvarguscamerasrc ! "
"video/x-raw(memory:NVMM), "
"width=(int)%d, height=(int)%d, "
"format=(string)NV12, framerate=(fraction)%d/1 ! "
"nvvidconv flip-method=%d ! "
"video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
"videoconvert ! "
"video/x-raw, format=(string)BGR ! appsink"
% (
capture_width,
capture_height,
framerate,
flip_method,
display_width,
display_height,
)
)
def get_prediction(file):
device = torch.device('cuda')
model = get_seg_model()
model = nn.DataParallel(model, device_ids=[0])
model = model.cuda()
checkpoint = torch.load('/home/zaki/Documents/Master/Code/image/2022/FIDTM/weights/model_best_nwpu.pth')
model.load_state_dict(checkpoint['state_dict'], strict=False)
# set your image path here
print("------------------------------------")
print(file)
if (file.endswith(".mp4")):
fourcc = cv2.VideoWriter_fourcc(*'XVID')
#fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
cap = cv2.VideoCapture(file)
ret, frame = cap.read()
print(frame.shape)
'''out video'''
width = frame.shape[1] #output size
height = frame.shape[0] #output size
out = cv2.VideoWriter('./demo.avi', fourcc, 30, (width, height))
while True:
try:
ret, frame = cap.read()
scale_factor = 0.5
frame = cv2.resize(frame, (0, 0), fx=scale_factor, fy=scale_factor)
ori_img = frame.copy()
except:
print("test end")
cap.release()
break
frame = frame.copy()
image = tensor_transform(frame)
image = img_transform(image).unsqueeze(0)
image = image.to(device)
with torch.no_grad():
d6 = model(image)
count, pred_kpoint = counting(d6)
point_map = generate_point_map(pred_kpoint)
box_img = generate_bounding_boxes(pred_kpoint, frame)
show_fidt = show_fidt_func(d6.data.cpu().numpy())
#res = np.hstack((ori_img, show_fidt, point_map, box_img))
res1 = np.hstack((ori_img, show_fidt))
res2 = np.hstack((box_img, point_map))
res = np.vstack((res1, res2))
cv2.putText(res, "Count:" + str(count), (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow("dst",res)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else :
frame = cv2.imread(file)
print(frame.shape)
scale_factor = 0.5
frame = cv2.resize(frame, (0, 0), fx=scale_factor, fy=scale_factor)
ori_img = frame.copy()
frame = frame.copy()
image = tensor_transform(frame)
image = img_transform(image).unsqueeze(0)
with torch.no_grad():
d6 = model(image)
count, pred_kpoint = counting(d6)
point_map = generate_point_map(pred_kpoint)
box_img = generate_bounding_boxes(pred_kpoint, frame)
show_fidt = show_fidt_func(d6.data.cpu().numpy())
#res = np.hstack((ori_img, show_fidt, point_map, box_img))
res1 = np.hstack((ori_img, show_fidt))
res2 = np.hstack((box_img, point_map))
res = np.vstack((res1, res2))
#cv2.putText(res, "Count:" + str(count), (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
density = './density_map.jpg'
cv2.imwrite('./demo.jpg', res)
print("pred:%.3f" % count)
x = random.randint(1,100000)
density = 'static/density_map'+str(x)+'.jpg'
plt.imsave(density,show_fidt)
return count , density