Ask Your Question
0

Error when loading torch model in OpenCV 3.4.1 Android

asked 2018-03-06 03:42:17 -0600

Pogona gravatar image

I'm trying to do face recognition on Android using OpenFace nn4.small2.v1 neural network.

To see if the network could be loaded with OpenCV, I tried to run a code taken from GitHub that uses the model. Using the prebuilt Windows version of OpenCV, I can run the code and the network is loaded without a problem.
However, If I try to load the network on Android, readNetFromTorch fails with the error :

"The function/feature is not implemented (Unsupported Lua type) in void cv::dnn::experimental_dnn_v4::TorchImporter::readObject(), file /build/master_pack-android/opencv/modules/dnn/src/torch/torch_importer.cpp, line 981".

I am using Xamarin for my app, so I have a Xamarin.Android project, and a Android shared library project where I have my C++ code.

Here's the C# code that calls the native code :

[DllImport("OpenCV", EntryPoint = "TryOpenCV", CharSet = CharSet.Ansi)]
private static extern int TryOpenCV_native([MarshalAs(UnmanagedType.LPStr)]string netPath, [MarshalAs(UnmanagedType.LPStr)]string cascadePath);  

public static int TryOpenCV(string netPath, string cascadePath)
{
    return TryOpenCV_native(newPath, cascadePath);
}

And here's the C++ code :

extern "C" int TryOpenCV(const char *netPath, const char *cascadePath)
{
    if (netPath && cascadePath)
    {
        cv::Mat mat = cv::Mat(96, 96, CV_8UC1);
        cv::String m_sNetPath(netPath);
        cv::String m_sCascadePath(cascadePath);

        try 
        {
            bool testNetPath = cv::utils::fs::exists(m_sNetPath);
            bool testCascadePath = cv::utils::fs::exists(m_sCascadePath);

            cv::CascadeClassifier cascade;
            cascade.load(m_sCascadePath);
        if (cascade.empty()) 
        {
            cv::String empty("cascade is empty");
        }

            cv::dnn::Net torchNet = cv::dnn::readNetFromTorch(m_sNetPath);
            cv::Mat inputBlob = cv::dnn::blobFromImage(mat, 1. / 255, cv::Size(96, 96), cv::Scalar(), true, false);
            torchNet.setInput(inputBlob);
            torchNet.forward();

            mat.release();

            return 0;
        }
        catch (cv::Exception &e)
        {
            cv::String m_exception(e.what());
            return 1;
        }
    }

    return returnInt();
}

The filepath I have for netpath and cascadePath is something like "/data/user/0/com.my.app/files/name.of.file.xxx".
I can create a Mat, both testNetPath and testCascadePath are true and the cascade load successfully (at least I hope so, because it's not empty), so OpenCV is working, can see the files, and so should load them both.

Stepping through the C++ code also gives me error when calling e.what() for some reason, but I get the error string when debugging the C# side.
I really can't see what's wrong here, but I am new to C++ so maybe I'm missing something obvious.

edit retag flag offensive close merge delete

Comments

hmm, i just tried, and it still works for me. are you really sure, it's opencv3.4.1, and https://raw.githubusercontent.com/pya... (should be: 30mb) ?

try a head openface.nn4.small2.v1.t7 or to peek into it with a text editor. maybe it was just a failed download, and youhave some html error msg instead of a network there ? (happens all too often !)

berak gravatar imageberak ( 2018-03-06 04:33:38 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-03-06 05:12:23 -0600

Pogona gravatar image

Turns out the problem was indeed with the file. It was downloaded correctly but my function to copy it from the apk to the sdcard was messing it up. Thanks !

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-03-06 03:42:17 -0600

Seen: 1,276 times

Last updated: Mar 06 '18