Android中的Opencv ANN_MLP培训 [英] Opencv ANN_MLP training in Android

查看:234
本文介绍了Android中的Opencv ANN_MLP培训的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在opencv C ++中实现了一个ANN字符分类器。我创建了一个模型:

I have implemented an ANN character classifier in opencv C++. I have created a model:

        cv::Mat layers(3,1,CV_32S);
        layers.at<int>(0,0) = ATTRIBUTES;//400
        layers.at<int>(1,0)=25;//hidden layer
        layers.at<int>(2,0) =CLASSES;// eg. 10

        CvANN_MLP nnetwork(layers, CvANN_MLP::SIGMOID_SYM,0.6,1);

        CvANN_MLP_TrainParams params(
           cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 1000, 0.000001),
                                        CvANN_MLP_TrainParams::BACKPROP,
                                        0.1, 0.1);
        int iterations = nnetwork.train(training_set, training_set_classifications,cv::Mat(),cv::Mat(),params);
        CvFileStorage* storage = cvOpenFileStorage( "C:\\example\\myModel.xml", 0, CV_STORAGE_WRITE );
        nnetwork.write(storage,"OCR");
        cvReleaseFileStorage(&storage);

现在,我的模型存储在C:/example/myModel.xml
当我想要使用ANN分类器,我在C ++中使用以下代码:

Now, my model is stored in C:/example/myModel.xml When I want to use ANN classifier, I use the following codes in C++:

CvANN_MLP nnetwork;

CvFileStorage* storage = cvOpenFileStorage("C:\\example\\myModel.xml", 0, CV_STORAGE_READ );
CvFileNode *n = cvGetFileNodeByName(storage,0,"OCR");
nnetwork.read(storage,n);
cvReleaseFileStorage(&storage);

现在我可以使用 nnetwork.predict()并对我的角色进行分类。我的问题在这里,我想在Android中使用相同的分类器。但是,我不知道如何在Android中的myModel.xml中加载模型。我正在使用opencv 3.0.0,我无法在Android opencv中找到 CvFileStorage 的Java conterpart。我不知道如何在Java中使用FileStorage。
请帮帮我。

Now I can use nnetwork.predict() and classify my characters. My question is here, I want to have the same classifier in Android. However, I don't know how to load the model in myModel.xml in Android. I am using opencv 3.0.0 and I cant find the Java conterpart of CvFileStorage in Android opencv. I don't know how to use FileStorage in Java. Please help me with that.

推荐答案

我发现没有


CvFileStorage

CvFileStorage

支持opencv4android。但是,如果你跳过jni,你可以做任何你想做的事情。我正在使用Android Studio,我很难找到解决方案。

support in opencv4android. However, you can do whatever you want if you leap through jni. I'm using Android Studio and I had a tough time to find the solution.

将jni与opencv链接到android有一个近乎复杂的过程。我将讨论整个过程如下:

There is a nearly complicated procedure to link jni with opencv to android. I discuss the whole procedure as below:

步骤1.在android项目中创建一个新类。称之为linkToNative。
添加从jni读取字符串的本机方法。

Step 1. Create a new class in your android project. Call it linkToNative. add the native method which reads a string from jni.

package com.example.yourname.yourproject;

public class linkToNative {

    public native String compare(long src,long dest);

    static {
        System.loadLibrary("compare");
    }

}

还有一件事,介绍你的家乡模块到app文件夹中的glradle.build。因此,您的gradle defaultConfig范围将如下所示:

One more thing, introduce your native module to the glradle.build in your app folder. Therefore, your gradle defaultConfig scope would be like this:

defaultConfig {
        applicationId "com.example.yourname.yourproject"
        minSdkVersion 10
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"

        ndk{
            moduleName 'compare'
                   }
    }

步骤2.建立项目

步骤3.使用javah创建标题。为此,请在终端中导航至[您的项目根目录] / app / src / main并键入:

Step 3. Use javah to create your headers. To do so, in your terminal, navigate to [Your project root directory]/app/src/main and type:

javah -d jni -classpath [Your Android sdk root directory]/platforms/android-19/android.jar;../../build/intermediates/classes/debug com.example.yourname.yourproject.linkToNative




  • 请注意,如果将其他包导入myClass.java,则需要添加相应的java或jar文件到类路径搜索区域。在使用opencv包装器的情况下,请参阅

    • Note that if you import other packages to myClass.java, you will be required to add the corresponding java or jar file to the classpath search area. In the case of using opencv wrappers see THIS.
    • 步骤4.在javah创建的jni文件夹中,你应该制作你的cpp [我强烈推荐cpp]文件。我创建一个简单的如下:

      Step 4. In jni folder created by javah, you should make your cpp[I strongly recommend cpp] file. I create a simple one as below:

      #include <com.example.yourname.yourproject.linkToNative.h>
      
      JNIEXPORT jstring JNICALL Java_com_example_yourname_yourproject_linkToNative_compare
      (JNIEnv *env, jobject obj, jlong src, jlong dest){
      return env->NewStringUTF("hello, I'm jni");
      }
      

      转到新创建的jni文件夹并创建两个名为Android.mk的文件和Application.mk如下:

      Go to the newly-created jni folder and create two files called Android.mk and Application.mk as below:

      LOCAL_PATH := $(call my-dir)
      
         include $(CLEAR_VARS)
      
      
          OPENCVROOT:= [PATH/TO/Your/Opencv4android/Directory]
          OPENCV_CAMERA_MODULES:=off
          OPENCV_INSTALL_MODULES:=on
          OPENCV_LIB_TYPE:=SHARED
          LOCAL_C_INCLUDE := ${OPENCVROOT}/sdk/native/jni/include/
          include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk
          LOCAL_LDLIBS += -llog -lstdc++ -lz
          LOCAL_MODULE    := compare
          LOCAL_SRC_FILES := compare.cpp
          include $(BUILD_SHARED_LIBRARY)
      



      Application.mk



      Application.mk

      APP_STL := gnustl_static
      APP_CPPFLAGS := -frtti -fexceptions
      APP_ABI := armeabi-v7a
      APP_PLATFORM := android-19
      

      现在你可以交叉编译你的na了使用ndk-build的代码。您应该已经下载了NDK并事先将其解压缩。然后,转到您的终端,导航到app文件夹并写下以下代码:

      Now you can cross-compile your native code using ndk-build. You should have downloaded NDK and extracted it beforehand. Then, go to your terminal, navigate to app folder and write down below code:

      [YOUR NDK FOLDER]\ndk-build.cmd NDK_PROJECT_PATH=build/intermediates/ndk NDK_LIBS_OUT=src/main/jniLibs APP_BUILD_SCRIPT=src/main/jni/Android.mk  NDK_APPLICATION_MK=src/main/jni/Application.mk
      

      请注意,如果您使用的是Linux或Mac平台,请从上面的命令中删除.cmd。等待代码编译并运行您的项目。现在,您可以在java中运行代码。
      每次更改cpp文件时,都应该编译上面提到的jni代码。如果你想摆脱这种情况,可以通过在其中定义任务来做到这一点。提供了一个很好的教程这里

      Note that if you are on linux or mac platforms, drop .cmd out of the above command. Wait for the code to compile and run your project. Now, you can run the code in java. Every time you change your cpp file, you should compile the jni codes as mentioned above. If you want to get rid of this, as gradle to do that by defining a task within it. A good tutorial to do that is provided HERE.

      StatModel有点改变在opencv3中。这是加载模型并根据testClass预测类的代码。事先,在cpp文件中包含以下库。

      StatModel is changed a bit in opencv3. This is the code that loads the model and predicts a class according to testClass. Beforehand, include the following libraries in your cpp file.

      #include <opencv/cv.h>
      #include "opencv2/opencv.hpp"
      #include "opencv2/core/core.hpp"
      #include "opencv2/highgui/highgui.hpp"
      #include "opencv2/imgproc/imgproc.hpp"
      #include "opencv2/objdetect/objdetect.hpp"
      #include <opencv2/ml/ml.hpp>
      

      现在,这是代码的其余部分。

      Now, this is the rest of the code.

      #define CLASSES 10
      using namespace cv;
      using namespace cv::ml;
      
      JNIEXPORT jstring JNICALL Java_com_example_yourname_yourproject_linkToNative_compare
        (JNIEnv *env, jobject obj, jlong addrGray , jlong dest){
        Mat* srcMat = (Mat*) src;
        Mat* destMat = (Mat*) dest;
        Mat ref = pMatYr->clone();
        Ptr<ANN_MLP> model=StatModel::load<ANN_MLP>("/storage/emulated/0/ALPR/param.xml");
        cv::Mat outputArray(1,CLASSES,CV_8U);
        //
        Your code that creates testClass according to srcMat 
        .
        .
        //
        model->predict(testClass,outputArray)
      
        return env->NewStringUTF("YOUR MSG!");
        }
      

      有关STatModel的更多信息,请阅读文档页面

      For more information about STatModel, read documentation page.

      这篇关于Android中的Opencv ANN_MLP培训的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆