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.

visualize.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/usr/bin/env python3
  2. # -*- coding:utf-8 -*-
  3. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  4. import cv2
  5. import numpy as np
  6. __all__ = ["vis"]
  7. def vis(img, boxes, scores, cls_ids, conf=0.5, class_names=None):
  8. for i in range(len(boxes)):
  9. box = boxes[i]
  10. cls_id = int(cls_ids[i])
  11. score = scores[i]
  12. if score < conf:
  13. continue
  14. x0 = int(box[0])
  15. y0 = int(box[1])
  16. x1 = int(box[2])
  17. y1 = int(box[3])
  18. color = (_COLORS[cls_id] * 255).astype(np.uint8).tolist()
  19. text = '{}:{:.1f}%'.format(class_names[cls_id], score * 100)
  20. txt_color = (0, 0, 0) if np.mean(_COLORS[cls_id]) > 0.5 else (255, 255, 255)
  21. font = cv2.FONT_HERSHEY_SIMPLEX
  22. txt_size = cv2.getTextSize(text, font, 0.4, 1)[0]
  23. cv2.rectangle(img, (x0, y0), (x1, y1), color, 2)
  24. txt_bk_color = (_COLORS[cls_id] * 255 * 0.7).astype(np.uint8).tolist()
  25. cv2.rectangle(
  26. img,
  27. (x0, y0 + 1),
  28. (x0 + txt_size[0] + 1, y0 + int(1.5*txt_size[1])),
  29. txt_bk_color,
  30. -1
  31. )
  32. cv2.putText(img, text, (x0, y0 + txt_size[1]), font, 0.4, txt_color, thickness=1)
  33. return img
  34. def get_color(idx):
  35. idx = idx * 3
  36. color = ((37 * idx) % 255, (17 * idx) % 255, (29 * idx) % 255)
  37. return color
  38. def plot_tracking(image, tlwhs, obj_ids, scores=None, frame_id=0, fps=0., ids2=None):
  39. im = np.ascontiguousarray(np.copy(image))
  40. im_h, im_w = im.shape[:2]
  41. top_view = np.zeros([im_w, im_w, 3], dtype=np.uint8) + 255
  42. #text_scale = max(1, image.shape[1] / 1600.)
  43. #text_thickness = 2
  44. #line_thickness = max(1, int(image.shape[1] / 500.))
  45. text_scale = 2
  46. text_thickness = 2
  47. line_thickness = 3
  48. radius = max(5, int(im_w/140.))
  49. cv2.putText(im, 'frame: %d fps: %.2f num: %d' % (frame_id, fps, len(tlwhs)),
  50. (0, int(15 * text_scale)), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), thickness=2)
  51. for i, tlwh in enumerate(tlwhs):
  52. x1, y1, w, h = tlwh
  53. intbox = tuple(map(int, (x1, y1, x1 + w, y1 + h)))
  54. obj_id = int(obj_ids[i])
  55. id_text = '{}'.format(int(obj_id))
  56. if ids2 is not None:
  57. id_text = id_text + ', {}'.format(int(ids2[i]))
  58. color = get_color(abs(obj_id))
  59. cv2.rectangle(im, intbox[0:2], intbox[2:4], color=color, thickness=line_thickness)
  60. cv2.putText(im, id_text, (intbox[0], intbox[1]), cv2.FONT_HERSHEY_PLAIN, text_scale, (0, 0, 255),
  61. thickness=text_thickness)
  62. return im
  63. _COLORS = np.array(
  64. [
  65. 0.000, 0.447, 0.741,
  66. 0.850, 0.325, 0.098,
  67. 0.929, 0.694, 0.125,
  68. 0.494, 0.184, 0.556,
  69. 0.466, 0.674, 0.188,
  70. 0.301, 0.745, 0.933,
  71. 0.635, 0.078, 0.184,
  72. 0.300, 0.300, 0.300,
  73. 0.600, 0.600, 0.600,
  74. 1.000, 0.000, 0.000,
  75. 1.000, 0.500, 0.000,
  76. 0.749, 0.749, 0.000,
  77. 0.000, 1.000, 0.000,
  78. 0.000, 0.000, 1.000,
  79. 0.667, 0.000, 1.000,
  80. 0.333, 0.333, 0.000,
  81. 0.333, 0.667, 0.000,
  82. 0.333, 1.000, 0.000,
  83. 0.667, 0.333, 0.000,
  84. 0.667, 0.667, 0.000,
  85. 0.667, 1.000, 0.000,
  86. 1.000, 0.333, 0.000,
  87. 1.000, 0.667, 0.000,
  88. 1.000, 1.000, 0.000,
  89. 0.000, 0.333, 0.500,
  90. 0.000, 0.667, 0.500,
  91. 0.000, 1.000, 0.500,
  92. 0.333, 0.000, 0.500,
  93. 0.333, 0.333, 0.500,
  94. 0.333, 0.667, 0.500,
  95. 0.333, 1.000, 0.500,
  96. 0.667, 0.000, 0.500,
  97. 0.667, 0.333, 0.500,
  98. 0.667, 0.667, 0.500,
  99. 0.667, 1.000, 0.500,
  100. 1.000, 0.000, 0.500,
  101. 1.000, 0.333, 0.500,
  102. 1.000, 0.667, 0.500,
  103. 1.000, 1.000, 0.500,
  104. 0.000, 0.333, 1.000,
  105. 0.000, 0.667, 1.000,
  106. 0.000, 1.000, 1.000,
  107. 0.333, 0.000, 1.000,
  108. 0.333, 0.333, 1.000,
  109. 0.333, 0.667, 1.000,
  110. 0.333, 1.000, 1.000,
  111. 0.667, 0.000, 1.000,
  112. 0.667, 0.333, 1.000,
  113. 0.667, 0.667, 1.000,
  114. 0.667, 1.000, 1.000,
  115. 1.000, 0.000, 1.000,
  116. 1.000, 0.333, 1.000,
  117. 1.000, 0.667, 1.000,
  118. 0.333, 0.000, 0.000,
  119. 0.500, 0.000, 0.000,
  120. 0.667, 0.000, 0.000,
  121. 0.833, 0.000, 0.000,
  122. 1.000, 0.000, 0.000,
  123. 0.000, 0.167, 0.000,
  124. 0.000, 0.333, 0.000,
  125. 0.000, 0.500, 0.000,
  126. 0.000, 0.667, 0.000,
  127. 0.000, 0.833, 0.000,
  128. 0.000, 1.000, 0.000,
  129. 0.000, 0.000, 0.167,
  130. 0.000, 0.000, 0.333,
  131. 0.000, 0.000, 0.500,
  132. 0.000, 0.000, 0.667,
  133. 0.000, 0.000, 0.833,
  134. 0.000, 0.000, 1.000,
  135. 0.000, 0.000, 0.000,
  136. 0.143, 0.143, 0.143,
  137. 0.286, 0.286, 0.286,
  138. 0.429, 0.429, 0.429,
  139. 0.571, 0.571, 0.571,
  140. 0.714, 0.714, 0.714,
  141. 0.857, 0.857, 0.857,
  142. 0.000, 0.447, 0.741,
  143. 0.314, 0.717, 0.741,
  144. 0.50, 0.5, 0
  145. ]
  146. ).astype(np.float32).reshape(-1, 3)