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.

kitti_bev_utils.py 3.7 kB

2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """
  2. # -*- coding: utf-8 -*-
  3. -----------------------------------------------------------------------------------
  4. """
  5. import math
  6. import os
  7. import sys
  8. import cv2
  9. import numpy as np
  10. src_dir = os.path.dirname(os.path.realpath(__file__))
  11. # while not src_dir.endswith("sfa"):
  12. # src_dir = os.path.dirname(src_dir)
  13. if src_dir not in sys.path:
  14. sys.path.append(src_dir)
  15. import config.kitti_config as cnf
  16. def makeBEVMap(PointCloud_, boundary):
  17. Height = cnf.BEV_HEIGHT + 1
  18. Width = cnf.BEV_WIDTH + 1
  19. # Discretize Feature Map
  20. PointCloud = np.copy(PointCloud_)
  21. # PointCloud[:, 0] = np.int_(np.floor(PointCloud[:, 0] / cnf.DISCRETIZATION))
  22. # PointCloud[:, 1] = np.int_(np.floor(PointCloud[:, 1] / cnf.DISCRETIZATION) + Width / 2)
  23. # 针对Apollo数据集,检测360°
  24. PointCloud[:, 0] = np.int_(np.floor(PointCloud[:, 0] / cnf.DISCRETIZATION_Y) + Height / 2)
  25. PointCloud[:, 1] = np.int_(np.floor(PointCloud[:, 1] / cnf.DISCRETIZATION_X) + Width / 2)
  26. # sort-3times
  27. indices = np.lexsort((-PointCloud[:, 2], PointCloud[:, 1], PointCloud[:, 0]))
  28. PointCloud = PointCloud[indices]
  29. # Height Map
  30. heightMap = np.zeros((Height, Width))
  31. _, indices = np.unique(PointCloud[:, 0:2], axis=0, return_index=True)
  32. PointCloud_frac = PointCloud[indices]
  33. # some important problem is image coordinate is (y,x), not (x,y)
  34. max_height = float(np.abs(boundary['maxZ'] - boundary['minZ']))
  35. heightMap[np.int_(PointCloud_frac[:, 0]), np.int_(PointCloud_frac[:, 1])] = PointCloud_frac[:, 2] / max_height #(1217,609)
  36. # Intensity Map & DensityMap
  37. intensityMap = np.zeros((Height, Width))
  38. densityMap = np.zeros((Height, Width))
  39. _, indices, counts = np.unique(PointCloud[:, 0:2], axis=0, return_index=True, return_counts=True)
  40. PointCloud_top = PointCloud[indices]
  41. normalizedCounts = np.minimum(1.0, np.log(counts + 1) / np.log(64))
  42. intensityMap[np.int_(PointCloud_top[:, 0]), np.int_(PointCloud_top[:, 1])] = PointCloud_top[:, 3] / 255.0 # hesai40p的反射强度0~255
  43. densityMap[np.int_(PointCloud_top[:, 0]), np.int_(PointCloud_top[:, 1])] = normalizedCounts
  44. RGB_Map = np.zeros((3, Height - 1, Width - 1))
  45. RGB_Map[2, :, :] = densityMap[:cnf.BEV_HEIGHT, :cnf.BEV_WIDTH] # r_map
  46. RGB_Map[1, :, :] = heightMap[:cnf.BEV_HEIGHT, :cnf.BEV_WIDTH] # g_map
  47. RGB_Map[0, :, :] = intensityMap[:cnf.BEV_HEIGHT, :cnf.BEV_WIDTH] # b_map
  48. return RGB_Map
  49. # bev image coordinates format
  50. def get_corners(x, y, w, l, yaw):
  51. bev_corners = np.zeros((4, 2), dtype=np.float32)
  52. cos_yaw = np.cos(yaw)
  53. sin_yaw = np.sin(yaw)
  54. # front left
  55. bev_corners[0, 0] = x - w / 2 * cos_yaw - l / 2 * sin_yaw
  56. bev_corners[0, 1] = y - w / 2 * sin_yaw + l / 2 * cos_yaw
  57. # rear left
  58. bev_corners[1, 0] = x - w / 2 * cos_yaw + l / 2 * sin_yaw
  59. bev_corners[1, 1] = y - w / 2 * sin_yaw - l / 2 * cos_yaw
  60. # rear right
  61. bev_corners[2, 0] = x + w / 2 * cos_yaw + l / 2 * sin_yaw
  62. bev_corners[2, 1] = y + w / 2 * sin_yaw - l / 2 * cos_yaw
  63. # front right
  64. bev_corners[3, 0] = x + w / 2 * cos_yaw - l / 2 * sin_yaw
  65. bev_corners[3, 1] = y + w / 2 * sin_yaw + l / 2 * cos_yaw
  66. return bev_corners
  67. def drawRotatedBox(img, x, y, w, l, yaw, color):
  68. img_cp = img.copy()
  69. bev_corners = get_corners(x, y, w, l, yaw)
  70. corners_int = bev_corners.reshape(-1, 1, 2).astype(int)
  71. cv2.polylines(img, [corners_int], True, color, 2)
  72. corners_int = bev_corners.reshape(-1, 2)
  73. cv2.line(img, (int(corners_int[0, 0]), int(corners_int[0, 1])), (int(corners_int[3, 0]), int(corners_int[3, 1])), (255, 255, 0), 2)
  74. # return img_cp

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