How to convert gstreamer commands to python code

Hi all,

I’d like to convert gstreamer commands as below to python code.

gst-launch-1.0 v4l2src device=/dev/video0 ! ‘video/x-raw, width=1920, height=1080, framerate=60/1, format=UYVY’ ! nvvidconv ! ‘video/x-raw(memory:NVMM), width=1920, height=1080, framerate=60/1, format=I420’ ! nvoverlaysink sync=false

I have written as below. but, it’s not working.

================================================================================================
import cv2
import numpy as np
import gi
gi.require_version(“Gst”,“1.0”)
from gi.repository import Gst
import time

Gst.init(None)

class mygst:
def init(self):
self.pipeline = None
self.connection = None
self.client_address = None
self.bitrate = 2000000

self.control_queue = None

if self.pipeline is not None:
return

pipeline_string = " v4l2src device=/dev/video0" +
" ! ‘video/x-raw, width=1920, height=1080, framerate=60/1, format=UYVY’ " +
" ! nvvidconv " +
" ! ‘video/x-raw(memory:NVMM), format=NV12’ " +
" ! tee name=t t. " +
" ! queue " +
" ! omxh264enc bitrate=8000000 " +
" ! h264parse " +
" ! matroskamux " +
" ! filesink location=EO_image.mp4 t. " +
" ! queue " +
" ! nvtee " +
" ! nvoverlaysink sync=false "

self.pipeline = Gst.parse_launch(pipeline_string)

ret = self.pipeline.set_state(Gst.State.PLAYING)
if ret == Gst.StateChangeReturn.FAILURE:
print(“Unable to set pipeline to playing”)
self.pipeline.set_state(Gst.State.NULL)
self.pipeline = None

def main():

prevTime = time.time()

cap = cv2.VideoCapture(“EO_image.mp4”)

while True:
ret, frame = cap.read()

curTime = time.time()
sec = curTime - prevTime
fps = 1/sec

print("sec : ",sec)
print("fps : ",fps)

str = “FPS : %0.1f” % fps

cv2.putText(frame,str,(0,100),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0))

if not ret:
print(“Video Error”)
cv2.imshow(‘image’,frame)
cv2.imwrite(‘img.jpg’,frame)

#server = mygst()
#server.init()

if cv2.waitKey(50) & 0xFF == 27:
break
cv2.destroyAllWindows()
cap.release()

main()

================================================================================================

I want to know that working the gst command with the python code to the v4l2src method (not nvcamerasrc, filesrc).

Could you let me know wrong point?

Thanks & BR,

Hi nwlee,

We don’t have experience on converting gstreamer commands to python code, you may need to debug it by yourself.

Thanks

https://devtalk.nvidia.com/default/topic/979010/help-converting-gst-launch-command-to-python-/

This is an old thread in this regards from community developers for your reference.

Hi chijen,

Thank you for your support.

I have seen upper link already.

Actually, my code was made by reffering to your link.

But, this code is not working.

I’d like to know that pipeline of the gstreamer is working with python code normally.

If you have any other reference, please give me a help.

Thanks & BR,

Hi chijen,

Where can I contact this issue?

I’ll appreciate for you if you give me a information about the contact point.

Thanks & BR,