result.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright (c) 2024 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 os
  15. import numpy as np
  16. from ...common.result import BaseResult
  17. from .visualizer_3d import Visualizer3D
  18. class BEV3DDetResult(BaseResult):
  19. """Base class for computer vision results."""
  20. def __init__(self, data: dict) -> None:
  21. """
  22. Initialize the BaseCVResult.
  23. Args:
  24. data (dict): The initial data.
  25. Raises:
  26. AssertionError: If the required key (`BaseCVResult.INPUT_IMG_KEY`) are not found in the data.
  27. """
  28. super().__init__(data)
  29. def visualize(self, save_path: str, show: bool) -> None:
  30. # input point cloud
  31. assert "input_path" in self.keys(), "input_path is not found in the data"
  32. points = np.fromfile(self["input_path"], dtype=np.float32)
  33. points = points.reshape(-1, 5)
  34. points = points[:, :4]
  35. # detection result
  36. result = dict()
  37. assert "boxes_3d" in self.keys(), "boxes_3d is not found in the data"
  38. result["bbox3d"] = self["boxes_3d"]
  39. assert "scores_3d" in self.keys(), "scores_3d is not found in the data"
  40. result["scores"] = self["scores_3d"]
  41. assert "labels_3d" in self.keys(), "labels_3d is not found in the data"
  42. result["labels"] = self["labels_3d"]
  43. if save_path is not None:
  44. # save result for local visualization
  45. if not os.path.exists(save_path):
  46. os.makedirs(save_path)
  47. np.save(os.path.join(save_path, "results.npy"), result)
  48. np.save(os.path.join(save_path, "points.npy"), points)
  49. if show:
  50. # visualize
  51. score_threshold = 0.25
  52. vis = Visualizer3D()
  53. vis.draw_results(points, result, score_threshold)
  54. return