我试图用这段代码记录Astra模型中的深度和颜色流。它基本上是工作的,但有一个问题,在成功记录后,下一个将不会记录深度流,没有任何错误或异常,除非您拔下并重新连接usb电缆。这让我想到write_files方法的最终指令不能正常工作,不知何故有些东西没有正确关闭(流、录像机、设备)。)。我尝试了不同的close和stop组合,但没有任何运气。使用Orbbec提供的最新OpenNI2包,在Ubuntu16.04和Win10上的行为是相同的。相机是阿斯特拉。有什么帮助吗?
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()发布于 2019-03-01 17:30:50
我终于找到了解决这个问题的办法..
只需移动这些指令
dev.set_image_registration_mode(True)
dev.set_depth_color_sync_enabled(True)在流开始之前,并将按预期工作。
https://stackoverflow.com/questions/54342497
复制相似问题