Ask Your Question
0

[HELP] CvException @ Dnn.forward() in Android [closed]

asked 2018-08-11 15:57:29 -0500

AlenSali gravatar image

updated 2018-08-13 04:35:00 -0500

I'm trying to implement Gil Levi and Tal Hassner.Age and Gender Classification Using Convolutional Neural Networks to Android app but I'm getting an error on Dnn.forward(). I followed this link:tutorial and I get following error:

CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.2) /build/3_4_pack-android/opencv/modules/dnn/src/layers/convolution_layer.cpp:987: error: (-215:Assertion failed) inputs[0]->size[1] % blobs[0].size[1] == 0 in function 'virtual void cv::dnn::ConvolutionLayerImpl::forward(std::vector<cv::mat*>&, std::vector<cv::mat>&, std::vector<cv::mat>&)' ]

    at org.opencv.dnn.Net.forward_0(Native Method)
    at org.opencv.dnn.Net.forward(Net.java:52)
    at com.alensalihbasic.recfaceocv.MainActivity.onCameraFrame(MainActivity.java:226)
    at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:392)
    at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:373)
    at java.lang.Thread.run(Thread.java:764)

If could somebody help me, I'll be grateful :)

Code snippet:

@Override public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();

    if (mAbsoluteFaceSize == 0) {
        int height = mGray.rows();
        if (Math.round(height * mRelativeFaceSize) > 0) {
            mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
        }
    }

    MatOfRect faces = new MatOfRect();

    // Use the classifier to detect faces
    if (mFaceDetector != null) {
        mFaceDetector.detectMultiScale(mGray, faces, 1.1, 2, 2, new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
    }else {
        Log.e(TAG, "Detection is not selected!");
    }

    // If there are any faces found, draw a rectangle around it
    Rect[] facesArray = faces.toArray();
    for (int i = 0; i < facesArray.length; i++) {
        Imgproc.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 0, 255), 3);
    }

    if (facesArray.length == 1) {
        try {
            for (Rect face : facesArray) {
                Mat capturedFace = new Mat(mRgba, face);
                Imgproc.resize(capturedFace, capturedFace, new Size(227, 227));
                Mat inputBlob = Dnn.blobFromImage(capturedFace, 1.0f, new Size(227, 227), new Scalar(0), false, false);
                net.setInput(inputBlob, "data");
                Mat probs = net.forward("prob").reshape(1, 1); // flatten to a single row
                Core.MinMaxLocResult mm = Core.minMaxLoc(probs); // get largest softmax output

                double result = mm.maxLoc.x; //gender or age group
                Log.i(TAG, "Result is: " + result);
            }
        } catch (Exception e) {
            Log.e(TAG, "Error", e);
        }
    }
    return mRgba;
}
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by AlenSali
close date 2018-08-15 05:20:25.408584

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-08-11 23:27:50 -0500

berak gravatar image

updated 2018-08-12 03:15:13 -0500

the error is a bit cryptic, but you're feeding grayscale images into it, while it expects color (3-channel) images. also the images should be resized to 227x227 (if you look at the prototxt)

then, the code you're trying with was made for object detection , while your network is for classification.

you'll have to rewrite anything after the net.forward() call. i don't have the data, but hopefully it is as simple as:

// prototxt says, the last name is "prob", not "probs"
Mat probs = net.forward("prob").reshape(1,1); // flatten to a single row
MinMaxLocResult mm = Core.minMaxLoc(probs); // get largest softmax output

int result = mm.maxLoc.x; // gender or age group
edit flag offensive delete link more

Comments

Thank you for your answer. I updated my question with following error copied from Logcat after I tried your solution.

If you have some links with examples on Android or Java, please share them with me because I can't find anything online.

AlenSali gravatar imageAlenSali ( 2018-08-12 11:39:14 -0500 )edit

DID you change the size to 227x227, as required ?

berak gravatar imageberak ( 2018-08-12 21:59:35 -0500 )edit
1

Yes. I added Imgproc.resize(capturedFace, capturedFace, new Size(227, 227)); Same error :/ Maybe I'm calling something at wrong place?

Updated Code snippet.

AlenSali gravatar imageAlenSali ( 2018-08-13 04:39:18 -0500 )edit

inputs[0]->size[1] % blobs[0].size[1] == 0 <-- that's the channel count.

if your image has 4 channels (rgba), you have to convert it to BGR first.

the mean value for this seems to be: (78.4263377603, 87.7689143744, 114.895847746) (from here)

berak gravatar imageberak ( 2018-08-13 05:04:11 -0500 )edit
1

I converted to BGR and added mean value too. Now it works! I'm getting result. Thank you very much.

AlenSali gravatar imageAlenSali ( 2018-08-15 05:17:22 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-08-11 15:57:29 -0500

Seen: 70 times

Last updated: Aug 13