1. 完成了对相机深度和色彩模式的整合

2. 旧代码转移到ababdan_code目录中
This commit is contained in:
KYOSG 2024-03-01 10:57:41 +08:00
parent c7b7bbe194
commit 2e77f05678
10 changed files with 335 additions and 166 deletions

50
.idea/workspace.xml generated
View File

@ -4,11 +4,16 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="5de6e434-16d8-46ca-a50f-5cce2ce0877f" name="更改" comment="1. 完成了对相机和模型调用的封装">
<change afterPath="$PROJECT_DIR$/img_process/orbbec_camera/DepthViewer.py" afterDir="false" />
<list default="true" id="5de6e434-16d8-46ca-a50f-5cce2ce0877f" name="更改" comment="1. 完成了对相机深度信息的封装,但是深度获取还有点问题">
<change afterPath="$PROJECT_DIR$/abandon_code/test.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$/distance_measurement/__init__.py" beforeDir="false" afterPath="$PROJECT_DIR$/img_process/orbbec_camera/OrbbecCamera.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/distance_measurement/stereo_vision.py" beforeDir="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" />
<change beforePath="$PROJECT_DIR$/img_process/orbbec_camera/ColorViewer.py" beforeDir="false" afterPath="$PROJECT_DIR$/abandon_code/ColorViewer.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/img_process/orbbec_camera/DepthViewer.py" beforeDir="false" afterPath="$PROJECT_DIR$/abandon_code/DepthViewer.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/img_process/test.py" beforeDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -42,23 +47,25 @@
"Python.ColorViewer.executor": "Run",
"Python.DepthViewer.executor": "Run",
"Python.ObjectClassify.executor": "Run",
"Python.OrbbecCamera.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.test.executor": "Run",
"Python.yolo.executor": "Run",
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"WebServerToolWindowFactoryState": "false",
"ignore_missing_gitignore": "true",
"last_opened_file_path": "F:/Projects/PyCharmProjects/graduation_design/img_process/orbbec_camera",
"last_opened_file_path": "F:/Projects/PyCharmProjects/pyorbbecsdk",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm",
"settings.editor.selected.configurable": "preferences.pluginManager",
"settings.editor.selected.configurable": "settings.saveactions",
"vue.rearranger.settings.migration": "true"
}
}]]></component>
@ -71,17 +78,17 @@
<recent name="F:\Projects\PyCharmProjects\graduation_design\lib" />
</key>
<key name="MoveFile.RECENT_KEYS">
<recent name="F:\Projects\PyCharmProjects\graduation_design\abandon_code" />
<recent name="F:\Projects\PyCharmProjects\graduation_design\img_process\orbbec_camera" />
<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\camera\orbbec_camera\lib" />
<recent name="F:\Projects\PyCharmProjects\graduation_design\yolo" />
</key>
</component>
<component name="SharedIndexes">
<attachedChunks>
<set>
<option value="bundled-python-sdk-5a2391486177-2887949eec09-com.jetbrains.pycharm.pro.sharedIndexes.bundled-PY-233.13763.11" />
<option value="bundled-python-sdk-d68999036c7f-b11f5e8da5ad-com.jetbrains.pycharm.pro.sharedIndexes.bundled-PY-233.14475.56" />
</set>
</attachedChunks>
</component>
@ -112,7 +119,11 @@
<workItem from="1708925007136" duration="10070000" />
<workItem from="1708953749808" duration="6400000" />
<workItem from="1708996202967" duration="9931000" />
<workItem from="1709092700145" duration="10988000" />
<workItem from="1709092700145" duration="11067000" />
<workItem from="1709201263936" duration="3447000" />
<workItem from="1709206411719" duration="1390000" />
<workItem from="1709210454458" duration="710000" />
<workItem from="1709258808137" duration="2791000" />
</task>
<task id="LOCAL-00001" summary="1. 成功允运行了Yolo的分类模型大体了解了使用方式&#10;2. 成功运行了Gemini2摄像头&#10;3. 成功将摄像头与Yolo模型整合运行">
<option name="closed" value="true" />
@ -130,7 +141,15 @@
<option name="project" value="LOCAL" />
<updated>1709015553553</updated>
</task>
<option name="localTasksCounter" value="3" />
<task id="LOCAL-00003" summary="1. 完成了对相机深度信息的封装,但是深度获取还有点问题">
<option name="closed" value="true" />
<created>1709103859741</created>
<option name="number" value="00003" />
<option name="presentableId" value="LOCAL-00003" />
<option name="project" value="LOCAL" />
<updated>1709103859741</updated>
</task>
<option name="localTasksCounter" value="4" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -139,16 +158,19 @@
<component name="VcsManagerConfiguration">
<MESSAGE value="1. 成功允运行了Yolo的分类模型大体了解了使用方式&#10;2. 成功运行了Gemini2摄像头&#10;3. 成功将摄像头与Yolo模型整合运行" />
<MESSAGE value="1. 完成了对相机和模型调用的封装" />
<option name="LAST_COMMIT_MESSAGE" value="1. 完成了对相机和模型调用的封装" />
<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$ColorViewer.coverage" NAME="ColorViewer 覆盖结果" MODIFIED="1709210701965" 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="1709204380257" 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="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$test.coverage" NAME="test 覆盖结果" MODIFIED="1709202296879" 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$ObjectClassify.coverage" NAME="ObjectClassify 覆盖结果" MODIFIED="1709261495229" 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" />
<SUITE FILE_PATH="coverage/graduation_design$OrbbecCamera.coverage" NAME="OrbbecCamera 覆盖结果" MODIFIED="1709261493426" 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

@ -21,8 +21,7 @@
### 2.2. 主要模块
1. **图像处理模块**: 使用OpenCV和PyTorch进行图像识别和处理。
2. **双目测距模块**: 计算物体与相机的距离。
1. **图像处理模块**: 使用OpenCV和PyTorch进行图像识别和处理,该模块同时还集成了中心点测距
3. **机械臂控制模块**: 根据计算结果控制机械臂。
4. **集成与测试模块**: 集成各个模块,并进行测试。

View File

@ -3,10 +3,10 @@ from pyorbbecsdk import Pipeline, FrameSet
from pyorbbecsdk import Config
from pyorbbecsdk import OBSensorType, OBFormat
import cv2
from .utils import frame_to_bgr_image
from img_process.orbbec_camera.utils import frame_to_bgr_image
# 这个类用于初始化摄像头并调用颜色传感器获取图像
# 初始化摄像头并调用颜色传感器获取图像
class ColorViewer:
def __init__(self):
try:
@ -18,7 +18,7 @@ class ColorViewer:
self.ESC_KEY = 27 # ESC键的ASCII码用于退出循环
self.profile_list = None # 颜色传感器的流配置列表
self.color_profile = None # 颜色传感器的流配置
self.width = 1920 # 图像宽度
self.width = 640 # 图像宽度
self.fps = 30 # 帧率
except Exception as e:
print('初始化失败:', e)

View File

@ -18,7 +18,6 @@ class DepthViewer:
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 # 控制捕获循环的标志

118
abandon_code/test.py Normal file
View File

@ -0,0 +1,118 @@
# ******************************************************************************
# Copyright (c) 2023 Orbbec 3D Technology, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:# www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ******************************************************************************
from pyorbbecsdk import *
import cv2
import numpy as np
from utils import frame_to_bgr_image
import sys
import argparse
ESC_KEY = 27
def main(argv):
pipeline = Pipeline()
device = pipeline.get_device()
device_info = device.get_device_info()
device_pid = device_info.get_pid()
config = Config()
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--mode",
help="align mode, HW=hardware mode,SW=software mode,NONE=disable align",
type=str, default='HW')
parser.add_argument("-s", "--enable_sync", help="enable sync", type=bool, default=True)
args = parser.parse_args()
align_mode = args.mode
enable_sync = args.enable_sync
try:
profile_list = pipeline.get_stream_profile_list(OBSensorType.COLOR_SENSOR)
color_profile = profile_list.get_default_video_stream_profile()
config.enable_stream(color_profile)
profile_list = pipeline.get_stream_profile_list(OBSensorType.DEPTH_SENSOR)
assert profile_list is not None
depth_profile = profile_list.get_default_video_stream_profile()
assert depth_profile is not None
print("color profile : {}x{}@{}_{}".format(color_profile.get_width(),
color_profile.get_height(),
color_profile.get_fps(),
color_profile.get_format()))
print("depth profile : {}x{}@{}_{}".format(depth_profile.get_width(),
depth_profile.get_height(),
depth_profile.get_fps(),
depth_profile.get_format()))
config.enable_stream(depth_profile)
except Exception as e:
print(e)
return
if align_mode == 'HW':
if device_pid == 0x066B:
# Femto Mega does not support hardware D2C, and it is changed to software D2C
config.set_align_mode(OBAlignMode.SW_MODE)
else:
config.set_align_mode(OBAlignMode.HW_MODE)
elif align_mode == 'SW':
config.set_align_mode(OBAlignMode.SW_MODE)
else:
config.set_align_mode(OBAlignMode.DISABLE)
if enable_sync:
try:
pipeline.enable_frame_sync()
except Exception as e:
print(e)
try:
pipeline.start(config)
except Exception as e:
print(e)
return
while True:
try:
frames: FrameSet = pipeline.wait_for_frames(100)
if frames is None:
continue
color_frame = frames.get_color_frame()
if color_frame is None:
continue
# covert to RGB format
color_image = frame_to_bgr_image(color_frame)
if color_image is None:
print("failed to convert frame to image")
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.uint16)
depth_data = depth_data.reshape((height, width))
depth_data = depth_data.astype(np.float32) * scale
depth_image = cv2.normalize(depth_data, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U)
depth_image = cv2.applyColorMap(depth_image, cv2.COLORMAP_JET)
# overlay color image on depth image
depth_image = cv2.addWeighted(color_image, 0.5, depth_image, 0.5, 0)
cv2.imshow("SyncAlignViewer ", depth_image)
key = cv2.waitKey(1)
if key == ord('q') or key == ESC_KEY:
break
except KeyboardInterrupt:
break
pipeline.stop()
if __name__ == "__main__":
main(sys.argv[1:])

View File

@ -1,22 +0,0 @@
import numpy as np
import cv2
# 双目测距模块
# 双目相机校准
def stereo_calibration(left_camera_params, right_camera_params):
# TODO: 实现双目相机的校准
pass
# 计算视差
def compute_disparity(left_image, right_image):
# TODO: 计算左右图像的视差
pass
# 计算距离
def measure_distance(disparity_map, calibration_data):
# TODO: 使用视差图和校准数据计算距离
pass

View File

@ -1,7 +1,7 @@
from ultralytics import YOLO
import cv2
from orbbec_camera.ColorViewer import ColorViewer
from orbbec_camera.DepthViewer import DepthViewer
from abandon_code.ColorViewer import ColorViewer
from orbbec_camera.OrbbecCamera import OrbbecCamera
class ObjectClassify:
@ -25,26 +25,25 @@ class ObjectClassify:
if __name__ == '__main__':
oc = ObjectClassify()
# 实例化彩色图像操作类
# cw = ColorViewer()
# 启动彩色相机
# cw.start()
# 实例化深度图像操作类
dw = DepthViewer()
# 启动深度相机
dw.start()
ob = OrbbecCamera('HW', True)
ob.run()
# 处理循环
while True:
# frame = cw.get_current_frame()
frame = dw.get_current_image()
if frame is not None:
cv2.imshow("深度图像", frame)
print(dw.get_depth())
# if frame is not None:
# oc.model.predict(source=frame, show=True)
color_image = ob.get_color_image()
if color_image is None:
continue
cv2.imshow("彩色图像", color_image)
if ob.get_depth_image() is None:
continue
cv2.imshow("深度图像", ob.get_depth_image())
if ob.get_combined_image() is None:
continue
cv2.imshow("合成图像", ob.get_combined_image())
print(f"中心点的深度值: {ob.get_center_distance()}mm")
if color_image is not None:
oc.model.predict(source=color_image, show=True)
key = cv2.waitKey(1)
if key == ord('q'):
dw.stop()
ob.stop()
# cw.stop()
break

View File

@ -0,0 +1,159 @@
import threading
import cv2
import numpy as np
import sys
import argparse
from pyorbbecsdk import *
from .utils import frame_to_bgr_image
class OrbbecCamera:
# 定义一个类常量作为退出键
ESC_KEY = 27
def __init__(self, mode='HW', enable_sync=True):
# 构造函数初始化Orbbec相机和配置
self.pipeline = Pipeline() # 创建Pipeline对象
self.device = self.pipeline.get_device() # 获取设备对象
self.config = Config() # 创建配置对象
self.mode = mode # 设置对齐模式
self.enable_sync = enable_sync # 设置是否启用帧同步
self.center_distance = 0 # 中心点的深度值
self.MIN_DEPTH = 20 # 最小深度值为20mm
self.MAX_DEPTH = 10000 # 最大深度值为10000mm
self.color_image = None # 颜色图像
self.depth_image = None # 深度图像
self.combined_image = None # 叠加图像
# 线程控制
self.thread = None # 线程对象,用于并行执行捕获循环
self.active = False # 控制捕获循环的标志
# 显示设置
self.width = 640 # 图像宽度
self.fps = 30 # 帧率
# 初始化相机设置
device_info = self.device.get_device_info() # 获取设备信息
device_pid = device_info.get_pid() # 获取设备PID
try:
# 获取并设置颜色流配置
profile_list = self.pipeline.get_stream_profile_list(OBSensorType.COLOR_SENSOR)
# color_profile = profile_list.get_default_video_stream_profile()
color_profile = profile_list.get_video_stream_profile(self.width, 0, OBFormat.RGB, self.fps)
self.config.enable_stream(color_profile)
# 获取并设置深度流配置
profile_list = self.pipeline.get_stream_profile_list(OBSensorType.DEPTH_SENSOR)
# depth_profile = profile_list.get_default_video_stream_profile()
depth_profile = profile_list.get_video_stream_profile(self.width, 0, OBFormat.Y16, self.fps)
self.config.enable_stream(depth_profile)
except Exception as e:
print(e)
sys.exit(1)
# 根据模式设置对齐方式
# HW为硬件D2C对齐
# SW为软件D2C对齐
if self.mode == 'HW':
if device_pid == 0x066B:
self.config.set_align_mode(OBAlignMode.SW_MODE)
else:
self.config.set_align_mode(OBAlignMode.HW_MODE)
elif self.mode == 'SW':
self.config.set_align_mode(OBAlignMode.SW_MODE)
else:
self.config.set_align_mode(OBAlignMode.DISABLE)
# 启用帧同步
if self.enable_sync:
try:
self.pipeline.enable_frame_sync()
except Exception as e:
print(e)
sys.exit(1)
def stop(self):
# 停止捕获
self.pipeline.stop()
self.active = False
if self.thread is not None:
self.thread.join() # 等待线程结束
def run(self):
if not self.active:
self.active = True
# 创建并启动线程目标函数是_capture_loop
self.thread = threading.Thread(target=self._capture_loop)
self.thread.start()
def _capture_loop(self):
try:
self.pipeline.start(self.config) # 启动流
except Exception as e:
print(e)
return
while self.active:
try:
frames: FrameSet = self.pipeline.wait_for_frames(100) # 等待获取帧
if frames is None:
continue
color_frame = frames.get_color_frame() # 获取颜色帧
if color_frame is None:
continue
color_image = frame_to_bgr_image(color_frame) # 转换颜色帧为RGB图像
self.color_image = color_image
if color_image is None:
print("failed to convert frame to image")
continue
depth_frame = frames.get_depth_frame() # 获取深度帧
if depth_frame is None:
continue
# 处理深度数据
depth_image = self.process_depth_data(depth_frame)
self.depth_image = depth_image
# 合成深度图像
combined_image = cv2.addWeighted(color_image, 0.5, depth_image, 0.5, 0) # 叠加图像
self.combined_image = combined_image
except KeyboardInterrupt:
break # 捕捉到中断信号则退出
def process_depth_data(self, depth_frame):
# 处理深度数据,转换为可视化的图像
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.uint16).reshape((height, width))
depth_data = depth_data.astype(np.float32) * scale
depth_image = cv2.normalize(depth_data, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U)
# 获取图像中心点的深度值
center_y = int(height / 2)
center_x = int(width / 2)
self.center_distance = depth_data[center_y, center_x]
return cv2.applyColorMap(depth_image, cv2.COLORMAP_JET)
def get_color_image(self):
return self.color_image
def get_depth_image(self):
return self.depth_image
def get_combined_image(self):
return self.combined_image
def get_center_distance(self):
return self.center_distance
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--mode", help="对齐模式: HW=硬件, SW=软件, NONE=禁用", type=str, default='HW')
parser.add_argument("-s", "--enable_sync", help="启用同步", type=bool, default=True)
args = parser.parse_args()
camera = OrbbecCamera(mode=args.mode, enable_sync=args.enable_sync)
camera.start()
print("中心点的深度值: ", camera.center_distance, "mm")

View File

@ -1,105 +0,0 @@
# 用于测试的临时文件
import cv2
from ultralytics import YOLO
from pyorbbecsdk import Pipeline, FrameSet
from pyorbbecsdk import Config
from pyorbbecsdk import OBSensorType, OBFormat
from pyorbbecsdk import OBError
from pyorbbecsdk import VideoStreamProfile
from img_process.orbbec_camera.utils import frame_to_bgr_image
def load_model():
# 载入 YOLOv8 模型
model = YOLO('yolov8n.pt')
return model
# 参数说明
# --img: 输入图片的路径
# --is_save_video: 是否保存识别后的视频
def process_img(img=None):
# 暂时性配置
video_path = "output_video.mp4"
# 加载模型
model = load_model()
if img is not None:
# 对图像运行 YOLOv8 推理
results = model(img)
# 在图像上可视化结果
annotated_img = results[0].plot()
# 显示带有标注的图像
cv2.imshow("YOLOv8 推理", annotated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
# 获取本机摄像头内容
cap = cv2.VideoCapture(0)
# 奥比中光摄像头
# ESC键的键值
ESC_KEY = 27
# 创建配置对象
config = Config()
# 创建Pipeline对象
pipeline = Pipeline()
try:
# 获取颜色传感器的流配置列表
profile_list = pipeline.get_stream_profile_list(OBSensorType.COLOR_SENSOR)
try:
# 尝试获取指定分辨率、格式和帧率的颜色配置
color_profile: VideoStreamProfile = profile_list.get_video_stream_profile(700, 0, OBFormat.RGB, 30)
except OBError as e:
# 如果出错,打印错误信息并使用默认的颜色配置
print(e)
color_profile = profile_list.get_default_video_stream_profile()
# 启用颜色流
config.enable_stream(color_profile)
except Exception as e:
# 如果出现异常,打印错误信息并返回
print(e)
return
# 启动Pipeline
pipeline.start(config)
while True:
try:
# 等待获取帧集最多等待100毫秒
frames: FrameSet = pipeline.wait_for_frames(100)
if frames is None:
continue
# 获取颜色帧
color_frame = frames.get_color_frame()
if color_frame is None:
continue
# 将帧转换为BGR格式的图像
color_image = frame_to_bgr_image(color_frame)
model.predict(source=color_image, show=True)
if color_image is None:
print("failed to convert frame to image")
continue
key = cv2.waitKey(1)
if key == ord('q') or key == ESC_KEY:
break
except KeyboardInterrupt:
# 如果捕获到键盘中断信号,则退出循环
break
# 释放视频捕获对象并关闭显示窗口
cap.release()
cv2.destroyAllWindows()
# 停止Pipeline
pipeline.stop()
if __name__ == '__main__':
process_img()