• DepthAI-v2
  • Writting script is not respond after 40 frames had been writed

Hello. I had been writting python script to write RGBD data set.

import cv2

import depthai as dai

import numpy as np

from pathlib import Path

import time

def getFrame(queue):

# Get frame from queue

frame = queue.get()

# Convert frame to OpenCV format and return

return frame.getCvFrame()

def getMonoCamera(pipeline, isLeft):

# Configure mono camera

mono = pipeline.createMonoCamera()

# Set Camera Resolution

mono.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)

if isLeft:

# Get left camera

mono.setBoardSocket(dai.CameraBoardSocket.LEFT)

else:

# Get right camera

mono.setBoardSocket(dai.CameraBoardSocket.RIGHT)

return mono

def getStereoPair(pipeline, monoLeft, monoRight):

# Configure stereo pair for depth estimation

stereo = pipeline.createStereoDepth()

# Checks occluded pixels and marks them as invalid

stereo.setLeftRightCheck(True)

# Configure left and right cameras to work as a stereo pair

monoLeft.out.link(stereo.left)

monoRight.out.link(stereo.right)

return stereo

if __name__ == '__main__':

# Start defining a pipeline

pipeline = dai.Pipeline()

# Set up left and right cameras

monoLeft = getMonoCamera(pipeline, isLeft=True)

monoRight = getMonoCamera(pipeline, isLeft=False)

monoLeft.setFps(5)

monoRight.setFps(5)

# Combine left and right cameras to form a stereo pair

stereo = getStereoPair(pipeline, monoLeft, monoRight)

xoutDisp = pipeline.createXLinkOut()

xoutDisp.setStreamName("disparity")

xoutDepth = pipeline.createXLinkOut()

xoutDepth.setStreamName("depth")

xoutRectifiedLeft = pipeline.createXLinkOut()

xoutRectifiedLeft.setStreamName("rectifiedLeft")

xoutRectifiedRight = pipeline.createXLinkOut()

xoutRectifiedRight.setStreamName("rectifiedRight")

# Set output Xlink for left camera

xoutLeft = pipeline.createXLinkOut()

xoutLeft.setStreamName("left")

# Set output Xlink for right camera

xoutRight = pipeline.createXLinkOut()

xoutRight.setStreamName("right")

# Attach cameras to output Xlink

monoLeft.out.link(xoutLeft.input)

monoRight.out.link(xoutRight.input)

stereo.disparity.link(xoutDisp.input)

stereo.rectifiedLeft.link(xoutRectifiedLeft.input)

stereo.rectifiedRight.link(xoutRectifiedRight.input)

stereo.depth.link(xoutDepth.input)

with dai.Device(pipeline) as device:

depthQueue = device.getOutputQueue(name="depth", maxSize=1, blocking=False)

# Output queue will be used to get the grayscale frames from the output defined above

qRight = device.getOutputQueue(name="right", maxSize=1, blocking=False)

dirName = "color"

Path(dirName).mkdir(parents=True, exist_ok=True)

dirName_2 = "depth"

Path(dirName_2).mkdir(parents=True, exist_ok=True)

while True:

print("Start writting")

inRight = qRight.get()

img_gray = inRight.getCvFrame()

img_color = cv2.cvtColor(img_gray,cv2.COLOR_GRAY2BGR)

cv2.imshow("right", img_color)

inDepth = depthQueue.get()

cv2.imshow("depth", inDepth.getCvFrame())

cv2.imwrite(f"{dirName}/{int(time.time() * 1000)}.jpg", img_color)

cv2.imwrite(f"{dirName_2}/{int(time.time() * 1000)}.png", inDepth.getFrame())

if cv2.waitKey(1) == ord('q'):

break

But my script is not respond after 40 frames had been writed. What wrong in the script? Can you give me help? 
Sorry for bad indentation in the above code. But dicord not allowed attache py-file. Thanks
  • jakaskerl replied to this.
  • Hi ostepan2006
    You are creating too many xlinkout queues which are not getting read. By default, each queue will have space for 10 messages. You have 6, 4 of which are not used.

    Remove them and it should work.

    Thanks,
    Jaka

    Hi ostepan2006
    Is it always 40 frames? What happens if you lower the FPS? I think it might be that the queues are not getting read in time and get saturated.

    Thanks,
    Jaka

      jakaskerl Thanks

      The number of writted frames had been having value about 40. Your guess about FPS is the same as mine. I had been set a value of FPS to (3, 5, 10) but number of writted frames did not vary very much. Might it had been existing a code example for writting frames of color and gray cameras, depth images?

      Thanks

        jakaskerl

        If frames not had been writted my code not respond after some frames (about 35 - 40 frames) had been passed. Setted FPS is 5. My working station has: RAM 16 Gb, Intel Core i5-8500 CPU, Windows-10.

        Thanks

          Hi ostepan2006
          You are creating too many xlinkout queues which are not getting read. By default, each queue will have space for 10 messages. You have 6, 4 of which are not used.

          Remove them and it should work.

          Thanks,
          Jaka