0

I'm trying to use facial recognition with Android . All the loads are ok , but the haarcascade_frontalface_alt2.xml file wich i don't know how to load it using JavaCV. This is the code i have:

public static void detectFacialFeatures()
{
    // The cascade definition to be used for detection.

    // This will redirect the OpenCV errors to the Java console to give you
    // feedback about any problems that may occur.
    new JavaCvErrorCallback();

    // Load the original image.

    // We need a grayscale image in order to do the recognition, so we
    // create a new image of the same size as the original one.
    IplImage grayImage = IplImage.create(iplImage.width(),iplImage.height(), IPL_DEPTH_8U, 1);

    // We convert the original image to grayscale.
    cvCvtColor(iplImage, grayImage, CV_BGR2GRAY);

    CvMemStorage storage = CvMemStorage.create();

    // We instantiate a classifier cascade to be used for detection, using the cascade definition.
    CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(cvLoad("./haarcascade_frontalface_alt2.xml"));

    // We detect the faces.
    CvSeq faces = cvHaarDetectObjects(grayImage, cascade, storage, 1.1, 1, 0);

    Log.d("CARAS","Hay "+faces.total()+" caras ahora mismo");
}

The problem is here

CvHaarClassifierCascade(cvLoad("./haarcascade_frontalface_alt2.xml"));

I have tried putting the xml file it into the /assets folder , but i have no idea of how must i load it. It's always giving me the next error:

03-26 17:31:25.385: E/cv::error()(14787): OpenCV Error: Null pointer (Invalid classifier cascade) in CvSeq* cvHaarDetectObjectsForROC(const CvArr*, CvHaarClassifierCascade*, CvMemStorage*, std::vector<int>&, std::vector<double>&, double, int, int, CvSize, CvSize, bool), file /home/saudet/projects/cppjars/OpenCV-2.4.4/modules/objdetect/src/haar.cpp, line 1514

... looking more near at the error it points to this code line:

CvSeq faces = cvHaarDetectObjects(grayImage, cascade, storage, 1.1, 1, 0);

That's why i'm pretty sure that the problem comes from the haarcascade_frontalface_alt2.xml load.

Thanks for your help.

P.D: I want to include the cascade into the apk not in sdcard .

4

2 回答 2

0

您可以将文件打包在 apk 中,然后将其复制到外部位置,以便 OpenCV 函数可以访问它。

    try {
        File learnedInputFile = new File(Environment.getExternalStorageDirectory().getPath() + "/learnedData.xml");
        if (!learnedInputFile.exists()) {
            InputStream learnedDataInputStream = assetManager.open("learnedData.xml");
            FileOutputStream learnedDataOutputStream = new FileOutputStream(learnedInputFile);

            // copy file from asset folder to external location, i.e. sdcard
            byte[] buffer = new byte[300];
            int n = 0;
            while (-1 != (n = learnedDataInputStream.read(buffer))) {
                learnedDataOutputStream.write(buffer, 0, n);
            }
        }
        classifier.load(learnedInputFile.getAbsolutePath());
    } catch (IOException exception) {
        // there are no learned data, train ml algorithm or show error, etc.
    }
于 2013-04-09T16:20:53.860 回答
0

如果您的级联在 SD 卡中,您可以使用:

CascadeClassifier cascade = new CascadeClassifier(Environment.getExternalStorageDirectory().getAbsolutePath() + "/cascade.xml");

Environment.getExternalStorageDirectory().getAbsolutePath()为您提供 SD 卡的正确路径,接下来 - 是您 SD 中文件的地址。

于 2013-03-26T22:19:08.480 回答