Android OpenCV Tensorflow crashes with CvException because of native Memory leak
Hi,
I'm loading a default tensorflow dnn model with:
val config = getPath("ssd_mobilenet_v1_coco_2017_11_17.pbtxt", this)
val model = getPath("frozen_inference_graph.pb", this)
net = Dnn.readNetFromTensorflow(model, config)
and it works fine. But after less then 1-2 minutes the application crashes with CvException.
CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.1) /build/master_pack-android/opencv/modules/core/src/matrix.cpp:362: error: (-215) u != 0 in function void cv::Mat::create(int, const int*, int)
]
at org.opencv.imgproc.Imgproc.cvtColor_0(Native Method)
at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:1887)
at org.opencv.android.JavaCameraView$JavaCameraFrame.rgba(JavaCameraView.java:322)
at com.argos.android.opencv.activity.DnnActivity.onCameraFrame(DnnActivity.kt:84)
at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:392)
at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:373)
at java.lang.Thread.run(Thread.java:764)
Is there anything I can do to avoid this ?
I just used the Android Profiler and noticed that there is a memory leak, RAM goes up to 2 GB and then crashes the application - I'm using the sample code from the OpenCV tutorial for Caffee
val blob = Dnn.blobFromImage(frame, IN_SCALE_FACTOR, Size(IN_WIDTH.toDouble(), IN_HEIGHT.toDouble()), Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), false, true)
this line of code causes the memory leak. Not sure how I'd resolve this, cause the leak is happening in native code.
val
-- meaning: kotlin ?also, your exception is from cvtColor (invalid input) not from the dnn.
Thank you @berak! Yes kotlin. But I tried it in java as well, and I'm having the same issues. Yes, I realised it's not the Net, but a memory leak caused by the function blobFromImage.
Native memory keeps rising until the app crashes. Even if i comment out everything else and only run the method blobFromImage. This seems like a bug with the library... I hope this is fixable somehow I also tried calling the 1 paramter blobFromImage without cropping ect. same memory leak
ah right. if you really run out of mem, it might just crash anywhere ...
(also, while i DO like kotlin very much, you might want to lower the bar for anyone else trying to reproduce this, and stick with plain java, just saying)
Hi, @paatz04, may i know have you solve the memory leak of blobFromImage? I saw your comment in (https://github.com/opencv/opencv/issu...) that you suggest to manually release the blob Mat. I have done so using blob.release(), but somehow i still get the memory leak. Please help. TQ