简体   繁体   English

使用 Android 的 OpenCV 人脸识别

[英]OpenCV face recognition with Android

I am very new to android, as I got a project for maintenance.我对android很陌生,因为我有一个维护项目。 I completed other part of the project like authentication, token setting etc... In that face recognition is used to identify the person.我完成了项目的其他部分,如身份验证、令牌设置等......其中人脸识别用于识别人。

Previously it was working fine and taken images, trained with it and recognized the person.(Obviously not done by me :)).以前它工作正常并拍摄图像,用它训练并识别出这个人。(显然不是我做的:))。 Now it throws error as现在它抛出错误

Add Person ActivityCvException [org.opencv.core.CvException: cv::Exception: /build/master_pack-android/opencv/modules/core/src/matrix.cpp:1047: error: (-13) The matrix is not continuous, thus its number of rows can not be changed in function cv::Mat cv::Mat::reshape(int, int) const添加人员ActivityCvException [org.opencv.core.CvException: cv::Exception: /build/master_pack-android/opencv/modules/core/src/matrix.cpp:1047: error: (-13) 矩阵不连续,因此它的行数不能在函数 cv::Mat cv::Mat::reshape(int, int) const 中改变

Code sample is as follows 代码示例如下
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(); }

The input image needs to be stored as a continuous block of bytes in memory.输入图像需要作为连续的字节块存储在内存中。 I came to a similar situation, particularly because Android (at least the version I tried) does not seem to store images continuously.我遇到了类似的情况,特别是因为 Android(至少我尝试过的版本)似乎不会连续存储图像。 1) Check if the image is continuous using: inputImage.isContinuous() . 1)检查图像是否连续使用: inputImage.isContinuous() It returns a bool .它返回一个bool It should be true .应该是true 2) If the image is not continuous, you can create a copy of the image that it is by creating a clone of the image via inputImage.clone() . 2)如果图像不连续,您可以通过inputImage.clone()创建图像的clone来创建图像的inputImage.clone() Behind the scenes, we are creating a deep copy of the input using the create method, that should guarantee that the new matrix is continuous.在幕后,我们使用create方法创建输入的深层副本,这应该保证新矩阵是连续的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM