FaceRecognition with OpenCv, NDK and Android studio failed! [closed]

asked 2018-11-17 09:30:41 -0600

Proappdev gravatar image

updated 2018-11-17 13:14:10 -0600

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

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-12-09 03:50:41.672113

Comments

do you really expect us to remote debug 427 lines of code for you ?

berak gravatar imageberak ( 2018-11-17 09:42:45 -0600 )edit

I worte the lines of errors at the end of question but i will edit to just method causing the bug. Sorry bro.

Proappdev gravatar imageProappdev ( 2018-11-17 12:34:58 -0600 )edit
1

I edited my question can you plz put a look now.

Proappdev gravatar imageProappdev ( 2018-11-17 12:41:25 -0600 )edit

please look up, what "minimal, reproducable piece of code" means here.

berak gravatar imageberak ( 2018-11-17 12:53:49 -0600 )edit

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.

Proappdev gravatar imageProappdev ( 2018-11-17 13:16:39 -0600 )edit

try to check the images list for empty() items.

berak gravatar imageberak ( 2018-11-18 04:56:15 -0600 )edit

Ok I will do it

Proappdev gravatar imageProappdev ( 2018-11-19 04:08:12 -0600 )edit

you're also resizing your images somewhere, another point to look at.

berak gravatar imageberak ( 2018-11-19 04:14:49 -0600 )edit

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]

boonboon gravatar imageboonboon ( 2019-07-12 01:27:13 -0600 )edit

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)

berak gravatar imageberak ( 2019-07-12 01:32:47 -0600 )edit