§ 2.2 深度图视频流读取

1. 导入依赖

import cv2
import numpy as np
from matplotlib import pyplot as plt
# 奥比中光 Orbbec Python SDK
from ObTypes import *
from Property import *
import Pipeline
import StreamProfile
from Error import ObException
import Context

%matplotlib inline

2. 配置日志等级

ctx = Context.Context(None)
ctx.setLoggerSeverity(OB_PY_LOG_SEVERITY_ERROR)

3. 创建管道

# 创建图像管道
pipe = Pipeline.Pipeline(None, None)
# 配置视频流, 控制开启/关闭
config = Pipeline.Config()

4. 配置深度相机

# 获取彩色相机的所有流配置,包括流的分辨率,帧率,以及帧的格式
profiles = pipe.getStreamProfileList(OB_PY_SENSOR_DEPTH)
# 看一共有多少彩图的视频流格式/分辨率/编码方式选项
item_num = profiles.count()
print(f"彩色视频流可选类型: {item_num}")

输出日志

彩色视频流可选类型: 36
# 选择默认的彩色视频流配置
depth_profile = profiles.getProfile(0)
# 图像编码类型
# RLE压测格式(SDK默认会解包成Y16) 
depth_profile.format() == OB_PY_FORMAT_RLE

输出日志

True
# 图像类型 3代表深度图 
depth_profile.type() == OB_PY_SENSOR_DEPTH

输出日志

True
# 构建更具体的彩色相机的信息
depth_profile = depth_profile.toConcreteStreamProfile(OB_PY_STREAM_VIDEO)
img_width = depth_profile.width()
img_height = depth_profile.height()
print(f"深度图-图像宽度: {img_width}")
print(f"深度图-图像高度: {img_height}")

输出日志

深度图-图像宽度: 1280
深度图-图像高度: 800
fps = depth_profile.fps()
print(f"深度相机帧率: {fps}")

输出日志

深度相机帧率: 30

使能视频流

# 使能深度图视频流
config.enableStream(depth_profile)
# 管道开启视频流
pipe.start(config, None)

5. 采集图像

# 等待数据输入
timeout_ms = 100 # 超时等待时间, 单位ms
# 等待数据传入
frames = pipe.waitForFrames(timeout_ms)
if frames is None:
    print("数据获取失败") 
# 获取深度图
depth_frame = frames.depthFrame()
if depth_frame is None:
    print("深度图获取失败")
    
# 获取数据尺寸
size = depth_frame.dataSize()
# 获取数据
data = depth_frame.data()
# 修改深度图数据Data的尺寸
data = np.resize(data,(img_height, img_width, 2))
# 将两组8bit数据,转换为16bit
depth_img = (data[:,:,0]+data[:,:,1]*256) * depth_frame.getValueScale()
# 乘上缩放因子,将单位转换为1mm         
depth_img = (depth_img).astype('float64')

plt.imshow(depth_img)

输出日志

png