0

I'm developing an android application that uses OpenCV C++ dll(.so file) in Unity. The C++ dll accesses a cascade file to use OpenCV functions.

*C++ DLL side code

cv::String face_cascade_name = "haarcascade_frontalface_alt.xml";
if (!face_cascade.load(face_cascade_name)) {
    printf("--(!)Error loading face cascade, please change face_cascade_name in source code.\n");
    return -1;
};

However If I build an android application in Unity and install the app on Android device, it fails to load the xml file.

How can I access the cascade xml in C++ DLL in Android apk?

*If I put the xml to assets/StreamingAssets in Unity, the xml file exists in myapp.apk/assets folder. enter image description here

mazend
  • 456
  • 1
  • 7
  • 37
  • where is the xml file on your android system? Does your application have the android rights to access that file (or the file system at all)? – Micka Apr 18 '18 at 11:06
  • If I put the xml to assets/StreamingAssets in Unity, the xml file exists in myapp.apk/assets folder.(I attached snapshot) However I don't know how to access the compressed(=apk) xml file from C++ side code. – mazend Apr 18 '18 at 11:34
  • https://stackoverflow.com/questions/8246917/how-to-access-unity-assets-natively-on-android-or-iphone – Micka Apr 18 '18 at 11:47
  • If I understand it right, you can use `Application.streamingAssetsPath + "/" + filename` to access your files? – Micka Apr 18 '18 at 11:49
  • @Micka I want to know how to access the xml file in the apk from C++ code(=so file in the apk). – mazend Apr 18 '18 at 12:28
  • Sorry, It seems like my poor English skill made you confused. – mazend Apr 18 '18 at 12:47
  • is it basically the same question as this one? https://stackoverflow.com/questions/18090483/fopen-fread-apk-assets-from-nativeactivity-on-android – Micka Apr 18 '18 at 13:41
  • @Micka, I think the question is quite similar with mine. But the difference is that I'm developing an android application in Unity with C#. – mazend Apr 18 '18 at 14:56

1 Answers1

1

Android is a special case since the StreamingAssets folder is compressed and unusable by the OpenCV file reader. You have to 1) copy the file from your StreamingAssets folder to the persistent data path (there's documentation on how to copy and write files in the unity manual), 2) fetch the name of that file using Application.persistantDataPath, like:

string cascadeFileName = (Application.persistentDataPath + "/" + fileName);
  • Caveat: this works with varying degrees of success with newer versions of OpenCV; for whatever reason, the standard "Init" wrapper that tutorials give did not recognize the persistent data path to the cascade file that I passed it. 4.0.1 had it working fine, 4.5.3 did not. – SavedByZero Jun 13 '21 at 18:16