cargo_height_measure.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import time
  2. import cv2
  3. from depth_common import (
  4. Settings,
  5. TemporalFilter,
  6. compute_roi_bounds,
  7. extract_depth_data,
  8. find_nearest_point,
  9. init_depth_pipeline,
  10. nearest_distance_in_roi,
  11. )
  12. # 键盘退出键
  13. ESC_KEY = 27
  14. # 打印间隔(秒)
  15. PRINT_INTERVAL = 1 # seconds
  16. # 从环境变量加载测量配置
  17. SETTINGS = Settings.from_env()
  18. def main():
  19. # 初始化时间滤波器,减少抖动
  20. temporal_filter = TemporalFilter(alpha=0.5)
  21. try:
  22. # 启动深度相机管线
  23. pipeline, depth_intrinsics, depth_profile = init_depth_pipeline()
  24. print("depth profile: ", depth_profile)
  25. except Exception as e:
  26. print(e)
  27. return
  28. last_print_time = time.time()
  29. while True:
  30. try:
  31. # 获取一帧深度数据
  32. frames = pipeline.wait_for_frames(100)
  33. if frames is None:
  34. continue
  35. depth_frame = frames.get_depth_frame()
  36. depth_data = extract_depth_data(depth_frame, SETTINGS, temporal_filter)
  37. if depth_data is None:
  38. continue
  39. # 计算中心 ROI 区域
  40. bounds = compute_roi_bounds(depth_data, depth_intrinsics, SETTINGS)
  41. if bounds is None:
  42. continue
  43. x_start, x_end, y_start, y_end, center_distance = bounds
  44. roi = depth_data[y_start:y_end, x_start:x_end]
  45. # 计算 ROI 内最近距离
  46. nearest_distance = nearest_distance_in_roi(roi, SETTINGS) or 0
  47. # 找出 ROI 内最近点用于可视化
  48. nearest_point = find_nearest_point(
  49. roi,
  50. x_start,
  51. y_start,
  52. SETTINGS,
  53. nearest_distance,
  54. )
  55. current_time = time.time()
  56. if current_time - last_print_time >= PRINT_INTERVAL:
  57. # 定期输出最近距离
  58. print(
  59. "nearest distance in "
  60. f"{SETTINGS.roi_width_cm}cm x {SETTINGS.roi_height_cm}cm area: ",
  61. nearest_distance,
  62. )
  63. last_print_time = current_time
  64. # 生成彩色深度图并叠加标注
  65. depth_image = cv2.normalize(depth_data, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U)
  66. depth_image = cv2.applyColorMap(depth_image, cv2.COLORMAP_JET)
  67. cv2.rectangle(
  68. depth_image,
  69. (x_start, y_start),
  70. (x_end - 1, y_end - 1),
  71. (0, 255, 0),
  72. 2,
  73. )
  74. if nearest_point is not None:
  75. cv2.circle(depth_image, nearest_point, 4, (0, 0, 0), -1)
  76. cv2.circle(depth_image, nearest_point, 6, (0, 255, 255), 2)
  77. # 文字标注当前测量值
  78. label = f"nearest: {nearest_distance} mm"
  79. cv2.putText(
  80. depth_image,
  81. label,
  82. (10, 30),
  83. cv2.FONT_HERSHEY_SIMPLEX,
  84. 0.8,
  85. (255, 255, 255),
  86. 2,
  87. cv2.LINE_AA,
  88. )
  89. center_label = f"center: {int(center_distance)} mm"
  90. cv2.putText(
  91. depth_image,
  92. center_label,
  93. (10, 60),
  94. cv2.FONT_HERSHEY_SIMPLEX,
  95. 0.8,
  96. (255, 255, 255),
  97. 2,
  98. cv2.LINE_AA,
  99. )
  100. cv2.imshow("Depth Viewer", depth_image)
  101. key = cv2.waitKey(1)
  102. if key == ord('q') or key == ESC_KEY:
  103. break
  104. except KeyboardInterrupt:
  105. break
  106. # 清理窗口与相机资源
  107. cv2.destroyAllWindows()
  108. pipeline.stop()
  109. if __name__ == "__main__":
  110. main()