utils.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from typing import Union, Any, Optional
  2. import cv2
  3. import numpy as np
  4. from pyorbbecsdk import FormatConvertFilter, VideoFrame
  5. from pyorbbecsdk import OBFormat, OBConvertFormat
  6. def yuyv_to_bgr(frame: np.ndarray, width: int, height: int) -> np.ndarray:
  7. yuyv = frame.reshape((height, width, 2))
  8. bgr_image = cv2.cvtColor(yuyv, cv2.COLOR_YUV2BGR_YUY2)
  9. return bgr_image
  10. def uyvy_to_bgr(frame: np.ndarray, width: int, height: int) -> np.ndarray:
  11. uyvy = frame.reshape((height, width, 2))
  12. bgr_image = cv2.cvtColor(uyvy, cv2.COLOR_YUV2BGR_UYVY)
  13. return bgr_image
  14. def i420_to_bgr(frame: np.ndarray, width: int, height: int) -> np.ndarray:
  15. y = frame[0:height, :]
  16. u = frame[height:height + height // 4].reshape(height // 2, width // 2)
  17. v = frame[height + height // 4:].reshape(height // 2, width // 2)
  18. yuv_image = cv2.merge([y, u, v])
  19. bgr_image = cv2.cvtColor(yuv_image, cv2.COLOR_YUV2BGR_I420)
  20. return bgr_image
  21. def nv21_to_bgr(frame: np.ndarray, width: int, height: int) -> np.ndarray:
  22. y = frame[0:height, :]
  23. uv = frame[height:height + height // 2].reshape(height // 2, width)
  24. yuv_image = cv2.merge([y, uv])
  25. bgr_image = cv2.cvtColor(yuv_image, cv2.COLOR_YUV2BGR_NV21)
  26. return bgr_image
  27. def nv12_to_bgr(frame: np.ndarray, width: int, height: int) -> np.ndarray:
  28. y = frame[0:height, :]
  29. uv = frame[height:height + height // 2].reshape(height // 2, width)
  30. yuv_image = cv2.merge([y, uv])
  31. bgr_image = cv2.cvtColor(yuv_image, cv2.COLOR_YUV2BGR_NV12)
  32. return bgr_image
  33. def determine_convert_format(frame: VideoFrame):
  34. if frame.get_format() == OBFormat.I420:
  35. return OBConvertFormat.I420_TO_RGB888
  36. elif frame.get_format() == OBFormat.MJPG:
  37. return OBConvertFormat.MJPG_TO_RGB888
  38. elif frame.get_format() == OBFormat.YUYV:
  39. return OBConvertFormat.YUYV_TO_RGB888
  40. elif frame.get_format() == OBFormat.NV21:
  41. return OBConvertFormat.NV21_TO_RGB888
  42. elif frame.get_format() == OBFormat.NV12:
  43. return OBConvertFormat.NV12_TO_RGB888
  44. elif frame.get_format() == OBFormat.UYVY:
  45. return OBConvertFormat.UYVY_TO_RGB888
  46. else:
  47. return None
  48. def frame_to_rgb_frame(frame: VideoFrame) -> Union[Optional[VideoFrame], Any]:
  49. if frame.get_format() == OBFormat.RGB:
  50. return frame
  51. convert_format = determine_convert_format(frame)
  52. if convert_format is None:
  53. print("Unsupported format")
  54. return None
  55. print("covert format: {}".format(convert_format))
  56. convert_filter = FormatConvertFilter()
  57. convert_filter.set_format_convert_format(convert_format)
  58. rgb_frame = convert_filter.process(frame)
  59. if rgb_frame is None:
  60. print("Convert {} to RGB failed".format(frame.get_format()))
  61. return rgb_frame
  62. def frame_to_bgr_image(frame: VideoFrame) -> Union[Optional[np.array], Any]:
  63. width = frame.get_width()
  64. height = frame.get_height()
  65. color_format = frame.get_format()
  66. data = np.asanyarray(frame.get_data())
  67. image = np.zeros((height, width, 3), dtype=np.uint8)
  68. if color_format == OBFormat.RGB:
  69. image = np.resize(data, (height, width, 3))
  70. image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  71. elif color_format == OBFormat.BGR:
  72. image = np.resize(data, (height, width, 3))
  73. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  74. elif color_format == OBFormat.YUYV:
  75. image = np.resize(data, (height, width, 2))
  76. image = cv2.cvtColor(image, cv2.COLOR_YUV2BGR_YUYV)
  77. elif color_format == OBFormat.MJPG:
  78. image = cv2.imdecode(data, cv2.IMREAD_COLOR)
  79. elif color_format == OBFormat.I420:
  80. image = i420_to_bgr(data, width, height)
  81. return image
  82. elif color_format == OBFormat.NV12:
  83. image = nv12_to_bgr(data, width, height)
  84. return image
  85. elif color_format == OBFormat.NV21:
  86. image = nv21_to_bgr(data, width, height)
  87. return image
  88. elif color_format == OBFormat.UYVY:
  89. image = np.resize(data, (height, width, 2))
  90. image = cv2.cvtColor(image, cv2.COLOR_YUV2BGR_UYVY)
  91. else:
  92. print("Unsupported color format: {}".format(color_format))
  93. return None
  94. return image