文章目录
奥比中光深度相机(二):PyQt5实现打开深度摄像头功能
PyQt5实现打开可见光相机摄像头的功能,我们之前已经介绍过了,具体可参考:https://blog.youkuaiyun.com/WYKB_Mr_Q/article/details/129827685
上篇文章中我们介绍了奥比中光深度相机基于Python的环境配置部分。具体可参考:https://blog.youkuaiyun.com/WYKB_Mr_Q/article/details/137040226
这篇文章我们在配置好Python环境的基础上,使用PyQt5写个界面,并且设计逻辑,通过点击一个设计好的按钮来打开深度相机。深度相机的打开方法和可见光相机的打开方法略有不同,可见光相机可以利用OpenCV来直接调用,奥比中光深度相机主要是利用官方给出的SDK源码来调用。本文中,我们借鉴官方源码,将调用深度相机的主要代码提取出来,并和我们自己写的代码结合,实现在PyQt5界面中通过点击按钮打开深度相机。
官方给出的调用深度相机源码
官方给出了打开深度相机的Python SDK源码,链接为:https://github.com/orbbec/pyorbbecsdk
其中也包含了一些.py示例文件,存储在pyorbbecsdk/examples
文件夹中,我们找到 “depth_viewer.py” 文件,该文件的主要作用是为了调用深度相机,显示深度图像的。咱们可以先连接上奥比中光深度相机,运行一下该代码试一试,看看是否能够正常运行,展示出深度视频流。如果一切正常,你就可以看到如下界面了。
下面部分是 “depth_viewer.py” 文件的源代码:
from pyorbbecsdk import Pipeline
from pyorbbecsdk import Config
from pyorbbecsdk import OBSensorType, OBFormat
from pyorbbecsdk import OBError
import cv2
import numpy as np
import time
ESC_KEY = 27
PRINT_INTERVAL = 1 # seconds
MIN_DEPTH = 20 # 20mm
MAX_DEPTH = 10000 # 10000mm
# 管理时间流,这部分不用管
class TemporalFilter:
def __init__(self, alpha):
self.alpha = alpha
self.previous_frame = None
def process(self, frame):
if self.previous_frame is None:
result = frame
else:
result = cv2.addWeighted(frame, self.alpha, self.previous_frame, 1 - self.alpha, 0)
self.previous_frame = result
return result
def main():
# 配置深度相机文件
config = Config()
pipeline = Pipeline()
temporal_filter = TemporalFilter(alpha=0.5)
try:
# 调用深度摄像头
profile_list = pipeline.get_stream_profile_list(OBSensorType.DEPTH_SENSOR)
assert profile_list is not None
try:
# 配置深度视频流参数,包括视频帧长宽,视频流格式,以及每秒采集的视频帧数
depth_profile = profile_list.get_video_stream_profile(640, 0, OBFormat.Y16, 30)
except OBError as e:
print("Error: ", e)
depth_profile = profile_list.get_default_video_stream_profile()
assert depth_profile is not None
print("depth profile: ", type(depth_profile))
config.enable_stream(depth_profile)
except Exception as e:
print(e)
return
pipeline.start(config)
last_print_time = time.time()
while True:
try:
frames = pipeline.wait_for_frames(100)
if frames is None:
continue
depth_frame = frames.get_depth_frame()
if depth_frame is None:
continue
width = depth_frame.get_width()
height = depth_frame.get_height()
scale = depth_frame.get_depth_scale()
depth_data = np.frombuffer(depth_frame.get_data(), dtype=np