#!/usr/bin/env python3
import depthai as dai
import cv2
import numpy as np
# Create pipeline
pipeline = dai.Pipeline()
# Define sources and output
camD = pipeline.create(dai.node.MonoCamera)
camC = pipeline.create(dai.node.MonoCamera)
videoEncD = pipeline.create(dai.node.VideoEncoder)
videoEncC = pipeline.create(dai.node.VideoEncoder)
xoutC = pipeline.create(dai.node.XLinkOut)
xoutD = pipeline.create(dai.node.XLinkOut)
xoutC.setStreamName('mjpegc')
xoutD.setStreamName('mjpegd')
# Properties
camD.setBoardSocket(dai.CameraBoardSocket.CAM_D)
camD.setResolution(dai.MonoCameraProperties.SensorResolution.THE_1200_P)
videoEncD.setDefaultProfilePreset(30, dai.VideoEncoderProperties.Profile.MJPEG)
camC.setBoardSocket(dai.CameraBoardSocket.CAM_C)
camC.setResolution(dai.MonoCameraProperties.SensorResolution.THE_800_P)
videoEncC.setDefaultProfilePreset(30, dai.VideoEncoderProperties.Profile.MJPEG)
# Linking
camD.out.link(videoEncD.input)
videoEncD.bitstream.link(xoutD.input)
camC.out.link(videoEncC.input)
videoEncC.bitstream.link(xoutC.input)
# Connect to device and start pipeline
with dai.Device(pipeline) as device:
# Output queue will be used to get the encoded data from the output defined above
qd = device.getOutputQueue(name="mjpegd", maxSize=30, blocking=True)
qc = device.getOutputQueue(name="mjpegc", maxSize=30, blocking=True)
# The .mjpeg file is a raw stream file (not playable yet)
with open('video.mjpeg', 'wb') as videoFile:
print("Press Ctrl+C to stop encoding...")
try:
while True:
mjpegPacketd = qd.get() # Blocking call, will wait until a new data has arrived
mjpegPacketc = qc.get() # Blocking call, will wait until a new data has arrived
mat_jpegc = np.frombuffer(mjpegPacketc.getData(), dtype=np.uint8)
matc = cv2.imdecode(mat_jpegc, cv2.IMREAD_UNCHANGED)
mat_jpegd = np.frombuffer(mjpegPacketd.getData(), dtype=np.uint8)
matd = cv2.imdecode(mat_jpegd, cv2.IMREAD_UNCHANGED)
cv2.imshow("camc", matc)
cv2.imshow("camd", matd)
cv2.waitKey(10)
# mjpegPacket.getData().tofile(videoFile) # Appends the packet data to the opened file
# print(len(mjpegPacket.getData()))
except KeyboardInterrupt:
# Keyboard interrupt (Ctrl + C) detected
pass
print("To view the encoded data, convert the stream file (.mjpeg) into a video file (.mp4) using a command below:")
print("ffmpeg -framerate 30 -i video.mjpeg -c copy video.mp4")
Above is for mixing 1200p and 800p, artefact on 1200p decoded stream.
Changing camD.setResolution(dai.MonoCameraProperties.SensorResolution.THE_1200_P) to 400p would yield error.