2013-03-26 33 views
0

我正在嘗試在Android上使用面部識別。所有的負載都沒問題,但haarcascade_frontalface_alt2.xml文件,我不知道如何使用JavaCV加載它。 這是我的代碼有:如何使用JavaCV在Android中加載ClassifierCascade?

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"); 
} 

的問題是在這裏

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

我試圖把XML文件入/資產文件夾,但我不知道該怎樣我必須加載想法它。它總是給我的下一個錯誤:

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

這就是爲什麼我敢肯定,問題就來了:

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 

... 它指向該行代碼的錯誤尋找更近來自haarcascade_frontalface_alt2.xml加載。

感謝您的幫助。

P.D:我想包括級聯到apk中不在SD卡中。

回答

0

如果級聯是SD卡,您可以使用:

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

Environment.getExternalStorageDirectory().getAbsolutePath()給你的SD卡和未來正確的道路 - 是地址在您的SD文件。

+0

對不起,但我正在等待包含在apk級聯文件中。 – 2013-03-26 22:39:20

+0

我有同樣的問題。但仍然不知道該怎麼做。我嘗試從'file:/// android_asset/cascade.xml'和[也是這個](http://stackoverflow.com/a/8475135/2099287)這樣的資源中讀取,但它不成功。也許它是特定的設備,在某些設備上它會工作。我認爲的方式是 - 從資產寫入SD到SD,讀取和刪除後。也許將來會是最好的方式。 – McBodik 2013-03-27 10:37:29

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. 
    } 
+0

這實際上不是JavaCV,但官方的Java API綁定的OpenCV,但在你的情況CvLoad應該工作,而不是我的CvStatModel :: load()函數。 – marci003 2013-04-09 16:33:16