I’m trying to use VideoCapture and VideoWriter to stream a camera feed using python. I need to capture the frame in order to do some basic processing before sending it to the writer. However, I noticed that my pipeline has significant 1-2 ms delay compared to the pipeline I run in the terminal. I think it’s because I have more video conversion in the python pipeline, but I’m not sure if I’m able to optimzie/shorten the pipeline.
Here is my python script:
import cv2 import keyboard if __name__ == '__main__': normal_cam = cv2.VideoCapture("v4l2src device=/dev/video2 io-mode=2 ! video/x-raw,format=YUY2,width=2560,height=720,framerate=60/1 ! nvvidconv ! \ video/x-raw(memory:NVMM) ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink drop=0", cv2.CAP_GSTREAMER) #start gstreamer video writer gst = "appsrc ! queue ! videoconvert ! video/x-raw, format=RGBA ! nvvidconv ! \ video/x-raw(memory:NVMM) ! xvimagesink " vw = cv2.VideoWriter(gst, cv2.CAP_GSTREAMER, 0, 30, (2560, 720)) while True: if keyboard.is_pressed('q'): break _, frame = normal_cam.read() vw.write(frame) cv2.destroyAllWindows()
And this is what I run in the terminal:
gst-launch-1.0 v4l2src device=/dev/video2 ! video/x-raw,format=YUY2,width=2560,height=720,framerate=30/1 ! xvimagesink
Obviously the terminal script is much shorter, however I’m unsure if I can shorten the python script while achieving the same functionality.