安卓利用opencv实现自定义图像识别

时间:2024-05-30 18:47:46

安卓利用opencv实现自定义图像识别

要在安卓上实现自定义图像识别,我们需要用到opencv-android-sdk先上效果图

安卓利用opencv实现自定义图像识别

下面来讲讲具体实现思路:

1、配置android studio中opencv的环境

2、训练自定义图像数据集

3、将训练好的数据集加载进行识别

  InputStream is = getResources().openRawResource(R.raw.cascade);
                        File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
                        mCascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml");
                        FileOutputStream os = new FileOutputStream(mCascadeFile);

                        byte[] buffer = new byte[4096];
                        int bytesRead;
                        while ((bytesRead = is.read(buffer)) != -1) {
                            os.write(buffer, 0, bytesRead);
                        }
                        is.close();
                        os.close();

                        mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
                        if (mJavaDetector.empty()) {
                            Log.e(TAG, "Failed to load cascade classifier");
                            mJavaDetector = null;
                        } else
                            Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());

                        mNativeDetector = new DetectionBasedTracker(mCascadeFile.getAbsolutePath(), 0);

                        cascadeDir.delete();

整个工程已上传有需要请复制下面链接:

https://download.****.net/download/sm16111/10703471

参考博客:

环境配置:https://blog.****.net/andrexpert/article/details/78993533

自定义目标训练:https://blog.****.net/wuxiaoyao12/article/details/39227189