Python & openni2: use 2 cameras at the same time in different processes

Viewed 733

I'm using 2 cameras (Orbbec Astra Mini) with python3, opencv3.2 and openNi2 in xubuntu 16.04.

I can read 1 camera and I can choose which one read (changing dev[0])

dev = openni2.Device.open_all()
    depth_stream = dev[0].create_depth_stream()
    depth_stream.start()
    depth_stream.set_video_mode(c_api.OniVideoMode(pixelFormat = c_api.OniPixelFormat.ONI_PIXEL_FORMAT_DEPTH_100_UM, resolutionX = 320, resolutionY = 240, fps = 30))

The problem is that if I start another process which read the other camera it crash and says:

  File "/home/stark/.local/lib/python3.5/site-packages/primesense/openni2.py", line 230, in open_all
    return [cls(uri) for uri in cls.enumerate_uris()]
  File "/home/stark/.local/lib/python3.5/site-packages/primesense/openni2.py", line 230, in <listcomp>
    return [cls(uri) for uri in cls.enumerate_uris()]
  File "/home/stark/.local/lib/python3.5/site-packages/primesense/openni2.py", line 199, in __init__
    self._reopen()
  File "/home/stark/.local/lib/python3.5/site-packages/primesense/openni2.py", line 209, in _reopen
    c_api.oniDeviceOpen(self._orig_uri, ctypes.byref(self._handle))
  File "/home/stark/.local/lib/python3.5/site-packages/primesense/_openni2.py", line 2102, in wrapper
    raise OpenNIError(res, msg.strip(), logfile)
primesense.utils.OpenNIError: (OniStatus.ONI_STATUS_ERROR, b'Could not open "2bc5/0404@1/10": Failed to set USB interface!', None)

I tried to plug the cameras in the same bus and in a different one. Now they are plugged one in usb3.0 and the other in usb2.0.

They work both if called in a single process, when I start the second (same code with different device index) the second doesn't work.

EDIT WITH SOLUTION: I solved the problem creating a new function in openni2.py:

@classmethod
def open_any_available(cls, id):
    z= 0
    for cam in cls.enumerate_uris():
        if z== id:
            try:
                return cls(cam)
            except OpenNIError:
                pass
        z+=1
    raise Exception("no available cams")

Thanks to: https://3dclub.orbbec3d.com/t/python-openni2-use-2-cameras-at-the-same-time-in-different-processes/935

0 Answers
Related