Saving image from pipeline to disk with GPU

Complete information of setup:

• Hardware Platform (Jetson / GPU): dGPU
• DeepStream Version: 6.4
• TensorRT Version: 8.6.1.6
• NVIDIA GPU Driver Version (valid for GPU only): 535
• Issue Type( questions, new requirements, bugs): question
• OS: Ubuntu22.04

I have found these two posts about saving images to disk from deepstream directly by GPU, this and this.

My main goal is to save every nth frame to disk (currently, doing it in probe using cv2). I will try to create bindings to the above and use in DS6.4.

Can you please kindly tell if creating python bindings using code in deepstream-image-meta-test is still the way, or if any python native way is available in Deepstream 8.0 and above?

  1. Please do not use cv2 to save images, as this causes data to be copied from the GPU to the CPU.

  2. If using DS-6.4, you need to add the bindings yourself and then compile the code; please refer to this FAQ.
    DeepStream SDK FAQ - #50 by junshengy
    This FAQ also provides an API demo.

  3. It is recommended to use DS-9.1 and install pyds from source, which eliminates the need to manually add bindings.
    deepstream_python_apps/bindings/README.md at master · NVIDIA-AI-IOT/deepstream_python_apps · GitHub

Thank you very much for quick reply @junshengy . Found the pyds.nvds_obj_enc_process from your answer in pyds 8.0. Also, thanks for pointing to the usage in lower DS versions.

Hi @junshengy , I have two more questions, please kindly answer them.

I have been able to use the function pyds.nvds_obj_enc_process in both Deepstream 8.0 with official pyds release, and in Deepstream 6.4 with pyds built using your answer.

Question 1: The changes that I make to image from nvds_buf_surface are also showing up in the image saved by nvds_obj_enc_process. Is this expected?

I was thinking that the changes made by cv2 won’t show up in the saved image as nvds_obj_enc_process saves image from GPU to disk by GPU itself.

Parts of my code:

image_frame = pyds.get_nvds_buf_surface(hash(gst_buffer), frame_meta.batch_id)
# HERE I do some cv2 operations on the above image_frame like drawing bboxes.
# then
if config.SAVE_DIRECTLY_FROM_GPU:
    print("Saving directly by GPU")
    nvds_enc_args = pyds.NvDsObjEncUsrArgs()
    # filling them here: saving frame, no scaling, quality 95
    pyds.nvds_obj_enc_process(obj_ctx_handle, nvds_enc_args, hash(gst_bffer), None, frame_meta)  # Please note that I am not referring to the above image_frame variable in any manner here.
else:
    print("Saving by cv2")
    cv2.imwrite(frameloc, image_frame)

Extra context for this question: I am doing the cv2 operations because I want to put the result image in a SharedArray for a different purpose. I was testing nvds_obj_enc_process only for speed boost compared to cv2’s imwrite.

Question 2: nvds_obj_enc_process is taking more time to save the frame compared to cv2 imwrite. Is this expected?

I was thinking hardware encoding by GPU will be faster. I have one NVENC on my GPU. It is GeForce GTX 1650, and has one NVENC engine on it as mentioned here.

The function nvds_obj_enc_process prints the time taken for saving each frame as I set calcEncodeTimeto 1 in NvEncObjUsrArgs. Similarly, I printed time calculated for cv2 imwrite by placing time.perf_counter before and after. Details are mentioned below which are mean and median of several frames.

Image saver Mean time(msec) Median time(msec)
cv2 imwrite 2.1936 2.0590
nvds_obj_enc_process 4.6912 4.5280

Details mentioned above are for DS6.4 (similar difference was seen in DS 8.0 i.e. cv2 being faster, although both were higher compared to their corresponding DS6.4 values.

1.Yes, this is expected. get_nvds_buf_surface exposes the underlying frame buffer, so in-place OpenCV modifications are also visible to nvds_obj_enc_process.

2.The GTX 1650’s NVENC engine does not encode JPEG. GPU JPEG encoding is not guaranteed to have lower single-image latency; its advantage may appear when processing many images concurrently.
I suspect this issue is likely caused by the need to copy data from the GPU to the CPU when the dGPU saves JPEGs; this approach offers advantages when using large batch sizes.
Timing only cv2.imwrite excludes any GPU-to-CPU data transfer performed beforehand. Also, calcEncodeTime and the elapsed time around cv2.imwrite have different measurement boundaries. For a fair comparison, use identical images and JPEG settings, encode to memory on both sides, and measure completion time, including nvds_obj_enc_finish for Object Encoder

Thank you very much.