奥比中光相机Gemini pro使用

奥比中光相机Gemini pro使用

前言:使用python获取深度图和颜色图

一、环境准备

安装python3 opencv numpy等环境:
pip3 install opencv-python` `pip3 install numpy

二、拷贝库文件

将所需的库文件放至main程序路径下。

2.1 拷贝lib/python_lib/*.pyd和/lib/c_lib/*.dll到Samples目录
2.2 在Samples目录执行python HelloOrbbec.py等测试例子
2.3 Samples目录SyncAlignViewer.py测试例子,按D键开关对齐

关于配置文件:

​ 如果需要修改配置文件,操作如下
​ Samples/OrbbecSDKConfig_v1.0.xml,可以按照格式修改配置文件内容
​ 执行测试程序,程序会读取配置文件

python3.8代码:

#导入模块
from ObTypes import *
from Property import *
import Pipeline
import StreamProfile
from Error import ObException
import cv2
import numpy as np
import sys
import math

q = 113
ESC = 27

try:
  # 创建管道Pipeline,它是整个高级 API 的入口点,可以通过管道轻松打开和关闭多种类型的流并获取一组帧数据
  pipe = Pipeline.Pipeline(None, None)
  # 通过创建配置来配置要在管道中启用或禁用的流
  config = Pipeline.Config()

#窗口宽度和高度初始化,设为0
  windowsWidth = 0
  windowsHeight = 0
  windowsWidth2 = 0
  windowsHeight2 = 0
  try:
    # 获取深度摄像头的所有流配置,包括流分辨率、帧速率和帧格式

    depthprofiles = pipe.getStreamProfileList(OB_PY_SENSOR_DEPTH)
    colorprofiles = pipe.getStreamProfileList(OB_PY_SENSOR_COLOR)

    depthvideoProfile = None
    colorvideoProfile = None
    try:
      #选择默认分辨率打开流,可以通过配置文件配置默认分辨率
      depthvideoProfile = depthprofiles.getProfile(0)
      colorvideoProfile = colorprofiles.getProfile(0)
    except ObException as e:
      print("function: %s\nargs: %s\nmessage: %s\ntype: %d\nstatus: %d" %(e.getName(), e.getArgs(), e.getMessage(), e.getExceptionType(), e.getStatus()))
    #深度图及窗口配置  
    depthProfile = depthvideoProfile.toConcreteStreamProfile(OB_PY_STREAM_VIDEO)
    windowsWidth = depthProfile.width()
    windowsHeight = depthProfile.height()
    config.enableStream(depthProfile)
    #颜色图及窗口配置
    colorProfile = colorvideoProfile.toConcreteStreamProfile(OB_PY_STREAM_VIDEO)
    windowsWidth2 = colorProfile.width()
    windowsHeight2 = colorProfile.height()
    config.enableStream(colorProfile)
   #输出错误信息
  except ObException as e:
    print("function: %s\nargs: %s\nmessage: %s\ntype: %d\nstatus: %d" %(e.getName(), e.getArgs(), e.getMessage(), e.getExceptionType(), e.getStatus()))
    print("Current device is not support depth sensor!")
    sys.exit()


  #启动 Config 中配置的流,如果没有传递任何参数,它将启动默认配置启动流。
  pipe.start(config, None)

  # 获取镜像属性是否具有可写权限
  if pipe.getDevice().isPropertySupported(OB_PY_PROP_DEPTH_MIRROR_BOOL, OB_PY_PERMISSION_WRITE):
    # 设置镜像
    pipe.getDevice().setBoolProperty(OB_PY_PROP_DEPTH_MIRROR_BOOL, True)
  if pipe.getDevice().isPropertySupported(OB_PY_PROP_COLOR_MIRROR_BOOL, OB_PY_PERMISSION_WRITE):
    pipe.getDevice().setBoolProperty(OB_PY_PROP_COLOR_MIRROR_BOOL, True)

  while True:

    # 以阻塞方式等待数据帧,数据帧是包含配置中启用的所有流的帧数据的复合帧,并将帧等待超时设置为 100ms
    frameSet = pipe.waitForFrames(100)
    if frameSet == None:
      continue
    else:
      # 在窗口中渲染两组帧,深度帧、颜色帧
      depthFrame = frameSet.depthFrame()
      colorFrame = frameSet.colorFrame()

      if depthFrame != None:
        depthsize = depthFrame.dataSize()
        depthdata = depthFrame.data()

        if depthsize != 0:
          # 将帧数据的大小调整为(高度,宽度,2)
          data = np.resize(depthdata,(windowsHeight, windowsWidth, 2))
          
          # 将帧数据从 8 位转换为 16 位
          newData = data[:,:,0]+data[:,:,1]*256          
          # 将帧数据转换为 1mm 单位
          newData = (newData * depthFrame.getValueScale()).astype('uint16')
          # 渲染显示
          newData = (newData / 32).astype('uint8')
          # 将帧数据灰色转换为 RGB
          newData = cv2.cvtColor(newData, cv2.COLOR_GRAY2RGB)
          cv2.namedWindow("DepthViewer", cv2.WINDOW_NORMAL)
          cv2.imshow("DepthViewer", newData)
          key = cv2.waitKey(1)
      if colorFrame != None:
          # 要获取帧的大小和数据,请执行以下操作:
        colorsize = colorFrame.dataSize()
        colordata = colorFrame.data()
        if colorsize != 0:
          newData = colordata
          if colorFrame.format() == OB_PY_FORMAT_MJPG:
            newData = cv2.imdecode(newData, 1)
            if newData is not None:
                newData = np.resize(newData, (windowsHeight2, windowsWidth2, 3))
            elif colorFrame.format() == OB_PY_FORMAT_RGB888:
              newData = np.resize(newData, (windowsHeight2, windowsWidth2, 3))
              newData = cv2.cvtColor(newData, cv2.COLOR_RGB2BGR)
            elif colorFrame.format() == OB_PY_FORMAT_YUYV:
              newData = np.resize(newData, (windowsHeight2, windowsWidth2, 2))
              newData = cv2.cvtColor(newData, cv2.COLOR_YUV2BGR_YUYV)
            elif colorFrame.format() == OB_PY_FORMAT_UYVY:
              newData = np.resize(newData, (windowsHeight2, windowsWidth2, 2))
              newData = cv2.cvtColor(newData, cv2.COLOR_YUV2BGR_UYVY)
            elif colorFrame.format() == OB_PY_FORMAT_I420:
              newData = newData.reshape((windowsHeight2 * 3 // 2, windowsWidth2))
              newData = cv2.cvtColor(newData, cv2.COLOR_YUV2BGR_I420)
              newData = cv2.resize(newData, (windowsWidth2, windowsHeight2))
			#显示窗口
            cv2.namedWindow("ColorViewer", cv2.WINDOW_NORMAL)
          if newData is not None:
              cv2.imshow("ColorViewer", newData)
           #输入ESC或者q销毁所以窗口
          if key == ESC or key == q:
            cv2.destroyAllWindows()
            break
  pipe.stop()

except ObException as e:
  print("function: %s\nargs: %s\nmessage: %s\ntype: %d\nstatus: %d" %(e.getName(), e.getArgs(), e.getMessage(), e.getExceptionType(), e.getStatus()))

你可能感兴趣的:(python,opencv,开发语言)