【问题标题】:OpenCV face recognition with Android使用 Android 进行 OpenCV 人脸识别
【发布时间】:2021-03-01 15:00:54
【问题描述】:

我对 android 很陌生,因为我有一个维护项目。我完成了项目的其他部分,如身份验证、令牌设置等......其中面部识别用于识别人。

以前它工作正常并拍摄图像,用它训练并识别出这个人。(显然不是我做的:))。现在它抛出错误

添加人员 ActivityCvException [org.opencv.core.CvException: 简历::异常: /build/master_pack-android/opencv/modules/core/src/matrix.cpp:1047: 错误:(-13)矩阵不连续,因此它的行数可以 不能在函数 cv::Mat 中改变 cv::Mat::reshape(int, int) const

代码示例如下
public void training() {
    Thread thread;
    try{
    PreferenceManager.setDefaultValues(getApplicationContext(), R.xml.preferences, false);
    }catch (Exception e){
        AddPersonActivity.this.runOnUiThread(new Runnable() {
            public void run() {
                WriteLog("Add Person Activity" +e.fillInStackTrace());
                errorAlert("Add Person Activity" +e.fillInStackTrace());
                VolleyHelper.progressDialog.dismiss();
            }
        });

    }
    WriteLog("training 1 ");

    final Handler handler = new Handler(Looper.getMainLooper());
    thread = new Thread(new Runnable() {
        public void run() {
            if (!Thread.currentThread().isInterrupted()) {
                try {
                    WriteLog("training 2 ");
                PreProcessorFactory ppF = new PreProcessorFactory(AddPersonActivity.this);
                PreferencesHelper preferencesHelper = new PreferencesHelper(AddPersonActivity.this);
                String algorithm = preferencesHelper.getClassificationMethod();

                FileHelper fileHelper = new FileHelper();
                fileHelper.createDataFolderIfNotExsiting();
                final File[] persons = fileHelper.getTrainingList();
                if (persons.length > 0) {
                    Recognition rec = RecognitionFactory.getRecognitionAlgorithm(getApplicationContext(), Recognition.TRAINING, algorithm);
                    for (File person : persons) {
                        if (person.isDirectory()) {
                            File[] files = person.listFiles();
                            int counter = 1;
                            for (File file : files) {
                                if (FileHelper.isFileAnImage(file)) {
                                    Mat imgRgb = Imgcodecs.imread(file.getAbsolutePath());
                                    Imgproc.cvtColor(imgRgb, imgRgb, Imgproc.COLOR_BGRA2RGBA);
                                    Mat processedImage = new Mat();
                                    imgRgb.copyTo(processedImage);
                                    List<Mat> images = ppF.getProcessedImage(processedImage, PreProcessorFactory.PreprocessingMode.RECOGNITION);
                                    if (images == null || images.size() > 1) {
                                        continue;
                                    } else {
                                        processedImage = images.get(0);
                                    }
                                    if (processedImage.empty()) {
                                        continue;
                                    }
                                    String[] tokens = file.getParent().split("/");
                                    final String name = tokens[tokens.length - 1];

                                    for (int i = 0; i < files.length; i++) {
                                        File myfile = new File(person +

                                                "\\" + files[i].getName());
                                        String long_file_name = files[i].getName();
                                        System.out.println(long_file_name);
                                        System.out.print(long_file_name);

                                        myfile.renameTo(new File(person +
                                                "\\" + long_file_name + "_101" + ".png"));
                                    }

                                    WriteLog("training 3 ");

                                    MatName m = new MatName("processedImage", processedImage);
                                    fileHelper.saveMatToImage(m, FileHelper.DATA_PATH);


                                    rec.addImage(processedImage, name, false);

                                    counter++;
                                }
                            }
                        }
                    }

                    try {

                        if (rec.train()) {
                            if (zipFileAtPath("/storage/emulated/0/Pictures/facerecognition/training/" + lcode, "/storage/emulated/0/Pictures/facerecognition/data/SVM/" + lcode + ".zip")) {
                                WriteLog("training 4 ");
                                if (zipFileAtPath("/storage/emulated/0/Pictures/facerecognition/data/SVM", "/storage/emulated/0/Pictures/facerecognition/" + "SVM_" + lcode + ".zip")) {
                                    WriteLog("training 5 ");
                                    fileupload(getintent.getStringExtra("lcode"));
                                } else {
                                    Toast.makeText(getApplicationContext(), "No Face Recognised", Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                Toast.makeText(getApplicationContext(), "No Face Recognised", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(getApplicationContext(), "Try Again", Toast.LENGTH_SHORT).show();
                            
                        }
                    } catch (Exception e) {
                        WriteLog("Add Person Activity" +e.fillInStackTrace());
                        errorAlert("Add Person Activity" +e.fillInStackTrace());

                    }


                    handler.post(new Runnable() {
                        @Override
                        public void run() {

                        }
                    });
                } else {
                    Thread.currentThread().interrupt();
                }

            } catch (Exception e) {
                    AddPersonActivity.this.runOnUiThread(new Runnable() {
                        public void run() {
                            VolleyHelper.progressDialog.dismiss();
                            WriteLog("Add Person Activity" +e.fillInStackTrace());
                            errorAlert("Add Person Activity" +e.fillInStackTrace());
                        }
                    });


            }

            }
        }
    });
    thread.start();
}

【问题讨论】:

    标签: android c++ opencv face-recognition


    【解决方案1】:

    输入图像需要作为连续的字节块存储在内存中。我遇到了类似的情况,特别是因为 Android(至少我尝试过的版本)似乎并没有连续存储图像。 1)检查图像是否连续使用:inputImage.isContinuous()。它返回一个bool。应该是true。 2) 如果图像不连续,您可以通过inputImage.clone() 创建图像的clone 来创建图像的副本。在幕后,我们正在使用create 方法创建输入的深层副本,这应该保证新矩阵是连续的。

     //check image before continuing:
     if ( !inputImage.isContinuous() ){ 
        //if the image is not continuous, create a deep copy:
        inputImage = inputImage.clone();
     }
    

    【讨论】:

      猜你喜欢
      • 2012-06-15
      • 2014-01-20
      • 2011-08-01
      • 2013-08-13
      • 1970-01-01
      • 2018-09-15
      • 2017-07-08
      • 1970-01-01
      相关资源
      最近更新 更多