h264decoder
h264decoder copied to clipboard
Could some one help me. How to solve Oak-D pro camera data decoding and display
#every time different frame data size #May be it is commpress data
#this is my code
import cv2 import config import depthai as dai import subprocess as sp from os import name as osName import numpy as np import h264decoder import matplotlib.pyplot as pyplot import sys
''' Create pipeline''' pipeline = dai.Pipeline()
''' Define sources and output ''' camRgb = pipeline.create(dai.node.ColorCamera) videoEnc = pipeline.create(dai.node.VideoEncoder) xout = pipeline.create(dai.node.XLinkOut)
xout.setStreamName("h264")
''' Properties ''' camRgb.setBoardSocket(dai.CameraBoardSocket.RGB) camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P) videoEnc.setDefaultProfilePreset(camRgb.getVideoSize(), camRgb.getFps(), dai.VideoEncoderProperties.Profile.H264_MAIN)
''' Linking ''' camRgb.video.link(videoEnc.input) videoEnc.bitstream.link(xout.input)
''' video_in = cv2.VideoCapture("udpsrc port=12345 ! application/x-rtp-stream,encoding-name=JPEG ! rtpstreamdepay ! ''' rtpjpegdepay ! jpegdec ! videoconvert ! appsink", cv2.CAP_GSTREAMER) video_out = cv2.VideoWriter("appsrc ! videoconvert ! jpegenc ! rtpjpegpay ! rtpstreampay ! udpsink host=[destination-ip] port=12344", cv2.CAP_GSTREAMER, 0, 24, (1920, 1080), True)
img = None fig, ax = pyplot.subplots(1,1)
''' video_in = cv2.VideoCapture("udpsrc port=12345 ! application/x-rtp-stream,encoding-name=JPEG ! rtpstreamdepay !''' rtpjpegdepay ! jpegdec ! videoconvert ! appsink", cv2.CAP_GSTREAMER) video_out = cv2.VideoWriter("appsrc ! videoconvert ! jpegenc ! rtpjpegpay ! rtpstreampay ! udpsink host=[destination-ip] port=12344", cv2.CAP_GSTREAMER, 0, 24, (1920, 1080), True)
img = None fig, ax = pyplot.subplots(1,1)
def display(framedata): global img, fig, ax (frame, w, h, ls) = framedata print(w,' ',h,' ',ls) if frame is not None: print('frame size %i bytes, w %i, h %i, linesize %i' % (len(frame), w, h, ls)) frame = np.frombuffer(frame, dtype=np.ubyte, count=len(frame)) frame = frame.reshape((h, ls//3, 3)) frame = frame[:,:w,:]
if not img:
img = ax.imshow(frame)
pyplot.show(block = False)
else:
img.set_data(frame)
pyplot.draw()
pyplot.pause(0.001)
''' Two APIs to decode frames''' def run_decode_frame(decoder, data_in): while len(data_in): ''' One frame at a time, indicating how much of the input has been consumed.''' framedata, nread = decoder.decode_frame(data_in) data_in = data_in[nread:] display(framedata) def run_decode(decoder, data_in): ''' Consume the input completely. May result in multiple frames read.''' ''' This runs a bit faster than the other way.''' framedatas = decoder.decode(data_in) for framedata in framedatas: display(framedata)
def send(): ''' Connect to device and start pipeline''' with dai.Device(pipeline) as device: print("Oak-D") ''' Output queue will be used to get the encoded data from the output defined above''' q = device.getOutputQueue(name="h264", maxSize=30, blocking=True) decoder = h264decoder.H264Decoder() try: while True: frame = q.get().getData() # Blocking call, will wait until new data has arrived run_decode(decoder, frame) '''cv2.imshow('Original', data)''' key = cv2.waitKey(1) & 0xff if key == 27: break except: print("Exception") pass '''cv2.destroyAllWindows()''' video_out.release()
if name == 'main': send()