1. 完成了对相机深度信息的封装,但是深度获取还有点问题

This commit is contained in:
KYOSG 2024-02-28 15:04:18 +08:00
parent 4fa517fa1f
commit c7b7bbe194
4 changed files with 159 additions and 26 deletions

44
.idea/workspace.xml generated
View File

@ -4,19 +4,11 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="5de6e434-16d8-46ca-a50f-5cce2ce0877f" name="更改" comment="1. 成功允运行了Yolo的分类模型大体了解了使用方式&#10;2. 成功运行了Gemini2摄像头&#10;3. 成功将摄像头与Yolo模型整合运行">
<change afterPath="$PROJECT_DIR$/img_process/ObjectClassify.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/img_process/orbbec_camera/ColorViewer.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/img_process/test.py" afterDir="false" />
<list default="true" id="5de6e434-16d8-46ca-a50f-5cce2ce0877f" name="更改" comment="1. 完成了对相机和模型调用的封装">
<change afterPath="$PROJECT_DIR$/img_process/orbbec_camera/DepthViewer.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/camera/__init__.py" beforeDir="false" afterPath="$PROJECT_DIR$/img_process/__init__.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/camera/image_processing.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/camera/orbbec_camera/__init__.py" beforeDir="false" afterPath="$PROJECT_DIR$/img_process/orbbec_camera/__init__.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/camera/orbbec_camera/color_viewer.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/camera/utils.py" beforeDir="false" afterPath="$PROJECT_DIR$/img_process/orbbec_camera/utils.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/camera/yolo.py" beforeDir="false" afterPath="$PROJECT_DIR$/img_process/yolo.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/camera/yolov8n.pt" beforeDir="false" afterPath="$PROJECT_DIR$/img_process/yolov8n.pt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/img_process/ObjectClassify.py" beforeDir="false" afterPath="$PROJECT_DIR$/img_process/ObjectClassify.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/img_process/orbbec_camera/ColorViewer.py" beforeDir="false" afterPath="$PROJECT_DIR$/img_process/orbbec_camera/ColorViewer.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -48,8 +40,11 @@
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"Python.ColorViewer.executor": "Run",
"Python.DepthViewer.executor": "Run",
"Python.ObjectClassify.executor": "Run",
"Python.color_viewer.executor": "Run",
"Python.depth_color_sync_align_viewer.executor": "Run",
"Python.depth_viewer.executor": "Run",
"Python.image_processing.executor": "Run",
"Python.yolo.executor": "Run",
"RunOnceActivity.OpenProjectViewOnStart": "true",
@ -57,7 +52,7 @@
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"WebServerToolWindowFactoryState": "false",
"ignore_missing_gitignore": "true",
"last_opened_file_path": "F:/Projects/PyCharmProjects/graduation_design/.venv/Lib/site-packages",
"last_opened_file_path": "F:/Projects/PyCharmProjects/graduation_design/img_process/orbbec_camera",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
@ -69,11 +64,11 @@
}]]></component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="F:\Projects\PyCharmProjects\graduation_design\img_process\orbbec_camera" />
<recent name="F:\Projects\PyCharmProjects\graduation_design\.venv\Lib\site-packages" />
<recent name="F:\Projects\PyCharmProjects\graduation_design\camera\orbbec_camera" />
<recent name="F:\Projects\PyCharmProjects\graduation_design\camera" />
<recent name="F:\Projects\PyCharmProjects\graduation_design\lib" />
<recent name="F:\Projects\PyCharmProjects\graduation_design\integration_testing" />
</key>
<key name="MoveFile.RECENT_KEYS">
<recent name="F:\Projects\PyCharmProjects\graduation_design\camera\orbbec_camera" />
@ -116,7 +111,8 @@
<workItem from="1708918314610" duration="999000" />
<workItem from="1708925007136" duration="10070000" />
<workItem from="1708953749808" duration="6400000" />
<workItem from="1708996202967" duration="9852000" />
<workItem from="1708996202967" duration="9931000" />
<workItem from="1709092700145" duration="10988000" />
</task>
<task id="LOCAL-00001" summary="1. 成功允运行了Yolo的分类模型大体了解了使用方式&#10;2. 成功运行了Gemini2摄像头&#10;3. 成功将摄像头与Yolo模型整合运行">
<option name="closed" value="true" />
@ -126,7 +122,15 @@
<option name="project" value="LOCAL" />
<updated>1708960272915</updated>
</task>
<option name="localTasksCounter" value="2" />
<task id="LOCAL-00002" summary="1. 完成了对相机和模型调用的封装">
<option name="closed" value="true" />
<created>1709015553553</created>
<option name="number" value="00002" />
<option name="presentableId" value="LOCAL-00002" />
<option name="project" value="LOCAL" />
<updated>1709015553553</updated>
</task>
<option name="localTasksCounter" value="3" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -134,13 +138,17 @@
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="1. 成功允运行了Yolo的分类模型大体了解了使用方式&#10;2. 成功运行了Gemini2摄像头&#10;3. 成功将摄像头与Yolo模型整合运行" />
<option name="LAST_COMMIT_MESSAGE" value="1. 成功允运行了Yolo的分类模型大体了解了使用方式&#10;2. 成功运行了Gemini2摄像头&#10;3. 成功将摄像头与Yolo模型整合运行" />
<MESSAGE value="1. 完成了对相机和模型调用的封装" />
<option name="LAST_COMMIT_MESSAGE" value="1. 完成了对相机和模型调用的封装" />
</component>
<component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/graduation_design$depth_viewer.coverage" NAME="depth_viewer 覆盖结果" MODIFIED="1709095443171" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/img_process/orbbec_camera" />
<SUITE FILE_PATH="coverage/graduation_design$yolo.coverage" NAME="yolo 覆盖结果" MODIFIED="1709010274059" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/camera" />
<SUITE FILE_PATH="coverage/graduation_design$ColorViewer.coverage" NAME="ColorViewer 覆盖结果" MODIFIED="1709015445565" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/img_process/orbbec_camera" />
<SUITE FILE_PATH="coverage/graduation_design$DepthViewer.coverage" NAME="DepthViewer 覆盖结果" MODIFIED="1709102323304" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/img_process/orbbec_camera" />
<SUITE FILE_PATH="coverage/graduation_design$image_processing.coverage" NAME="image_processing 覆盖结果" MODIFIED="1708957639566" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/camera" />
<SUITE FILE_PATH="coverage/graduation_design$ObjectClassify.coverage" NAME="ObjectClassify 覆盖结果" MODIFIED="1709015448681" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/img_process" />
<SUITE FILE_PATH="coverage/graduation_design$ObjectClassify.coverage" NAME="ObjectClassify 覆盖结果" MODIFIED="1709103671084" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/img_process" />
<SUITE FILE_PATH="coverage/graduation_design$color_viewer.coverage" NAME="color_viewer 覆盖结果" MODIFIED="1708957257852" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/camera/orbbec_camera" />
<SUITE FILE_PATH="coverage/graduation_design$depth_color_sync_align_viewer.coverage" NAME="depth_color_sync_align_viewer 覆盖结果" MODIFIED="1709093655863" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/img_process/orbbec_camera" />
</component>
</project>

View File

@ -1,6 +1,7 @@
from ultralytics import YOLO
import cv2
from orbbec_camera.ColorViewer import ColorViewer
from orbbec_camera.DepthViewer import DepthViewer
class ObjectClassify:
@ -24,16 +25,26 @@ class ObjectClassify:
if __name__ == '__main__':
oc = ObjectClassify()
# 实例化相机操作类
cw = ColorViewer()
# 启动相机
cw.start()
# 实例化彩色图像操作类
# cw = ColorViewer()
# 启动彩色相机
# cw.start()
# 实例化深度图像操作类
dw = DepthViewer()
# 启动深度相机
dw.start()
# 处理循环
while True:
frame = cw.get_current_frame()
# frame = cw.get_current_frame()
frame = dw.get_current_image()
if frame is not None:
oc.model.predict(source=frame, show=True)
cv2.imshow("深度图像", frame)
print(dw.get_depth())
# if frame is not None:
# oc.model.predict(source=frame, show=True)
key = cv2.waitKey(1)
if key == ord('q'):
cw.stop()
dw.stop()
# cw.stop()
break

View File

@ -12,7 +12,7 @@ class ColorViewer:
try:
self.pipeline = Pipeline() # 创建Pipeline对象用于管理数据流
self.config = Config() # 创建配置对象
self.color_frame = None # 前颜色帧
self.color_frame = None # 前颜色帧
self.active = False # 控制捕获循环的标志
self.thread = None # 线程对象,用于并行执行捕获循环
self.ESC_KEY = 27 # ESC键的ASCII码用于退出循环

View File

@ -0,0 +1,114 @@
import threading
from pyorbbecsdk import Pipeline, FrameSet
from pyorbbecsdk import Config
from pyorbbecsdk import OBSensorType, OBFormat
from pyorbbecsdk import OBError
import cv2
import numpy as np
import time
# 定义一个用于实现时间滤波的类
class DepthViewer:
def __init__(self, ):
# 创建配置对象和管道对象
self.config = Config()
self.pipeline = Pipeline()
self.alpha = 0.5 # 厂家给出的参数
self.previous_frame = None
self.ESC_KEY = 27
self.PRINT_INTERVAL = 1 # 每隔1秒打印一次信息
self.MIN_DEPTH = 20 # 最小深度值为20mm
self.MAX_DEPTH = 10000 # 最大深度值为10000mm
self.active = False # 控制捕获循环的标志
self.thread = None # 线程对象,用于并行执行捕获循环
self.color_profile = None # 颜色传感器的流配置
self.depth_frame = None # 当前深度帧
self.width = 640 # 图像宽度
self.fps = 30 # 帧率
self.depth_image = None # 深度图像
self.center_distance = 0 # 中心点的深度值
def start(self):
if not self.active:
self.active = True
# 选择深度传感器
profile_list = self.pipeline.get_stream_profile_list(OBSensorType.DEPTH_SENSOR)
# 获取指定分辨率和格式的深度流配置
try:
depth_profile = profile_list.get_video_stream_profile(self.width, 0, OBFormat.Y16, self.fps)
print("深度流配置获取成功:", depth_profile)
except OBError as e:
print("获取深度流配置失败: ", e)
# 如果出错,则获取默认的深度流配置
depth_profile = profile_list.get_default_video_stream_profile()
assert depth_profile is not None
# 将深度流配置加入到配置对象中
print('将深度流配置加入到配置对象中')
self.config.enable_stream(depth_profile)
# 创建并启动线程目标函数是_capture_loop
self.thread = threading.Thread(target=self._capture_loop)
print('创建并启动线程目标函数是_capture_loop')
self.thread.start()
def stop(self):
self.active = False
if self.thread is not None:
self.thread.join()
def _capture_loop(self):
try:
print("管道启动")
self.pipeline.start(self.config)
print("管道启动成功")
while True:
try:
frames: FrameSet = self.pipeline.wait_for_frames(100)
if frames is None:
continue
self.depth_frame = frames.get_depth_frame()
if self.depth_frame is None:
continue
# 对当前帧进行数据处理
width = self.depth_frame.get_width()
height = self.depth_frame.get_height()
scale = self.depth_frame.get_depth_scale()
# 获取深度数据并进行处理
depth_data = np.frombuffer(self.depth_frame.get_data(), dtype=np.uint16)
depth_data = depth_data.reshape((height, width))
depth_data = depth_data.astype(np.float32) * scale
depth_data = np.where((depth_data > self.MIN_DEPTH) & (depth_data < self.MAX_DEPTH), depth_data, 0)
depth_data = depth_data.astype(np.uint16)
# 使用时间滤波
if self.previous_frame is None:
pass
else:
depth_data = cv2.addWeighted(depth_data, self.alpha, self.previous_frame, 1 - self.alpha, 0)
self.previous_frame = depth_data
# 获取图像中心点的深度值
center_y = int(height / 2)
center_x = int(width / 2)
self.center_distance = depth_data[center_y, center_x]
# 将深度数据转换为灰度图像,并使用伪彩色表示深度信息
depth_image = cv2.normalize(depth_data, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U)
self.depth_image = cv2.applyColorMap(depth_image, cv2.COLORMAP_JET)
except Exception as e:
print(print('创建流失败{}', e))
finally:
self.pipeline.stop()
# 获取当前帧
def get_current_image(self):
# 返回当前深度图
return self.depth_image
def get_depth(self):
# 返回图像中心点的深度值
return self.center_distance