ShiqiYu / libfacedetection

An open source library for face detection in images. The face detection speed can reach 1000FPS.
Other
12.28k stars 3.05k forks source link

Can not alloc memory for data. #28

Closed ussernamenikita closed 7 years ago

ussernamenikita commented 7 years ago

I use your library in java with jni. And this error happens if i'm run "facedetect_frontal" function about 350-400 times. Can you tell me what mean this error ? How i can resolve it ? Thank you. `

include "faceSwap_OurNativeLib.h"

include "facedetect-dll.h"

include <opencv2/opencv.hpp>

include

include

include

using namespace std; using namespace cv;

define DETECT_BUFFER_SIZE 0x20000

JNIEXPORT jintArray JNICALL Java_faceswap_OurNativeLib_getPointsAndRec (JNIEnv env, jobject obj, jlong matAddr, jfloat scale, jint min_neighbors,jint minObjectWidth,jint maxObjectWidth,jint dolandmark) { jintArray rezult = nullptr; Mat inputMat = (Mat)matAddr; Mat gray; cvtColor(inputMat, gray, CV_BGR2GRAY); int pResults = NULL; //pBuffer is used in the detection functions. //If you call functions in multiple threads, please create one buffer for each thread! unsigned char pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE); if (!pBuffer) { fprintf(stderr, "Can not alloc buffer.\n"); rezult = env->NewIntArray(1); return rezult; }

///////////////////////////////////////////
// frontal face detection / 68 landmark detection
// it's fast, but cannot detect side view faces
//////////////////////////////////////////
//!!! The input image must be a gray one (single-channel)
//!!! DO NOT RELEASE pResults !!!
pResults = facedetect_frontal(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,
    scale, min_neighbors, minObjectWidth, maxObjectWidth, dolandmark);

if ((*pResults) <= 0)
{
    fprintf(stderr, "Nofaces.\n");
    rezult = env->NewIntArray(1);
    return rezult;
}

rezult = env->NewIntArray((*pResults) * 140);
if (rezult == nullptr)
{
    fprintf(stderr, "Can not alloc rezult memory.\n");
    rezult = env->NewIntArray(1);
    return rezult;
}
//jint bodyOfrezult[140];
//for (int i = 0; i < (pResults ? *pResults : 0); i++)
//{
//  short * p = ((short*)(pResults + 1)) + 142 * i;
//  for (int k = 0; k < 4; k++)
//  {
//      bodyOfrezult[k] = p[k];
//  }
//  if (dolandmark)
//  {
//      for (int j = 4; j < 140; j++)
//      {
//          bodyOfrezult[j] = ((int)p[2 + j]);
//      }
//      env->SetIntArrayRegion(rezult, i * 140, 140 , bodyOfrezult);
//  }
//}
free(pBuffer);
gray.release();
return rezult;

}

`

ShiqiYu commented 7 years ago

where is the error log?

2017-02-24 18:04 GMT+08:00 ussernamenikita notifications@github.com:

I use your library in java with jni. And this error happens if i'm run "facedetect_frontal" function about 350-400 times. Can you tell me what mean this error ? How i can resolve it ? Thank you. `

include "faceSwap_OurNativeLib.h"

include "facedetect-dll.h"

include <opencv2/opencv.hpp>

include

include

include

using namespace std; using namespace cv;

define DETECT_BUFFER_SIZE 0x20000

JNIEXPORT jintArray JNICALL Java_faceswap_OurNativeLib_getPointsAndRec (JNIEnv env, jobject obj, jlong matAddr, jfloat scale, jint min_neighbors,jint minObjectWidth,jint maxObjectWidth,jint dolandmark) { jintArray rezult = nullptr; Mat inputMat = (Mat)matAddr; Mat gray; cvtColor(inputMat, gray, CV_BGR2GRAY); int pResults = NULL; //pBuffer is used in the detection functions. //If you call functions in multiple threads, please create one buffer for each thread! unsigned char pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE); if (!pBuffer) { fprintf(stderr, "Can not alloc buffer.\n"); rezult = env->NewIntArray(1); return rezult; }

/////////////////////////////////////////// // frontal face detection / 68 landmark detection // it's fast, but cannot detect side view faces ////////////////////////////////////////// //!!! The input image must be a gray one (single-channel) //!!! DO NOT RELEASE pResults !!! pResults = facedetect_frontal(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step, scale, min_neighbors, minObjectWidth, maxObjectWidth, dolandmark);

if ((*pResults) <= 0) { fprintf(stderr, "Nofaces.\n"); rezult = env->NewIntArray(1); return rezult; }

rezult = env->NewIntArray((pResults) 140); if (rezult == nullptr) { fprintf(stderr, "Can not alloc rezult memory.\n"); rezult = env->NewIntArray(1); return rezult; } //jint bodyOfrezult[140]; //for (int i = 0; i < (pResults ? pResults : 0); i++) //{ // short p = ((short)(pResults + 1)) + 142 i; // for (int k = 0; k < 4; k++) // { // bodyOfrezult[k] = p[k]; // } // if (dolandmark) // { // for (int j = 4; j < 140; j++) // { // bodyOfrezult[j] = ((int)p[2 + j]); // } // env->SetIntArrayRegion(rezult, i * 140, 140 , bodyOfrezult); // } //} free(pBuffer); gray.release(); return rezult;

}

`

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ShiqiYu/libfacedetection/issues/28, or mute the thread https://github.com/notifications/unsubscribe-auth/AG0eHQVzoFIq2tzJpDh9KTBjlYkSeyE_ks5rfqs8gaJpZM4MLBsv .

-- Dr. Shiqi YU (于仕琪) School of Computer Science and Software Engineering, Shenzhen University, 518060, China +86-755-2673 3127

ussernamenikita commented 7 years ago

In Java :

A fatal error has been detected by the Java Runtime Environment: EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ff9714d2016, pid=2704, tid=0x0000000000000730

JRE version: Java(TM) SE Runtime Environment (8.0_112-b15) (build 1.8.0_112-b15) Java VM: Java HotSpot(TM) 64-Bit Server VM (25.112-b15 mixed mode windows-amd64 compressed oops) Problematic frame: C [pointsLib.dll+0x2016] Java_faceswap_OurNativeLib_getPointsAndRec+0x146

Failed to write core dump. Minidumps are not enabled by default on client versions of Windows

An error report file with more information is saved as: C:\Users\Rinat\Desktop\opencvFaceSwap\hs_err_pid2704.log

If you would like to submit a bug report, please visit: http://bugreport.java.com/bugreport/crash.jsp The crash happened outside the Java Virtual Machine in native code. See problematic frame for where to report the bug.

Can not alloc memory for data.

Process finished with exit code 1

hs_err_pid2704.txt

ShiqiYu commented 7 years ago

I don't know the reason. I also have no experience on JNI.

ussernamenikita commented 7 years ago

Can you tell, this message "Can not alloc memory for data." from your code or not ? Thank you.

ShiqiYu commented 7 years ago

from my code. It seems out of memory because malloc() failed.

guoming0000 commented 7 years ago

@ussernamenikita You should free pBuffer before 'return', or the malloc memory won't release by the system.

add " free(pBuffer); " after function facedetect_frontal before any " return; "

in your case, you can move the last free(pBuffer); sentence after facedetect_frontal function, like this:

pResults = facedetect_frontal(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,
    scale, min_neighbors, minObjectWidth, maxObjectWidth, dolandmark);

 free(pBuffer);
ussernamenikita commented 7 years ago

Hi, it's was problem in opencv. Thank you for help. Your library is very cool. P.S @guoming0000 I alredy free pBuffer in my code. Thank you for help.

junliu-cn commented 7 years ago

Can you tell more details about the problem as you described before caused by opencv?Do you fixed the problem?It will be better if you can show more details how to fix it.

Sent from my iPhone

On 25 Feb 2017, at 16:40, ussernamenikita notifications@github.com wrote:

Hi, it's was problem in opencv. Thank you for help. Your library is very cool. P.S @guoming0000 I alredy free pBuffer in my code. Thank you for help.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

ussernamenikita commented 7 years ago

Google says what this happens because GC can not get in time for free Mat object. For example this code will fail with OutOfMemory :

for(int i = 0; i < 500 ; i++)
{
    imread(filename);
}

this too :

Mat mat  = null;
for(int i = 0; i < 500; i++)
{
    if(mat != null)
    {
         mat.release();
    }
    mat = imread(filename);
}

But this code work fine

Mat mat  = null;
for(int i = 0; i < 10000; i++)
{
    if(mat != null)
    {
         mat.release();
    }
    mat = imread(filename);
    System.gc();
}
junliu-cn commented 7 years ago

Good examples!Thanks for your patience. One more question:Are you going to build a android edition of face detection demo through JNI?

On 26 Feb 2017, at 03:29, ussernamenikita notifications@github.com wrote:

Google says what this happens because GC can not get in time for free Mat object. For example this code will fail with OutOfMemory : for(int i = 0; i < 500 ; i++) { imread(filename); } this too : Mat mat = null; for(int i = 0; i < 500; i++) { if(mat != null) { mat.release(); } mat = imread(filename); } But this code work fine Mat mat = null; for(int i = 0; i < 10000; i++) { if(mat != null) { mat.release(); } mat = imread(filename); System.gc(); }

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

ussernamenikita commented 7 years ago

Thank you. Not now. Maybe in the future.