Spaces:
Runtime error
Runtime error
File size: 7,194 Bytes
7734d5b |
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 |
import os
import sys
import json
import cv2
import glob as gb
import numpy as np
def colormap(rgb=False):
color_list = np.array(
[
0.000, 0.447, 0.741,
0.850, 0.325, 0.098,
0.929, 0.694, 0.125,
0.494, 0.184, 0.556,
0.466, 0.674, 0.188,
0.301, 0.745, 0.933,
0.635, 0.078, 0.184,
0.300, 0.300, 0.300,
0.600, 0.600, 0.600,
1.000, 0.000, 0.000,
1.000, 0.500, 0.000,
0.749, 0.749, 0.000,
0.000, 1.000, 0.000,
0.000, 0.000, 1.000,
0.667, 0.000, 1.000,
0.333, 0.333, 0.000,
0.333, 0.667, 0.000,
0.333, 1.000, 0.000,
0.667, 0.333, 0.000,
0.667, 0.667, 0.000,
0.667, 1.000, 0.000,
1.000, 0.333, 0.000,
1.000, 0.667, 0.000,
1.000, 1.000, 0.000,
0.000, 0.333, 0.500,
0.000, 0.667, 0.500,
0.000, 1.000, 0.500,
0.333, 0.000, 0.500,
0.333, 0.333, 0.500,
0.333, 0.667, 0.500,
0.333, 1.000, 0.500,
0.667, 0.000, 0.500,
0.667, 0.333, 0.500,
0.667, 0.667, 0.500,
0.667, 1.000, 0.500,
1.000, 0.000, 0.500,
1.000, 0.333, 0.500,
1.000, 0.667, 0.500,
1.000, 1.000, 0.500,
0.000, 0.333, 1.000,
0.000, 0.667, 1.000,
0.000, 1.000, 1.000,
0.333, 0.000, 1.000,
0.333, 0.333, 1.000,
0.333, 0.667, 1.000,
0.333, 1.000, 1.000,
0.667, 0.000, 1.000,
0.667, 0.333, 1.000,
0.667, 0.667, 1.000,
0.667, 1.000, 1.000,
1.000, 0.000, 1.000,
1.000, 0.333, 1.000,
1.000, 0.667, 1.000,
0.167, 0.000, 0.000,
0.333, 0.000, 0.000,
0.500, 0.000, 0.000,
0.667, 0.000, 0.000,
0.833, 0.000, 0.000,
1.000, 0.000, 0.000,
0.000, 0.167, 0.000,
0.000, 0.333, 0.000,
0.000, 0.500, 0.000,
0.000, 0.667, 0.000,
0.000, 0.833, 0.000,
0.000, 1.000, 0.000,
0.000, 0.000, 0.167,
0.000, 0.000, 0.333,
0.000, 0.000, 0.500,
0.000, 0.000, 0.667,
0.000, 0.000, 0.833,
0.000, 0.000, 1.000,
0.000, 0.000, 0.000,
0.143, 0.143, 0.143,
0.286, 0.286, 0.286,
0.429, 0.429, 0.429,
0.571, 0.571, 0.571,
0.714, 0.714, 0.714,
0.857, 0.857, 0.857,
1.000, 1.000, 1.000
]
).astype(np.float32)
color_list = color_list.reshape((-1, 3)) * 255
if not rgb:
color_list = color_list[:, ::-1]
return color_list
def txt2img(visual_path="visual_val_gt"):
print("Starting txt2img")
valid_labels = {1}
ignore_labels = {2, 7, 8, 12}
if not os.path.exists(visual_path):
os.makedirs(visual_path)
color_list = colormap()
gt_json_path = 'datasets/mot/annotations/val_half.json'
img_path = 'datasets/mot/train/'
show_video_names = ['MOT17-02-FRCNN',
'MOT17-04-FRCNN',
'MOT17-05-FRCNN',
'MOT17-09-FRCNN',
'MOT17-10-FRCNN',
'MOT17-11-FRCNN',
'MOT17-13-FRCNN']
test_json_path = 'datasets/mot/annotations/test.json'
test_img_path = 'datasets/mot/test/'
test_show_video_names = ['MOT17-01-FRCNN',
'MOT17-03-FRCNN',
'MOT17-06-FRCNN',
'MOT17-07-FRCNN',
'MOT17-08-FRCNN',
'MOT17-12-FRCNN',
'MOT17-14-FRCNN']
if visual_path == "visual_test_predict":
show_video_names = test_show_video_names
img_path = test_img_path
gt_json_path = test_json_path
for show_video_name in show_video_names:
img_dict = dict()
if visual_path == "visual_val_gt":
txt_path = 'datasets/mot/train/' + show_video_name + '/gt/gt_val_half.txt'
elif visual_path == "visual_yolox_x":
txt_path = 'YOLOX_outputs/yolox_mot_x_1088/track_results/'+ show_video_name + '.txt'
elif visual_path == "visual_test_predict":
txt_path = 'test/tracks/'+ show_video_name + '.txt'
else:
raise NotImplementedError
with open(gt_json_path, 'r') as f:
gt_json = json.load(f)
for ann in gt_json["images"]:
file_name = ann['file_name']
video_name = file_name.split('/')[0]
if video_name == show_video_name:
img_dict[ann['frame_id']] = img_path + file_name
txt_dict = dict()
with open(txt_path, 'r') as f:
for line in f.readlines():
linelist = line.split(',')
mark = int(float(linelist[6]))
label = int(float(linelist[7]))
vis_ratio = float(linelist[8])
if visual_path == "visual_val_gt":
if mark == 0 or label not in valid_labels or label in ignore_labels or vis_ratio <= 0:
continue
img_id = linelist[0]
obj_id = linelist[1]
bbox = [float(linelist[2]), float(linelist[3]),
float(linelist[2]) + float(linelist[4]),
float(linelist[3]) + float(linelist[5]), int(obj_id)]
if int(img_id) in txt_dict:
txt_dict[int(img_id)].append(bbox)
else:
txt_dict[int(img_id)] = list()
txt_dict[int(img_id)].append(bbox)
for img_id in sorted(txt_dict.keys()):
img = cv2.imread(img_dict[img_id])
for bbox in txt_dict[img_id]:
cv2.rectangle(img, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), color_list[bbox[4]%79].tolist(), thickness=2)
cv2.putText(img, "{}".format(int(bbox[4])), (int(bbox[0]), int(bbox[1])), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color_list[bbox[4]%79].tolist(), 2)
cv2.imwrite(visual_path + "/" + show_video_name + "{:0>6d}.png".format(img_id), img)
print(show_video_name, "Done")
print("txt2img Done")
def img2video(visual_path="visual_val_gt"):
print("Starting img2video")
img_paths = gb.glob(visual_path + "/*.png")
fps = 16
size = (1920,1080)
videowriter = cv2.VideoWriter(visual_path + "_video.avi",cv2.VideoWriter_fourcc('M','J','P','G'), fps, size)
for img_path in sorted(img_paths):
img = cv2.imread(img_path)
img = cv2.resize(img, size)
videowriter.write(img)
videowriter.release()
print("img2video Done")
if __name__ == '__main__':
visual_path="visual_yolox_x"
if len(sys.argv) > 1:
visual_path =sys.argv[1]
txt2img(visual_path)
#img2video(visual_path)
|