-
Notifications
You must be signed in to change notification settings - Fork 1
/
deep_sort.py
208 lines (176 loc) · 8.94 KB
/
deep_sort.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
import torch
import time
import cv2
import os
from operators.distributed_wrapper import DistributedWrapper
from configs.rrnet_config import Config
import numpy as np
from operators.rrnet_operator import RRNetOperator
from deep_sort import DeepSort
from utils.functional import draw_bboxes
if __name__ == '__main__':
print('***** generating bounding box!! *****')
dis_operator = DistributedWrapper(Config, RRNetOperator)
dis_operator.deep_sort()
# deepsort_echinus = DeepSort('./original_ckpt.t7')
# deepsort_holothurian = DeepSort('./original_ckpt.t7')
# deepsort_scallop = DeepSort('./original_ckpt.t7')
num = 0
print('***** start deep_sort!! *****')
# for txt in os.listdir('./results/'):
'''
for i in range(3700 ,4499 + 1):
start = time.time()
bbox_xywh_echinus = []
bbox_xywh_holothurian = []
bbox_xywh_scallop = []
txt_file = open('./results/'+ str(i) + '.txt', 'r')
img = cv2.imread('./data/2018origin/video/images/'+ str(i) + '.jpg')
im = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
for line in txt_file.readlines():
if int(line.split(',')[5]) == 2:
bbox = [float(line.split(',')[0]), float(line.split(',')[1]), float(line.split(',')[2]),
float(line.split(',')[3]), float(line.split(',')[4]), int(line.split(',')[5])]
bbox_xywh_echinus.append(bbox)
if int(line.split(',')[5]) == 1:
bbox = [float(line.split(',')[0]), float(line.split(',')[1]), float(line.split(',')[2]),
float(line.split(',')[3]), float(line.split(',')[4]), int(line.split(',')[5])]
bbox_xywh_holothurian.append(bbox)
if int(line.split(',')[5]) == 3:
bbox = [float(line.split(',')[0]), float(line.split(',')[1]), float(line.split(',')[2]),
float(line.split(',')[3]), float(line.split(',')[4]), int(line.split(',')[5])]
bbox_xywh_scallop.append(bbox)
# if bbox_xywh is not None:
if len(bbox_xywh_echinus):
bbox_xywh_echinus = np.array(bbox_xywh_echinus)
outputs = deepsort_echinus.update(bbox_xywh_echinus[:, :4], bbox_xywh_echinus[:, 5], img)
if len(outputs) > 0:
bbox_xyxy = outputs[:, :4]
identities = outputs[:, -1]
ori_im = draw_bboxes(img, bbox_xyxy, identities)
cv2.imwrite('./deepsort_echinus/'+ str(i) +'.jpg', ori_im)
else:
cv2.imwrite('./deepsort_echinus/' + str(i) + '.jpg', img)
num += 1
total = len(os.listdir('./results/'))
if len(bbox_xywh_holothurian):
bbox_xywh_holothurian = np.array(bbox_xywh_holothurian)
outputs = deepsort_holothurian.update(bbox_xywh_holothurian[:, :4], bbox_xywh_holothurian[:, 5], img)
if len(outputs) > 0:
bbox_xyxy = outputs[:, :4]
identities = outputs[:, -1]
ori_im = draw_bboxes(img, bbox_xyxy, identities)
cv2.imwrite('./deepsort_holothurian/'+ str(i) +'.jpg', ori_im)
else:
cv2.imwrite('./deepsort_holothurian/' + str(i) + '.jpg', img)
if len(bbox_xywh_scallop):
bbox_xywh_scallop = np.array(bbox_xywh_scallop)
outputs = deepsort_scallop.update(bbox_xywh_scallop[:, :4], bbox_xywh_scallop[:, 5], img)
if len(outputs) > 0:
bbox_xyxy = outputs[:, :4]
identities = outputs[:, -1]
ori_im = draw_bboxes(img, bbox_xyxy, identities)
cv2.imwrite('./deepsort_scallop/'+ str(i) +'.jpg', ori_im)
else:
cv2.imwrite('./deepsort_scallop/' + str(i) + '.jpg', img)
end = time.time()
print('[{}, {}], time: {}s, fps: {}'.format(num, total, end-start, 1/(end - start)), end='', flush=True)
'''
deepsort_echinus = DeepSort('./original_ckpt.t7')
unconfirmed_num = 0
for i in range(3700, 4499 + 1):
start = time.time()
bbox_xywh_echinus = []
txt_file = open('./results/' + str(i) + '.txt', 'r')
img = cv2.imread('./data/2018origin/video2/images/' + str(i) + '.jpg')
im = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
for line in txt_file.readlines():
if int(line.split(',')[5]) == 2:
bbox = [float(line.split(',')[0]), float(line.split(',')[1]), float(line.split(',')[2]),
float(line.split(',')[3]), float(line.split(',')[4]), int(line.split(',')[5])]
bbox_xywh_echinus.append(bbox)
# if bbox_xywh is not None:
if len(bbox_xywh_echinus):
bbox_xywh_echinus = np.array(bbox_xywh_echinus)
outputs, unconfirmed = deepsort_echinus.update(bbox_xywh_echinus[:, :4], bbox_xywh_echinus[:, 5], img)
unconfirmed_num = unconfirmed
if len(outputs) > 0:
bbox_xyxy = outputs[:, :4]
identities = outputs[:, -1]
ori_im = draw_bboxes(img, bbox_xyxy,identities=identities)
cv2.imwrite('./deepsort_echinus/' + str(i) + '.jpg', ori_im)
else:
cv2.imwrite('./deepsort_echinus/' + str(i) + '.jpg', img)
num += 1
total = len(os.listdir('./results/'))
end = time.time()
print('\r[{}, {}], time: {}s, fps: {}'.format(num, total, end - start, 1 / (end - start)), end='', flush=True)
print('echinus unconfirmed num = {}'.format(unconfirmed_num))
del deepsort_echinus
deepsort_holothurian = DeepSort('./original_ckpt.t7')
unconfirmed_num = 0
for i in range(3700 ,4499 + 1):
start = time.time()
bbox_xywh_holothurian = []
txt_file = open('./results/'+ str(i) + '.txt', 'r')
img = cv2.imread('./data/2018origin/video2/images/'+ str(i) + '.jpg')
im = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
for line in txt_file.readlines():
if int(line.split(',')[5]) == 1:
bbox = [float(line.split(',')[0]), float(line.split(',')[1]), float(line.split(',')[2]),
float(line.split(',')[3]), float(line.split(',')[4]), int(line.split(',')[5])]
bbox_xywh_holothurian.append(bbox)
num += 1
total = len(os.listdir('./results/'))
if len(bbox_xywh_holothurian):
bbox_xywh_holothurian = np.array(bbox_xywh_holothurian)
outputs, unconfirmed = deepsort_holothurian.update(bbox_xywh_holothurian[:, :4], bbox_xywh_holothurian[:, 5], img)
unconfirmed_num = unconfirmed
if len(outputs) > 0:
bbox_xyxy = outputs[:, :4]
identities = outputs[:, -1]
ori_im = draw_bboxes(img, bbox_xyxy, identities)
cv2.imwrite('./deepsort_holothurian/'+ str(i) +'.jpg', ori_im)
else:
cv2.imwrite('./deepsort_holothurian/' + str(i) + '.jpg', img)
end = time.time()
print('\r[{}, {}], time: {}s, fps: {}'.format(num, total, end-start, 1/(end - start)), end='', flush=True)
print('holothurian unconfirmed num = {}'.format(unconfirmed_num))
del deepsort_holothurian
deepsort_scallop = DeepSort('./original_ckpt.t7')
unconfirmed_num = 0
for i in range(3700 ,4499 + 1):
start = time.time()
bbox_xywh_scallop = []
txt_file = open('./results/'+ str(i) + '.txt', 'r')
img = cv2.imread('./data/2018origin/video2/images/'+ str(i) + '.jpg')
im = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
for line in txt_file.readlines():
if int(line.split(',')[5]) == 3:
bbox = [float(line.split(',')[0]), float(line.split(',')[1]), float(line.split(',')[2]),
float(line.split(',')[3]), float(line.split(',')[4]), int(line.split(',')[5])]
bbox_xywh_scallop.append(bbox)
num += 1
total = len(os.listdir('./results/'))
if len(bbox_xywh_scallop):
bbox_xywh_scallop = np.array(bbox_xywh_scallop)
outputs, unconfirmed = deepsort_scallop.update(bbox_xywh_scallop[:, :4], bbox_xywh_scallop[:, 5], img)
unconfirmed_num = unconfirmed
if len(outputs) > 0:
bbox_xyxy = outputs[:, :4]
identities = outputs[:, -1]
ori_im = draw_bboxes(img, bbox_xyxy, identities)
cv2.imwrite('./deepsort_scallop/'+ str(i) +'.jpg', ori_im)
else:
cv2.imwrite('./deepsort_scallop/' + str(i) + '.jpg', img)
end = time.time()
print('\r[{}, {}], time: {}s, fps: {}'.format(num, total, end-start, 1/(end - start)), end='', flush=True)
print('scallop unconfirmed num = {}'.format(unconfirmed_num))
del deepsort_scallop
fps = 10
fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
videowriter = cv2.VideoWriter('./echinus.avi', fourcc, fps, (768, 768))
for name in os.listdir('./deepsort_echinus/'):
image = cv2.imread('./deepsort_echinus/'+name)
videowriter.write(image)
videowriter.release()