-
-
Save Erol444/f0349283663f5802d26965ac676af781 to your computer and use it in GitHub Desktop.
DepthAI RGB encoded & preview display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import depthai as dai | |
import cv2 | |
# Create pipeline | |
pipeline = dai.Pipeline() | |
# Define sources and output | |
camRgb = pipeline.createColorCamera() | |
videoEnc = pipeline.createVideoEncoder() | |
xout = pipeline.createXLinkOut() | |
xout.setStreamName('h265') | |
# Properties | |
camRgb.setBoardSocket(dai.CameraBoardSocket.RGB) | |
camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_4_K) | |
videoEnc.setDefaultProfilePreset(3840, 2160, 30, dai.VideoEncoderProperties.Profile.H265_MAIN) | |
# Send preview imgs to host as well | |
xoutRgb = pipeline.createXLinkOut() | |
xoutRgb.setStreamName("rgb") | |
# Linking | |
camRgb.preview.link(xoutRgb.input) | |
camRgb.video.link(videoEnc.input) | |
videoEnc.bitstream.link(xout.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 | |
q = device.getOutputQueue(name="h265", maxSize=30, blocking=False) | |
qRgb = device.getOutputQueue(name="rgb", maxSize=4, blocking=False) | |
# The .h265 file is a raw stream file (not playable yet) | |
with open('video.h265', 'wb') as videoFile: | |
print("Press Ctrl+C to stop encoding...") | |
try: | |
while True: | |
h265Packet = q.tryGet() # Blocking call, will wait until a new data has arrived | |
if h265Packet is not None: | |
h265Packet.getData().tofile(videoFile) # Appends the packet data to the opened file | |
previewIn = qRgb.tryGet() | |
if previewIn is not None: | |
cv2.imshow("preview", previewIn.getCvFrame()) | |
if cv2.waitKey(1) == ord('q'): | |
break | |
except KeyboardInterrupt: | |
# Keyboard interrupt (Ctrl + C) detected | |
pass | |
print("To view the encoded data, convert the stream file (.h265) into a video file (.mp4) using a command below:") | |
print("ffmpeg -framerate 30 -i video.h265 -c copy video.mp4") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment