记录Orbbec Astra使用python和OpenNI2进行流式处理

问题描述 投票:0回答:1

我正试图用这段代码记录Astra模型的深度和颜色流。它基本上可以正常工作,但是有一个问题,在成功录制之后,下一个不会记录深度流,没有任何错误或异常,除非您拔下并重新连接USB电缆。这让我认为方法write_files的最终指令不能正常工作,并且某些东西没有正确关闭(流,记录器,设备......)。我尝试了不同的关闭和停止组合而没有任何运气。 Ubuntu 16.04和Win10上的行为是相同的,使用Orbbec提供的最新OpenNI2软件包。相机是阿斯特拉。有帮助吗?

from datetime import datetime
import time
import argparse
import sys
import configparser

from openni import openni2
from openni import _openni2 as c_api

width = 640
height = 480
fps = 30
mirroring = True
compression = False
lenght = 300 #5 minutes


def write_files(dev):

    depth_stream = dev.create_depth_stream()
    color_stream = dev.create_color_stream()

    depth_stream.set_video_mode(c_api.OniVideoMode(pixelFormat=c_api.OniPixelFormat.ONI_PIXEL_FORMAT_DEPTH_1_MM,
                                                   resolutionX=width,
                                                   resolutionY=height,
                                                   fps=fps))
    color_stream.set_video_mode(c_api.OniVideoMode(pixelFormat=c_api.OniPixelFormat.ONI_PIXEL_FORMAT_RGB888,
                                                   resolutionX=width,
                                                   resolutionY=height,
                                                   fps=fps))
    depth_stream.start()
    color_stream.start()
    dev.set_image_registration_mode(True)
    dev.set_depth_color_sync_enabled(True)

    depth_stream.set_mirroring_enabled(mirroring)
    color_stream.set_mirroring_enabled(mirroring)

    actual_date = datetime.now().strftime("%Y%m%d-%H%M%S%f")[:-3]
    rec = openni2.Recorder((actual_date + ".oni").encode('utf-8'))
    rec.attach(depth_stream, compression)
    rec.attach(color_stream, compression)
    rec.start()
    print("Recording started.. press ctrl+C to stop or wait " + str(lenght) + " seconds..")
    start=time.time()
    try:
        while True:
            if (time.time()-start)>lenght:
                break
    except KeyboardInterrupt:
        pass
    rec.stop()
    depth_stream.close()
    color_stream.close()
    dev.close()
    rec.close()
def readSettings():
    global width,height,fps,mirroring,compression,lenght
    config = configparser.ConfigParser()
    config.read('settings.ini')
    width = int(config['camera']['width'])
    height = int(config['camera']['height'])
    fps = int(config['camera']['fps'])
    mirroring = config.getboolean('camera','mirroring')
    compression = config.getboolean('camera','compression')
    lenght = int(config['camera']['lenght'])

def main():

    readSettings()

    try:
        if sys.platform == "win32":
            libpath = "lib/Windows"
        else:
            libpath = "lib/Linux"
        openni2.initialize(libpath)
        print("Device initialized")
    except:
        print("Device not initialized")
        return
    try:
        dev = openni2.Device.open_any()
        write_files(dev)
    except:
        print("Unable to open the device")
    try:
        openni2.unload()
        print("Device unloaded")
    except:
        print("Device not unloaded")


if __name__ == '__main__':
    main()
python openni orbbec
1个回答
0
投票

我终于找到了解决方案..

只需移动这些说明即可

dev.set_image_registration_mode(True)
dev.set_depth_color_sync_enabled(True)

在流开始之前,将按预期工作。


推荐问答