指令调用d435i录制并保存
时间: 2023-07-11 13:25:45 浏览: 121
您可以使用以下代码调用D435i相机录制并保存视频。同样,您需要安装相应的SDK并在代码中引用它。
```python
import pyrealsense2 as rs
import numpy as np
import cv2
# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
# Start streaming
pipeline.start(config)
# Define codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 30.0, (640, 480))
# Record video for 10 seconds
start_time = time.time()
while time.time() - start_time < 10:
# Wait for a coherent pair of frames: depth and color
frames = pipeline.wait_for_frames()
depth_frame = frames.get_depth_frame()
color_frame = frames.get_color_frame()
if not depth_frame or not color_frame:
continue
# Convert images to numpy arrays
depth_image = np.asanyarray(depth_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())
# Apply colormap on depth image (image must be converted to 8-bit per pixel first)
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
# Write frames to video
out.write(color_image)
# Stop streaming
pipeline.stop()
out.release()
```
在上面的代码中,我们定义了一个 VideoWriter 对象来保存录制的视频。我们等待相机捕获一组深度和彩色帧,并将它们转换为 numpy 数组。然后,我们对深度图像应用了伪彩色映射,然后将彩色图像写入视频。我们记录了10秒的视频,并在结束后停止了相机流式传输并释放视频写入器。
阅读全文