目前所用使用的代码均为python,代码编辑器使用Pycharm 社区版,操作系统为Ubuntu2204的Linux系统。Pycharm直接下载安装即可。
为了方便环境管理及不同运行环境之间的自由切换,本项目安装了Conda管理工具Miniconda[1](备注:Anaconda更加完善,但容量更大,两者皆可)。
Reference:
[1] Miniconda — conda documentation
安装好conda之后创建一个conda虚拟环境,例如python10_realsense2
# create a python environment named python_realsense2 with python 3.10
conda create --name==python_realsense2 python==3.10
# activate the environment
conda activate python_realsense2
图片采集部分,使用Intel RealSense D435i深度相机。该相机有集成的开发工具包,只需要安装即可使用。目前只安装了python工具包pyrealsense2。
# install the librealsense python wrapper using pip
pip install pyrealsense2
Reference:
https://github.com/IntelRealSense/librealsensehttps://github.com/IntelRealSense/librealsense通过相机读取数据,主要包括图片和深度值,通过保存为RGB图片和深度矩阵的形式:
import pyrealsense2 as rs
import numpy as np
import cv2
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)
align_to = rs.stream.color
align = rs.align(align_to)
cfg = pipeline.start(config)
t = 0
try:
while True:
frames = pipeline.wait_for_frames()
frames_aligned = align.process(frames)
depth_frame = frames_aligned.get_depth_frame()
color_frame = frames_aligned.get_color_frame()
depth_np = np.asanyarray(depth_frame.get_data())
color_np = np.asanyarray(color_frame.get_data())
cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
cv2.imshow('RealSense', color_np)
key = cv2.waitKey(1)
if key in (27, ord("q")):
break
if key == ord("s"):
cv2.imwrite("color_%d.png" % t, color_np)
np.save("depth_%d" % t, depth_np)
t += 1
finally:
pipeline.stop()
print("OVER")