Meta Byte Track
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

txt2video.py 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import os
  2. import sys
  3. import json
  4. import cv2
  5. import glob as gb
  6. import numpy as np
  7. def colormap(rgb=False):
  8. color_list = np.array(
  9. [
  10. 0.000, 0.447, 0.741,
  11. 0.850, 0.325, 0.098,
  12. 0.929, 0.694, 0.125,
  13. 0.494, 0.184, 0.556,
  14. 0.466, 0.674, 0.188,
  15. 0.301, 0.745, 0.933,
  16. 0.635, 0.078, 0.184,
  17. 0.300, 0.300, 0.300,
  18. 0.600, 0.600, 0.600,
  19. 1.000, 0.000, 0.000,
  20. 1.000, 0.500, 0.000,
  21. 0.749, 0.749, 0.000,
  22. 0.000, 1.000, 0.000,
  23. 0.000, 0.000, 1.000,
  24. 0.667, 0.000, 1.000,
  25. 0.333, 0.333, 0.000,
  26. 0.333, 0.667, 0.000,
  27. 0.333, 1.000, 0.000,
  28. 0.667, 0.333, 0.000,
  29. 0.667, 0.667, 0.000,
  30. 0.667, 1.000, 0.000,
  31. 1.000, 0.333, 0.000,
  32. 1.000, 0.667, 0.000,
  33. 1.000, 1.000, 0.000,
  34. 0.000, 0.333, 0.500,
  35. 0.000, 0.667, 0.500,
  36. 0.000, 1.000, 0.500,
  37. 0.333, 0.000, 0.500,
  38. 0.333, 0.333, 0.500,
  39. 0.333, 0.667, 0.500,
  40. 0.333, 1.000, 0.500,
  41. 0.667, 0.000, 0.500,
  42. 0.667, 0.333, 0.500,
  43. 0.667, 0.667, 0.500,
  44. 0.667, 1.000, 0.500,
  45. 1.000, 0.000, 0.500,
  46. 1.000, 0.333, 0.500,
  47. 1.000, 0.667, 0.500,
  48. 1.000, 1.000, 0.500,
  49. 0.000, 0.333, 1.000,
  50. 0.000, 0.667, 1.000,
  51. 0.000, 1.000, 1.000,
  52. 0.333, 0.000, 1.000,
  53. 0.333, 0.333, 1.000,
  54. 0.333, 0.667, 1.000,
  55. 0.333, 1.000, 1.000,
  56. 0.667, 0.000, 1.000,
  57. 0.667, 0.333, 1.000,
  58. 0.667, 0.667, 1.000,
  59. 0.667, 1.000, 1.000,
  60. 1.000, 0.000, 1.000,
  61. 1.000, 0.333, 1.000,
  62. 1.000, 0.667, 1.000,
  63. 0.167, 0.000, 0.000,
  64. 0.333, 0.000, 0.000,
  65. 0.500, 0.000, 0.000,
  66. 0.667, 0.000, 0.000,
  67. 0.833, 0.000, 0.000,
  68. 1.000, 0.000, 0.000,
  69. 0.000, 0.167, 0.000,
  70. 0.000, 0.333, 0.000,
  71. 0.000, 0.500, 0.000,
  72. 0.000, 0.667, 0.000,
  73. 0.000, 0.833, 0.000,
  74. 0.000, 1.000, 0.000,
  75. 0.000, 0.000, 0.167,
  76. 0.000, 0.000, 0.333,
  77. 0.000, 0.000, 0.500,
  78. 0.000, 0.000, 0.667,
  79. 0.000, 0.000, 0.833,
  80. 0.000, 0.000, 1.000,
  81. 0.000, 0.000, 0.000,
  82. 0.143, 0.143, 0.143,
  83. 0.286, 0.286, 0.286,
  84. 0.429, 0.429, 0.429,
  85. 0.571, 0.571, 0.571,
  86. 0.714, 0.714, 0.714,
  87. 0.857, 0.857, 0.857,
  88. 1.000, 1.000, 1.000
  89. ]
  90. ).astype(np.float32)
  91. color_list = color_list.reshape((-1, 3)) * 255
  92. if not rgb:
  93. color_list = color_list[:, ::-1]
  94. return color_list
  95. def txt2img(visual_path="visual_val_gt"):
  96. print("Starting txt2img")
  97. valid_labels = {1}
  98. ignore_labels = {2, 7, 8, 12}
  99. if not os.path.exists(visual_path):
  100. os.makedirs(visual_path)
  101. color_list = colormap()
  102. gt_json_path = 'datasets/mot/annotations/val_half.json'
  103. img_path = 'datasets/mot/train/'
  104. show_video_names = ['MOT17-02-FRCNN',
  105. 'MOT17-04-FRCNN',
  106. 'MOT17-05-FRCNN',
  107. 'MOT17-09-FRCNN',
  108. 'MOT17-10-FRCNN',
  109. 'MOT17-11-FRCNN',
  110. 'MOT17-13-FRCNN']
  111. test_json_path = 'datasets/mot/annotations/test.json'
  112. test_img_path = 'datasets/mot/test/'
  113. test_show_video_names = ['MOT17-01-FRCNN',
  114. 'MOT17-03-FRCNN',
  115. 'MOT17-06-FRCNN',
  116. 'MOT17-07-FRCNN',
  117. 'MOT17-08-FRCNN',
  118. 'MOT17-12-FRCNN',
  119. 'MOT17-14-FRCNN']
  120. if visual_path == "visual_test_predict":
  121. show_video_names = test_show_video_names
  122. img_path = test_img_path
  123. gt_json_path = test_json_path
  124. for show_video_name in show_video_names:
  125. img_dict = dict()
  126. if visual_path == "visual_val_gt":
  127. txt_path = 'datasets/mot/train/' + show_video_name + '/gt/gt_val_half.txt'
  128. elif visual_path == "visual_yolox_x":
  129. txt_path = 'YOLOX_outputs/yolox_mot_x_1088/track_results/'+ show_video_name + '.txt'
  130. elif visual_path == "visual_test_predict":
  131. txt_path = 'test/tracks/'+ show_video_name + '.txt'
  132. else:
  133. raise NotImplementedError
  134. with open(gt_json_path, 'r') as f:
  135. gt_json = json.load(f)
  136. for ann in gt_json["images"]:
  137. file_name = ann['file_name']
  138. video_name = file_name.split('/')[0]
  139. if video_name == show_video_name:
  140. img_dict[ann['frame_id']] = img_path + file_name
  141. txt_dict = dict()
  142. with open(txt_path, 'r') as f:
  143. for line in f.readlines():
  144. linelist = line.split(',')
  145. mark = int(float(linelist[6]))
  146. label = int(float(linelist[7]))
  147. vis_ratio = float(linelist[8])
  148. if visual_path == "visual_val_gt":
  149. if mark == 0 or label not in valid_labels or label in ignore_labels or vis_ratio <= 0:
  150. continue
  151. img_id = linelist[0]
  152. obj_id = linelist[1]
  153. bbox = [float(linelist[2]), float(linelist[3]),
  154. float(linelist[2]) + float(linelist[4]),
  155. float(linelist[3]) + float(linelist[5]), int(obj_id)]
  156. if int(img_id) in txt_dict:
  157. txt_dict[int(img_id)].append(bbox)
  158. else:
  159. txt_dict[int(img_id)] = list()
  160. txt_dict[int(img_id)].append(bbox)
  161. for img_id in sorted(txt_dict.keys()):
  162. img = cv2.imread(img_dict[img_id])
  163. for bbox in txt_dict[img_id]:
  164. cv2.rectangle(img, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), color_list[bbox[4]%79].tolist(), thickness=2)
  165. 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)
  166. cv2.imwrite(visual_path + "/" + show_video_name + "{:0>6d}.png".format(img_id), img)
  167. print(show_video_name, "Done")
  168. print("txt2img Done")
  169. def img2video(visual_path="visual_val_gt"):
  170. print("Starting img2video")
  171. img_paths = gb.glob(visual_path + "/*.png")
  172. fps = 16
  173. size = (1920,1080)
  174. videowriter = cv2.VideoWriter(visual_path + "_video.avi",cv2.VideoWriter_fourcc('M','J','P','G'), fps, size)
  175. for img_path in sorted(img_paths):
  176. img = cv2.imread(img_path)
  177. img = cv2.resize(img, size)
  178. videowriter.write(img)
  179. videowriter.release()
  180. print("img2video Done")
  181. if __name__ == '__main__':
  182. visual_path="visual_yolox_x"
  183. if len(sys.argv) > 1:
  184. visual_path =sys.argv[1]
  185. txt2img(visual_path)
  186. #img2video(visual_path)