-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
143 lines (119 loc) · 4.21 KB
/
Copy pathrun.py
File metadata and controls
143 lines (119 loc) · 4.21 KB
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
import cv2
import numpy as np
from pathlib import Path
import yaml
from types import SimpleNamespace
from boxmot import DeepOCSORT, StrongSORT
from boxmot import create_tracker, get_tracker_config
import os
import glob
import time
tracker = StrongSORT(
Path('osnet_x0_25_msmt17.pt'),
"cpu",
False,
max_age=800,
)
video_path = "/home/srihari/tcs/data/mango_videos/GOPR1731_reconstruction/images"
num_images = 844
detections_path = "/home/srihari/tcs/data/mango_videos/GOPR1731_reconstruction/detections.txt"
save_results_file = "/home/srihari/tcs/data/mango_videos/GOPR1731_reconstruction/localizesort_plus.txt"
wcs_path = "/home/srihari/tcs/data/mango_videos/GOPR1731_reconstruction/wcs.txt"
color = (255, 255, 255) # BGR
thickness = 2
fontscale = 0.5
mango_detections = []
with open(detections_path, 'r') as f:
for i in f.readlines():
if i.strip() == '':
mango_detections.append([])
continue
bboxes = i.strip().split(';')
bboxes = [[float(x) for x in y.split(',')] for y in bboxes]
mango_detections.append(bboxes)
wcs = []
with open(wcs_path, 'r') as f:
for i in f.readlines():
if i.strip() == '':
wcs.append([])
continue
bboxes = i.strip().split(';')
bboxes = [[float(x) for x in y.split(',')] for y in bboxes]
wcs.append(bboxes)
def file_sorter(x):
x = x.strip().split('/')[-1]
return float(x[5:-4])
# def file_sorter(x):
# x = x.strip().split('/')[-1]
# x = x[:-4].split('_')
# x = float(x[1])+0.1*float(x[2])
# return x
files = sorted(glob.glob(os.path.join(video_path, '*.*')), key=file_sorter)
gidx = []
result_file = open(save_results_file, 'w')
start = time.time()
req_frames = [y for y in range(25)] + [y for y in range(740,760)]
# for _, img_file in enumerate(files[:]):
for frame_counter in req_frames:
img_file = files[frame_counter]
print("Frame:", frame_counter+1)
im = cv2.imread(img_file)
bboxes_ = mango_detections[frame_counter]
wc = wcs[frame_counter]
# good_idxs = []
# for idx, tmp in enumerate(wc):
# if tmp[0] != -1:
# good_idxs.append(idx)
# bboxes_ = [bboxes_[x] for x in good_idxs]
# wcs = [wcs[x] for x in good_idxs]
# bboxes_.sort(key=lambda x:x[0])
for i in bboxes_:
i.append(1)
i.append(0)
dets = np.array(bboxes_)
if len(bboxes_) == 0:
dets = np.array([[0.,0.,1.,1.,0.,0]])
wc = np.array([[0,0,0]])
tracks = tracker.update(dets, im, wc)
if len(tracks) != 0:
tracks, last_wcs = tracks
xyxys = tracks[:, 0:4].astype('int') # float64 to int
ids = tracks[:, 4].astype('int') # float64 to int
confs = tracks[:, 5]
clss = tracks[:, 6].astype('int') # float64 to int
inds = tracks[:, 7].astype('int') # float64 to int
if tracks.shape[0] != 0:
for xyxy, id, conf, cls, last_wc in zip(xyxys, ids, confs, clss, last_wcs):
if conf >= 0.9:
result_file.write(f"{frame_counter+1},{id},{xyxy[0]},{xyxy[1]},{xyxy[2]-xyxy[0]},{xyxy[3]-xyxy[1]},{last_wc[0]},{last_wc[1]},{last_wc[2]},-1\n")
if not id in gidx:
gidx.append(id)
im = cv2.rectangle(
im,
(xyxy[0], xyxy[1]),
(xyxy[2], xyxy[3]),
color,
thickness
)
cv2.putText(
im,
f'id: {id}, conf: {conf}, c: {cls}',
(xyxy[0], xyxy[1]-10),
cv2.FONT_HERSHEY_SIMPLEX,
fontscale,
color,
thickness
)
# show image with bboxes, ids, classes and confidences
im = cv2.resize(im, (1920,1080))
cv2.imshow('frame', im)
# break on pressing q
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# frame_counter += 1
print('---------------------------------------------------')
end = time.time()
result_file.close()
print(num_images/(end-start))
cv2.destroyAllWindows()
print(len(gidx))