visualization.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import cv2
  15. import numpy as np
  16. def get_color(idx):
  17. idx = idx * 3
  18. color = ((37 * idx) % 255, (17 * idx) % 255, (29 * idx) % 255)
  19. return color
  20. def plot_tracking(image,
  21. tlwhs,
  22. obj_ids,
  23. scores=None,
  24. frame_id=0,
  25. fps=0.,
  26. ids2names=[]):
  27. im = np.ascontiguousarray(np.copy(image))
  28. im_h, im_w = im.shape[:2]
  29. top_view = np.zeros([im_w, im_w, 3], dtype=np.uint8) + 255
  30. text_scale = max(1, image.shape[1] / 1600.)
  31. text_thickness = 2
  32. line_thickness = max(1, int(image.shape[1] / 500.))
  33. radius = max(5, int(im_w / 140.))
  34. cv2.putText(
  35. im,
  36. 'frame: %d fps: %.2f num: %d' % (frame_id, fps, len(tlwhs)),
  37. (0, int(15 * text_scale)),
  38. cv2.FONT_HERSHEY_PLAIN,
  39. text_scale, (0, 0, 255),
  40. thickness=2)
  41. for i, tlwh in enumerate(tlwhs):
  42. x1, y1, w, h = tlwh
  43. intbox = tuple(map(int, (x1, y1, x1 + w, y1 + h)))
  44. obj_id = int(obj_ids[i])
  45. id_text = '{}'.format(int(obj_id))
  46. if ids2names != []:
  47. assert len(
  48. ids2names) == 1, "plot_tracking only supports single classes."
  49. id_text = '{}_'.format(ids2names[0]) + id_text
  50. _line_thickness = 1 if obj_id <= 0 else line_thickness
  51. color = get_color(abs(obj_id))
  52. cv2.rectangle(
  53. im, intbox[0:2], intbox[2:4], color=color, thickness=line_thickness)
  54. cv2.putText(
  55. im,
  56. id_text, (intbox[0], intbox[1] - 10),
  57. cv2.FONT_HERSHEY_PLAIN,
  58. text_scale, (0, 0, 255),
  59. thickness=text_thickness)
  60. if scores is not None:
  61. text = '{:.2f}'.format(float(scores[i]))
  62. cv2.putText(
  63. im,
  64. text, (intbox[0], intbox[1] + 10),
  65. cv2.FONT_HERSHEY_PLAIN,
  66. text_scale, (0, 255, 255),
  67. thickness=text_thickness)
  68. return im
  69. def plot_tracking_dict(image,
  70. num_classes,
  71. tlwhs_dict,
  72. obj_ids_dict,
  73. scores_dict,
  74. frame_id=0,
  75. fps=0.,
  76. ids2names=[]):
  77. im = np.ascontiguousarray(np.copy(image))
  78. im_h, im_w = im.shape[:2]
  79. top_view = np.zeros([im_w, im_w, 3], dtype=np.uint8) + 255
  80. text_scale = max(1, image.shape[1] / 1600.)
  81. text_thickness = 2
  82. line_thickness = max(1, int(image.shape[1] / 500.))
  83. radius = max(5, int(im_w / 140.))
  84. for cls_id in range(num_classes):
  85. tlwhs = tlwhs_dict[cls_id]
  86. obj_ids = obj_ids_dict[cls_id]
  87. scores = scores_dict[cls_id]
  88. cv2.putText(
  89. im,
  90. 'frame: %d fps: %.2f num: %d' % (frame_id, fps, len(tlwhs)),
  91. (0, int(15 * text_scale)),
  92. cv2.FONT_HERSHEY_PLAIN,
  93. text_scale, (0, 0, 255),
  94. thickness=2)
  95. for i, tlwh in enumerate(tlwhs):
  96. x1, y1, w, h = tlwh
  97. intbox = tuple(map(int, (x1, y1, x1 + w, y1 + h)))
  98. obj_id = int(obj_ids[i])
  99. id_text = '{}'.format(int(obj_id))
  100. if ids2names != []:
  101. id_text = '{}_{}'.format(ids2names[cls_id], id_text)
  102. else:
  103. id_text = 'class{}_{}'.format(cls_id, id_text)
  104. _line_thickness = 1 if obj_id <= 0 else line_thickness
  105. color = get_color(abs(obj_id))
  106. cv2.rectangle(
  107. im,
  108. intbox[0:2],
  109. intbox[2:4],
  110. color=color,
  111. thickness=line_thickness)
  112. cv2.putText(
  113. im,
  114. id_text, (intbox[0], intbox[1] - 10),
  115. cv2.FONT_HERSHEY_PLAIN,
  116. text_scale, (0, 0, 255),
  117. thickness=text_thickness)
  118. if scores is not None:
  119. text = '{:.2f}'.format(float(scores[i]))
  120. cv2.putText(
  121. im,
  122. text, (intbox[0], intbox[1] + 10),
  123. cv2.FONT_HERSHEY_PLAIN,
  124. text_scale, (0, 255, 255),
  125. thickness=text_thickness)
  126. return im