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