Streaming video to aws kinesis from python using opencv and GStreamer

659 Views Asked by At

I'm trying to stream video to AWS Kinesis Video Streams using OpenCV and GStreamer, but I'm having trouble getting it to work. I have set up a Kinesis video stream with the name ExampleStream, and I want to use a numpy array as the source of the video frames.

Here's the code I'm using:

import cv2
import numpy as np
import time
from Utils.utils import get_aws_cred

access_key, secret_key, region = get_aws_cred()

pipeline = (
    f"appsrc ! videoconvert ! video/x-raw,format=BGR,width={400},height={400},framerate=25/1 ! videoconvert ! video/x-raw ! x264enc key-int-max=45 ! video/x-h264,stream-format=avc,alignment=au,profile=baseline ! kvssink stream-name=ExampleStream storage-size=512 aws-region={region} access-key={access_key} secret-key={secret_key}  max-latency=1 buffer-duration=1"
)

out = cv2.VideoWriter(pipeline, cv2.CAP_GSTREAMER, 0, float(25), (400, 400), True)

while True:
    frame = (np.random.rand(400, 400, 3) * 255).astype(np.uint8)
    out.write(frame)
    time.sleep(0.04)

There is no error or something I can debug to figure it out. Also when I am running a pipeline from my terminal its works, so I assume that the configuration in the aws is correct. The pipeline I am running through the terminal is:

"gst-launch-1.0 videotestsrc is-live=true ! video/x-raw,framerate=25/1 ! videoconvert ! x264enc  bframes=0 key-int-max=45 bitrate=500 ! video/x-h264,stream-format=avc,alignment=au,profile=baseline ! kvssink stream-name=ExampleStream storage-size=512 access-key=$AWS_ACCESS_KEY_ID secret-key=$AWS_SECRET_ACCESS_KEY aws-region=$AWS_REGION max-latency=1 buffer-duration=1"

any idea what's wrong with the pipeline or how to debug it?

1

There are 1 best solutions below

0
James Chen On

It's because your opencv doesn't support gstreamer. Use this to check

print(cv2.getBuildInformation())

If it says no, you need to build opencv from source code. Check this out.

And one last thing, you can't build opencv using Python3.11. There will be some dependency issue.