FaceRecognition with OpenCv, NDK and Android studio failed! [closed]
I was trying to run this code to take a photo and recognize tho photo owner, at first the user clicks the button Train and takes a picture than enter its name, after that he clicks on Recognize button and take a picture if the picture was saved the apps recognize its face if not it shows unknown person for example. The app runs good but when it crashes sometimes with this error:
CvException [org.opencv.core.CvException: cv::Exception: OpenCV(4.0.0-pre) E:\AssemCourses\opencv-master\modules\core\src\matrix.cpp:235: error: (-215:Assertion failed) s >= 0 in function 'setSize'
The error in logcat refers to the method trainfaces :
Class TrainActivity.java:
private boolean trainfaces() {
if(images.isEmpty())
return false;
List<Mat> imagesMatrix = new ArrayList<>();
for (int i = 0; i < images.size(); i++)
imagesMatrix.add(images.get(i));
Set<String> uniqueLabelsSet = new HashSet<>(imagesLabels); // Get all unique labels
uniqueLabels = uniqueLabelsSet.toArray(new String[uniqueLabelsSet.size()]); // Convert to String array, so we can read the values from the indices
int[] classesNumbers = new int[uniqueLabels.length];
for (int i = 0; i < classesNumbers.length; i++)
classesNumbers[i] = i + 1; // Create incrementing list for each unique label starting at 1
int[] classes = new int[imagesLabels.size()];
for (int i = 0; i < imagesLabels.size(); i++) {
String label = imagesLabels.get(i);
for (int j = 0; j < uniqueLabels.length; j++) {
if (label.equals(uniqueLabels[j])) {
classes[i] = classesNumbers[j]; // Insert corresponding number
break;
}
}
}
Mat vectorClasses = new Mat(classes.length, 1, CvType.CV_32SC1); // CV_32S == int
vectorClasses.put(0, 0, classes); // Copy int array into a vector
recognize = LBPHFaceRecognizer.create(3,8,8,8,200);
recognize.train(imagesMatrix, vectorClasses);
if(SaveImage())
return true;
return false;
}
The Lines of Errors mentionned in the logcat refers to those java code lines :
In TrainActivity.java:
recognize = LBPHFaceRecognizer.create(3,8,8,8,200);
recognize.train(imagesMatrix, vectorClasses);
And
if(trainfaces()) {
images.clear();
imagesLabels.clear();
}
Do you guys have any idea about this problem and how to fix it?
PS: I am a beginner with OpenCv
do you really expect us to remote debug 427 lines of code for you ?
I worte the lines of errors at the end of question but i will edit to just method causing the bug. Sorry bro.
I edited my question can you plz put a look now.
please look up, what "minimal, reproducable piece of code" means here.
if i know where exactly the problem is i wont put all that code! I am a beginner with OpenCv and i thought putting more informations would help as they recommand in other forums. it's my first time posting here so i didn't know your recommendations. sorry for wasting your time. I edited "again" my question, if you still think there is a lot of code i will edit it again no problem. I m here to learn from people better than me.
try to check the images list for empty() items.
Ok I will do it
you're also resizing your images somewhere, another point to look at.
hello, can you lid opencv with contain recognize = LBPHFaceRecognizer.create(3,8,8,8,200); because I didn't find it thanks Email: [email protected]
helo @boonboon , please do not post answers here, if you have a question or a comment, thank you.
(and noone will mail you anything anytime from here, please have a look at the faq and possibly ask your own question)