Ask Your Question

Revision history [back]

OpenCV insufficient memory crash on iOS

I created a fisherface face recognizer an am trying to train it. When I call the train function it returns this error in the logs.

 FaceDetector(1872,0x1b625bb80) malloc: *** mach_vm_map(size=2016002048) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
OpenCV(3.4.0-dev) Error: Insufficient memory (Failed to allocate 2016000000 bytes) in OutOfMemoryError, file /Users/Name/Desktop/OpenCVBuild/opencv/modules/core/src/alloc.cpp, line 55
OpenCV(3.4.0-dev) Error: Assertion failed (u != 0) in create, file /Users/mustafa/Desktop/OpenCVBuild/opencv/modules/core/src/matrix.cpp, line 436
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: OpenCV(3.4.0-dev) /Users/Name/Desktop/OpenCVBuild/opencv/modules/core/src/matrix.cpp:436: error: (-215) u != 0 in function create

(lldb)

Here is my training function:

-(void) trainFaceRecognitionModel2 {
    NSArray * imageArray2;
    imageArray2 = [NSArray arrayWithObjects:[UIImage imageNamed:@"IMG_1781"], [UIImage imageNamed:@"IMG_1782"], [UIImage imageNamed:@"IMG_1783"], [UIImage imageNamed:@"IMG_1784"], [UIImage imageNamed:@"IMG_1785"], [UIImage imageNamed:@"IMG_1786"], [UIImage imageNamed:@"IMG_1787"], [UIImage imageNamed:@"IMG_1788"], [UIImage imageNamed:@"IMG_1789"], [UIImage imageNamed:@"IMG_1790"], [UIImage imageNamed:@"IMG_1791"], [UIImage imageNamed:@"IMG_1792"], [UIImage imageNamed:@"IMG_1793"], [UIImage imageNamed:@"IMG_1794"], [UIImage imageNamed:@"IMG_1795"], [UIImage imageNamed:@"IMG_1796"], [UIImage imageNamed:@"IMG_1797"], [UIImage imageNamed:@"IMG_1798"], [UIImage imageNamed:@"IMG_1799"], [UIImage imageNamed:@"IMG_1800"], [UIImage imageNamed:@"IMG_1801"], [UIImage imageNamed:@"1801"], [UIImage imageNamed:@"1802"], [UIImage imageNamed:@"IMG_1803"], nil];

    //1781 - 1803

    NSMutableArray * trainArray;

    cv::Mat grayMat;
    std::vector<cv::Mat> images = vector<cv::Mat>();
    std::vector<int> labels;


    for (UIImage* image in imageArray2) {
        cv::Mat imageMat;
        UIImageToMat(image, imageMat);


        cv::cvtColor(imageMat, grayMat, CV_BGR2GRAY);

        images.push_back(grayMat);
        labels.push_back(2);

        printf("training model...");

    }

    model2 -> train(images, labels); //CRASH ON THIS LINE


}

I am not sure that I am training the model right. How would I reduce the memory usage and solve the problem?