Grabbing consistent FPS from USB camera and reading frames in python

I am attempting to read from a USB connected camera at a consistent FPS. I am doing frequency analysis on the frames so slight incorrect spacing between frames causes problems. Right now I have tried doing a threaded process with the following setup for cap.read(). I have ensured the exposure time is sufficient to operate at 30 fps.
gstr = ‘v4l2src device=/dev/video0 ! video/x-raw, format=(string)UYVY, width=(int)1280, height=(int)720, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1 } ! videoconvert ! video/x-raw,format=BGR ! appsink’
cap = cv2.VideoCapture(gstr, cv2.CAP_GSTREAMER)

I can also get away with inconsistent spacing if I know that exact time a frame was grabbed. Doing a time.time() in my cap.read() while loop produces inaccurate results.

On stackoverflow I found an answer that sugggests to stream the images to ram with ffmpeg and read them off from ram with python but not sure how to do that on the Jetson Xavier NX. How to maintain constant FPS when grabbing frames with opencv and python? - Stack Overflow

Any suggestions would be much appreciated.

Hi,
Please check if you can get consistent frame rate in gst-launch-1.0 command:

gst-launch-1.0 v4l2src device=/dev/video0 ! video/x-raw, format=(string)UYVY, width=(int)1280, height=(int)720, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1 } ! videoconvert ! video/x-raw,format=BGR ! fpsdisplaysink text-overlay=0 video-sink=fakesink sync=0 -v

If you get desired frame rate in gst-launch-1.0, please then try this sample:
V4l2src using OpenCV Gstreamer is not working in Jetson Xavier NX - #3 by DaneLLL

It takes significant CPU loading to run with OpenCV, please execute sudo nvpmodel -m 2 and sudo jetson_clcoks. To run CPU cores at max clock.

It’s pretty consistent in terminal. Is there anyway to have a text overlay of current timestamp on each frame and grab with opencv. Or have timestamp of each frame based on the buffer timestamp and extract with python? When I try adding in the text overlay with gstr = ‘…textoverlay commands’ and cap = cv2.VideoCapture(cgstr, cv2.CAP_GSTREAMER) it won’t grab any frames?

Hi,
Please set sync=0 to appsink for a try. This will disable synchronization mechanism in gstreamer. It may help the case.

finally resolved the issue. Just needed to make sure the threaded function grabbing the frames was not doing anything expect cap.read()

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.