0

Im trying face detection using opencv in android but I unable to load the object detection xml files.

The code is as follows,

.....

static CvHaarClassifierCascade* cascade = 0;
  CvMemStorage* storage = 0;
  LOGI("before haarcascade");  

if (!cascade) {
    const char* file = "/Users/Downloads/OpenCV-

2.2.0/data/haarcascadeshaarcascade_frontalface_alt.xml";
    cascade = (CvHaarClassifierCascade *)cvLoad(file, 0, 0, 0);
    storage = cvCreateMemStorage(0);
  }  

if(cascade)
      LOGI("xml loaded");
  else
          LOGI("unable to load the xml");

......

In logcat it showing that unable to load the xml.

How to load the xml??

Please someone help me out.

Thanks, Srinivasan

Bastardo
  • 4,144
  • 9
  • 41
  • 60
srinivasan
  • 685
  • 9
  • 18

5 Answers5

2

Daft question... but the file does exist doesn't it?

If that code runs on your android device, but the file exists on the host (Mac OS) workstation you're out of luck!

Martin Thompson
  • 16,395
  • 1
  • 38
  • 56
  • yeah I tried by placing the xml with the package. Lets see I will try it by placing in my device and check... – srinivasan Apr 12 '11 at 14:12
1

Should be the path issue. Please write the full path or keep the xml file along with the code so that it picks it up directly.

0

Try Putting this before loading the cascade:

    /////////////////////////////////////////////////////////////////////////
///OpenCV Bug Work-around////////////////////////////////////////////////
IplImage* dummyImage = cvCreateImage(cvSize(1, 1), IPL_DEPTH_8U, 1);/////
cvErode(dummyImage, dummyImage);/////////////////////////////////////////
cvReleaseImage(&dummyImage);/////////////////////////////////////////////
///OpenCV Bug Work-around////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
rossb83
  • 1,694
  • 4
  • 21
  • 40
  • Yes I know it looks wierd, but its a known bug in opencv what you are experiencing. This is the work-around. It calls a function that is from a preceding library prior to calling cvload. Just put this code block just before cvLoad(..cascade...). – rossb83 Apr 13 '11 at 18:35
0

don't you need a directory separating slash between haarcades and haarcascade_frontalface....

const char* file = "/Users/Downloads/OpenCV-

2.2.0/data/haarcascades/haarcascade_frontalface_alt.xml";
David
  • 23
  • 3
0
  • For those who are struggling with loading haarcascade .xml file, it is because Android does not allow to access "etc" folder in OpenCV-Android (as I understand)

  • At first, I put haarcascade file in app/res/raw manually

  • For Camera X (Kotlin), paste these in onCreate() or in your ImageAnalyzer (file is created repeatedly every lifecycle)

         // Cannot load Haar Cascade file because path to OpenCV resources cannot be accessed directly(Android security)
         // So put model file manually in "/res/raw" resource folder
         val haarRelativePath = "/res/raw/haarcascade_frontalface_alt2.xml"
    
         // then read file as input stream and copy stream to accessible temporary file
         val haarCascadeIns = javaClass.getResourceAsStream(haarRelativePath)
    
         // Declare default temporary file location in Android
         var tmpDir = System.getProperty("java.io.tmpdir")
    
         // JNI now can access temporary file and utilize model
         val tmpFile =  File(tmpDir, "haarcascade_frontalface.xml")
         haarCascadeIns.use { input ->
             tmpFile.outputStream().use { output ->
                 input?.copyTo(output)
             }
         }
    
  • haarCascadePath, or Cascade Classifier temp location, would be at /data/user/0/com.example.yourapplicationname/cache/classifier[some random number]haarcascade_frontalface . Then I can use this path in CascadeClassifier::load() function in JNI

         // Get absolute path to "tmpFile" and pass it to JNI function 
         val haarCascadePath = tmpFile.absolutePath

         // Other way to get file path
         val haarCascadePath =  File(tmpDir, "haarcascade_frontalface.xml")
furry cat
  • 1
  • 3