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

visualization_utils.py 5.1 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. """
  2. # -*- coding: utf-8 -*-
  3. -----------------------------------------------------------------------------------
  4. # Author: Nguyen Mau Dung
  5. # DoC: 2020.08.09
  6. # email: nguyenmaudung93.kstn@gmail.com
  7. -----------------------------------------------------------------------------------
  8. # Description: The utils of the kitti dataset
  9. """
  10. from __future__ import print_function
  11. import os
  12. import sys
  13. import numpy as np
  14. import cv2
  15. src_dir = os.path.dirname(os.path.realpath(__file__))
  16. # while not src_dir.endswith("sfa"):
  17. # src_dir = os.path.dirname(src_dir)
  18. if src_dir not in sys.path:
  19. sys.path.append(src_dir)
  20. import config.kitti_config as cnf
  21. def roty(angle):
  22. # Rotation about the y-axis.
  23. c = np.cos(angle)
  24. s = np.sin(angle)
  25. return np.array([[c, 0, s],
  26. [0, 1, 0],
  27. [-s, 0, c]])
  28. def compute_box_3d(dim, location, ry):
  29. # dim: 3
  30. # location: 3
  31. # ry: 1
  32. # return: 8 x 3
  33. R = roty(ry)
  34. h, w, l = dim
  35. x_corners = [l / 2, l / 2, -l / 2, -l / 2, l / 2, l / 2, -l / 2, -l / 2]
  36. y_corners = [0, 0, 0, 0, -h, -h, -h, -h]
  37. z_corners = [w / 2, -w / 2, -w / 2, w / 2, w / 2, -w / 2, -w / 2, w / 2]
  38. corners = np.array([x_corners, y_corners, z_corners], dtype=np.float32)
  39. corners_3d = np.dot(R, corners)
  40. corners_3d = corners_3d + np.array(location, dtype=np.float32).reshape(3, 1)
  41. return corners_3d.transpose(1, 0)
  42. def project_to_image(pts_3d, P):
  43. # pts_3d: n x 3
  44. # P: 3 x 4
  45. # return: n x 2
  46. pts_3d_homo = np.concatenate([pts_3d, np.ones((pts_3d.shape[0], 1), dtype=np.float32)], axis=1)
  47. pts_2d = np.dot(P, pts_3d_homo.transpose(1, 0)).transpose(1, 0)
  48. pts_2d = pts_2d[:, :2] / pts_2d[:, 2:]
  49. return pts_2d.astype(np.int)
  50. def draw_box_3d_v2(image, qs, color=(255, 0, 255), thickness=2):
  51. ''' Draw 3d bounding box in image
  52. qs: (8,3) array of vertices for the 3d box in following order:
  53. 1 -------- 0
  54. /| /|
  55. 2 -------- 3 .
  56. | | | |
  57. . 5 -------- 4
  58. |/ |/
  59. 6 -------- 7
  60. '''
  61. qs = qs.astype(np.int32)
  62. for k in range(0, 4):
  63. # Ref: http://docs.enthought.com/mayavi/mayavi/auto/mlab_helper_functions.html
  64. i, j = k, (k + 1) % 4
  65. # use LINE_AA for opencv3
  66. cv2.line(image, (qs[i, 0], qs[i, 1]), (qs[j, 0], qs[j, 1]), color, thickness)
  67. i, j = k + 4, (k + 1) % 4 + 4
  68. cv2.line(image, (qs[i, 0], qs[i, 1]), (qs[j, 0], qs[j, 1]), color, thickness)
  69. i, j = k, k + 4
  70. cv2.line(image, (qs[i, 0], qs[i, 1]), (qs[j, 0], qs[j, 1]), color, thickness)
  71. return image
  72. def draw_box_3d(image, corners, color=(0, 0, 255)):
  73. ''' Draw 3d bounding box in image
  74. corners: (8,3) array of vertices for the 3d box in following order:
  75. 1 -------- 0
  76. /| /|
  77. 2 -------- 3 .
  78. | | | |
  79. . 5 -------- 4
  80. |/ |/
  81. 6 -------- 7
  82. '''
  83. face_idx = [[0, 1, 5, 4],
  84. [1, 2, 6, 5],
  85. [2, 3, 7, 6],
  86. [3, 0, 4, 7]]
  87. for ind_f in range(3, -1, -1):
  88. f = face_idx[ind_f]
  89. for j in range(4):
  90. cv2.line(image, (corners[f[j], 0], corners[f[j], 1]),
  91. (corners[f[(j + 1) % 4], 0], corners[f[(j + 1) % 4], 1]), color, 2, lineType=cv2.LINE_AA)
  92. if ind_f == 0:
  93. cv2.line(image, (corners[f[0], 0], corners[f[0], 1]),
  94. (corners[f[2], 0], corners[f[2], 1]), color, 1, lineType=cv2.LINE_AA)
  95. cv2.line(image, (corners[f[1], 0], corners[f[1], 1]),
  96. (corners[f[3], 0], corners[f[3], 1]), color, 1, lineType=cv2.LINE_AA)
  97. return image
  98. def show_rgb_image_with_boxes(img, labels, calib):
  99. for box_idx, label in enumerate(labels):
  100. cls_id, location, dim, ry = label[0], label[1:4], label[4:7], label[7]
  101. if location[2] < 2.0: # The object is too close to the camera, ignore it during visualization
  102. continue
  103. if cls_id < 0:
  104. continue
  105. corners_3d = compute_box_3d(dim, location, ry)
  106. corners_2d = project_to_image(corners_3d, calib.P2)
  107. img = draw_box_3d(img, corners_2d, color=cnf.colors[int(cls_id)])
  108. return img
  109. def merge_rgb_to_bev(img_rgb, img_bev, output_width):
  110. img_rgb_h, img_rgb_w = img_rgb.shape[:2]
  111. ratio_rgb = output_width / img_rgb_w
  112. output_rgb_h = int(ratio_rgb * img_rgb_h)
  113. ret_img_rgb = cv2.resize(img_rgb, (output_width, output_rgb_h))
  114. img_bev_h, img_bev_w = img_bev.shape[:2]
  115. ratio_bev = output_width / img_bev_w
  116. output_bev_h = int(ratio_bev * img_bev_h)
  117. ret_img_bev = cv2.resize(img_bev, (output_width, output_bev_h))
  118. out_img = np.zeros((output_rgb_h + output_bev_h, output_width, 3), dtype=np.uint8)
  119. # Upper: RGB --> BEV
  120. out_img[:output_rgb_h, ...] = ret_img_rgb
  121. out_img[output_rgb_h:, ...] = ret_img_bev
  122. return out_img

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能