model.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import numpy as np
  2. from openpilot.common.transformations.orientation import rot_from_euler
  3. from openpilot.common.transformations.camera import get_view_frame_from_calib_frame, view_frame_from_device_frame
  4. # segnet
  5. SEGNET_SIZE = (512, 384)
  6. # MED model
  7. MEDMODEL_INPUT_SIZE = (512, 256)
  8. MEDMODEL_YUV_SIZE = (MEDMODEL_INPUT_SIZE[0], MEDMODEL_INPUT_SIZE[1] * 3 // 2)
  9. MEDMODEL_CY = 47.6
  10. medmodel_fl = 910.0
  11. medmodel_intrinsics = np.array([
  12. [medmodel_fl, 0.0, 0.5 * MEDMODEL_INPUT_SIZE[0]],
  13. [0.0, medmodel_fl, MEDMODEL_CY],
  14. [0.0, 0.0, 1.0]])
  15. # BIG model
  16. BIGMODEL_INPUT_SIZE = (1024, 512)
  17. BIGMODEL_YUV_SIZE = (BIGMODEL_INPUT_SIZE[0], BIGMODEL_INPUT_SIZE[1] * 3 // 2)
  18. bigmodel_fl = 910.0
  19. bigmodel_intrinsics = np.array([
  20. [bigmodel_fl, 0.0, 0.5 * BIGMODEL_INPUT_SIZE[0]],
  21. [0.0, bigmodel_fl, 256 + MEDMODEL_CY],
  22. [0.0, 0.0, 1.0]])
  23. # SBIG model (big model with the size of small model)
  24. SBIGMODEL_INPUT_SIZE = (512, 256)
  25. SBIGMODEL_YUV_SIZE = (SBIGMODEL_INPUT_SIZE[0], SBIGMODEL_INPUT_SIZE[1] * 3 // 2)
  26. sbigmodel_fl = 455.0
  27. sbigmodel_intrinsics = np.array([
  28. [sbigmodel_fl, 0.0, 0.5 * SBIGMODEL_INPUT_SIZE[0]],
  29. [0.0, sbigmodel_fl, 0.5 * (256 + MEDMODEL_CY)],
  30. [0.0, 0.0, 1.0]])
  31. bigmodel_frame_from_calib_frame = np.dot(bigmodel_intrinsics,
  32. get_view_frame_from_calib_frame(0, 0, 0, 0))
  33. sbigmodel_frame_from_calib_frame = np.dot(sbigmodel_intrinsics,
  34. get_view_frame_from_calib_frame(0, 0, 0, 0))
  35. medmodel_frame_from_calib_frame = np.dot(medmodel_intrinsics,
  36. get_view_frame_from_calib_frame(0, 0, 0, 0))
  37. medmodel_frame_from_bigmodel_frame = np.dot(medmodel_intrinsics, np.linalg.inv(bigmodel_intrinsics))
  38. calib_from_medmodel = np.linalg.inv(medmodel_frame_from_calib_frame[:, :3])
  39. calib_from_sbigmodel = np.linalg.inv(sbigmodel_frame_from_calib_frame[:, :3])
  40. # This function is verified to give similar results to xx.uncommon.utils.transform_img
  41. def get_warp_matrix(device_from_calib_euler: np.ndarray, intrinsics: np.ndarray, bigmodel_frame: bool = False) -> np.ndarray:
  42. calib_from_model = calib_from_sbigmodel if bigmodel_frame else calib_from_medmodel
  43. device_from_calib = rot_from_euler(device_from_calib_euler)
  44. camera_from_calib = intrinsics @ view_frame_from_device_frame @ device_from_calib
  45. warp_matrix: np.ndarray = camera_from_calib @ calib_from_model
  46. return warp_matrix