quick_start.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import cv2
  2. import numpy as np
  3. import time
  4. from pyorbbecsdk import *
  5. from utils import frame_to_bgr_image
  6. ESC_KEY = 27
  7. MIN_DEPTH = 20 # 20mm
  8. MAX_DEPTH = 10000 # 10000mm
  9. def main():
  10. pipeline = Pipeline()
  11. pipeline.start()
  12. print("Pipeline started successfully. Press 'q' or ESC to exit.")
  13. # Set window size
  14. window_width = 1280
  15. window_height = 720
  16. cv2.namedWindow("QuickStart Viewer", cv2.WINDOW_NORMAL)
  17. cv2.resizeWindow("QuickStart Viewer", window_width, window_height)
  18. while True:
  19. try:
  20. frames = pipeline.wait_for_frames(100)
  21. if frames is None:
  22. continue
  23. # Get color frame
  24. color_frame = frames.get_color_frame()
  25. if color_frame is None:
  26. continue
  27. color_image = frame_to_bgr_image(color_frame)
  28. # Get depth frame
  29. depth_frame = frames.get_depth_frame()
  30. if depth_frame is None:
  31. continue
  32. if depth_frame.get_format() != OBFormat.Y16:
  33. print("Depth format is not Y16")
  34. continue
  35. # Process depth data
  36. width = depth_frame.get_width()
  37. height = depth_frame.get_height()
  38. scale = depth_frame.get_depth_scale()
  39. depth_data = np.frombuffer(depth_frame.get_data(), dtype=np.uint16).reshape((height, width))
  40. depth_data = depth_data.astype(np.float32) * scale
  41. depth_data = np.where((depth_data > MIN_DEPTH) & (depth_data < MAX_DEPTH), depth_data, 0).astype(np.uint16)
  42. # Create depth visualization
  43. depth_image = cv2.normalize(depth_data, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U)
  44. depth_image = cv2.applyColorMap(depth_image, cv2.COLORMAP_JET)
  45. # Resize and combine images
  46. color_image_resized = cv2.resize(color_image, (window_width // 2, window_height))
  47. depth_image_resized = cv2.resize(depth_image, (window_width // 2, window_height))
  48. combined_image = np.hstack((color_image_resized, depth_image_resized))
  49. cv2.imshow("QuickStart Viewer", combined_image)
  50. if cv2.waitKey(1) in [ord('q'), ESC_KEY]:
  51. break
  52. except KeyboardInterrupt:
  53. break
  54. cv2.destroyAllWindows()
  55. pipeline.stop()
  56. print("Pipeline stopped and all windows closed.")
  57. if __name__ == "__main__":
  58. main()