unable to load the haarcascadeshaarcascade.xml in opencv
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(cascad开发者_开发知识库e)
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
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!
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.
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////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
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";
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
manuallyFor Camera X (Kotlin), paste these in
onCreate()
or in yourImageAnalyzer
(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 inCascadeClassifier::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")
精彩评论