为了使用Intel RealSense D435i相机,我们需要安装pyrealsense2库。你可以通过以下命令安装它:
pip install pyrealsense2
下面是一个使用pyrealsense2库实现的示例程序:
import os
import cv2
import pyrealsense2 as rs
def take_photo(color_frame, photo_path):
color_image = np.asanyarray(color_frame.get_data())
cv2.imwrite(photo_path, color_image)
print(f"照片已保存到: {photo_path}")
def main():
# 创建保存照片的文件夹
photo_dir = "photos"
if not os.path.exists(photo_dir):
os.makedirs(photo_dir)
# 配置相机
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
# 启动相机
profile = pipeline.start(config)
try:
photo_count = 0
while True:
# 等待帧
frames = pipeline.wait_for_frames()
color_frame = frames.get_color_frame()
if not color_frame:
continue
# 将帧转换为图像
color_image = np.asanyarray(color_frame.get_data())
cv2.imshow('RealSense D435i 实时视频', color_image)
key = cv2.waitKey(1) & 0xFF
if key == ord(' '):
photo_path = os.path.join(photo_dir, f"photo_{photo_count}.png")
take_photo(color_frame, photo_path)
photo_count += 1
elif key == 27: # 按下ESC键退出
break
finally:
pipeline.stop()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
这个程序将显示RealSense D435i相机的实时视频,并在按下空格键时拍照。拍摄的照片将保存在程序所在目录下的"photos"文件夹中。如果出现问题,程序会输出错误信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)