Save frames extracted from Deepstream pipeline to video using OpenCV

Please provide complete information as applicable to your setup.

• Hardware Platform (Jetson / GPU) Jetson Nano
• DeepStream Version 6.0
• JetPack Version (valid for Jetson only) 4.6

I’m trying to save extracted frames from a Deepstream pipeline to video with OpenCV but all I end up is with a 9KB file.

This is my code (executed inside a probe function):

batch_meta = pyds.gst_buffer_get_nvds_batch_meta(hash(gst_buffer))
l_frame = batch_meta.frame_meta_list
frame_meta = pyds.NvDsFrameMeta.cast(
frame = pyds.get_nvds_buf_surface(hash(gst_buffer), frame_meta.batch_id)
frame_copy = np.array(frame, copy=True, order='C')            
frame_copy = cv2.cvtColor(frame_copy, cv2.COLOR_RGBA2BGRA)

The above code is executed each time the probe function is invoked. Images are saved to a queue:


After the required number of frames has been push into the queue, I use below code to save the buffered frames to a video file:

codec = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('out.avi', codec, fps, (output_width, output_height))

while total_frames > 0:                
   frame = self.frame_buffer.get() 
   total_frames -= 1

Unfortunately the file produced is not a valid video file. Is there sth I am doing wrong in the above process? Any help would be greatly appreciated.

P.S. Just to test that the frames have been correctly stored inside the queue, if I attempt to save the frames as images inside the while loop:

cv2.imwrite(dest_folder + '/' + f'tmp{total_frames}.png', frame)

I get properly saved and valid png images.

I think the issue is that frame is RGBA instead of RGB. out.write fails because of this (?).

Is there some Gstreamer element I could use to convert from RGBA to RGB?

can you have a try with nvvideoconvert?

The solution to my problem was simple. Instead of above command I used following command to convert coloring scheme:

frame_copy = cv2.cvtColor(frame_copy, cv2.COLOR_RGBA2BGR)

After this I was able to successfully save extracted frames to video using OpenCV.

1 Like

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