使用Android的OpenCV人脸识别



我对android非常陌生,因为我有一个项目需要维护。我完成了项目的其他部分,如身份验证、令牌设置等。在这方面,人脸识别是用来识别人的

以前,它工作得很好,拍摄了图像,用它进行了训练,并认出了这个人。(显然不是我做的:((。现在它抛出错误作为

添加人员活动CvException[org.opencv.core.CvException:cv::异常:/build/master_pack-android/opencv/modules/core/src/matrics.cpp:1047:错误:(-13(矩阵不是连续的,因此它的行数可以在函数cv::Mat cv::Mat::整形(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(至少是我尝试过的版本(似乎无法连续存储图像。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();
}

最新更新