Memory leak running Darknet YOLO with OpenCV on a gstreamer input

Hello,

I have an issue when running darknet (built with jetpack Opencv) GitHub - AlexeyAB/darknet: YOLOv4 / Scaled-YOLOv4 / YOLO - Neural Networks for Object Detection (Windows and Linux version of Darknet ) on the jetson nano…(it is reproducible with any jetson or nvidia desktop GPU running darknet)

How to reproduce it:

  • Flash fresh jetpack 4.2.2
  • Get and compile darknet with the default OpenCV 3.3.1 with gstreamer support pre-installed with Jetpack
  • Run darknet on a cam with this gstreamer pipeline: “v4l2src device=/dev/video0 ! video/x-raw, framerate=30/1, width=640, height=360 ! videoconvert ! appsink”

Full command:

./darknet detector demo cfg/coco.data cfg/yolov3-tiny.cfg yolov3-tiny.weights "v4l2src device=/dev/video0 ! video/x-raw, framerate=30/1, width=640, height=360 ! videoconvert ! appsink" -ext_output -dont_show

When I do so, there is a big memory leak, RAM increase very fast until the nano crashes after a while. This can also be reproduced with this gstreamer input for raspberrypi cam:

"nvarguscamerasrc ! video/x-raw(memory:NVMM),width=1280, height=720, framerate=30/1, format=NV12 ! nvvidconv ! video/x-raw, format=BGRx, width=640, height=360 ! videoconvert ! video/x-raw, format=BGR ! appsink"

But this is not happening when using another input into darknet to get the webcam feed, which is:

./darknet detector demo cfg/coco.data cfg/yolov3-tiny.cfg yolov3-tiny.weights -c 0 -ext_output -dont_show

There doesn’t seem to be a leak happening…

So this doesn’t seem to be related with darknet, but with the OpenCV interface that feed the video stream into darknet.

This API is VideoCapture from OpenCV, and the only difference seems to be that when we input a Gstreamer pipeline, it uses:

VideoCapture::VideoCapture(const string& filename)

Whereas when we use a the -c 0 flag, it uses:

VideoCapture::VideoCapture(int device)

My guess is opencv uses Gstreamer for Video I/O if it is a Gstreamer pipeline input, otherwise it uses other Video I/O to hook directly to the webcam…

I might be wrong but I would say there is an issue with Gstreamer and OpenCV in this case, I tried to run on more recent version of OpenCV (compile it myself), but I always get this leak…

As Nvidia also use OpenCV in its Deepstream SDK … did you had issue with the VideoCapture API of OpenCV, any idea how might I solve this ?

Thanks

Hi,
It seems to be an issue in OpenCV since a pipeline without NVIDIA plugins also triggers it:

v4l2src device=/dev/video0 ! video/x-raw, framerate=30/1, width=640, height=360 ! videoconvert ! appsink

You may upgrade OpenCv and try again:
https://devtalk.nvidia.com/default/topic/1057460/jetson-tx2/custom-built-opencv-3-4-0-following-nvidia-advice-not-working/post/5363355/#5363355
Script for Jetson Naano: