Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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:236: error: (-215:Assertion failed) ngroups > 0 && inpCn % ngroups == 0 && outCn % ngroups == 0 in function 'virtual bool cv::dnn::ConvolutionLayerImpl::getMemoryShapes(const std::vector<std::vector<int> >&, int, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&) const']

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(mGray, face);
                Mat inputBlob = Dnn.blobFromImage(capturedFace, 1.0f, new Size(256, 256), new Scalar(0), false, false);
                net.setInput(inputBlob, "data");
                Mat detections = net.forward("prob");

                int cols = capturedFace.cols();
                int rows = capturedFace.rows();

                detections = detections.reshape(1, (int) detections.total() / 7);

                for (int i = 0; i < detections.rows(); i++) {
                    double confidence = detections.get(i, 2)[0];
                    if (confidence > THRESHOLD) {
                        int classId = (int) detections.get(i, 1)[0];

                        int xLeftBottom = (int) (detections.get(i, 3)[0] * cols);
                        int yLeftBottom = (int) (detections.get(i, 4)[0] * rows);
                        int xRightTop = (int) (detections.get(i, 5)[0] * cols);
                        int yRightTop = (int) (detections.get(i, 6)[0] * rows);

                        // Draw rectangle around detected object.
                        Imgproc.rectangle(capturedFace, new Point(xLeftBottom, yLeftBottom),
                                new Point(xRightTop, yRightTop),
                                new Scalar(0, 255, 0));
                        String label = classId + ": " + confidence;
                        // Write class name and confidence.
                        Imgproc.putText(capturedFace, label, new Point(xLeftBottom, yLeftBottom),
                                Core.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(0, 0, 0));
                    }
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "Error", e);
        }
    }
    return mRgba;
}

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

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:236: /build/3_4_pack-android/opencv/modules/dnn/src/layers/convolution_layer.cpp:987: error: (-215:Assertion failed) ngroups > 0 && inpCn inputs[0]->size[1] % ngroups == 0 && outCn % ngroups blobs[0].size[1] == 0 in function 'virtual bool cv::dnn::ConvolutionLayerImpl::getMemoryShapes(const std::vector<std::vector<int> >&, int, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&) const']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(mGray, Mat(mRgba, face);
                Mat inputBlob = Dnn.blobFromImage(capturedFace, 1.0f, new Size(256, 256), Size(227, 227), new Scalar(0), false, false);
                net.setInput(inputBlob, "data");
                Mat detections = net.forward("prob");

                int cols = capturedFace.cols();
                int rows = capturedFace.rows();

                detections = detections.reshape(1, (int) detections.total() / 7);

                for (int i = 0; i < detections.rows(); i++) {
    probs = net.forward("prob").reshape(1, 1); // flatten to a single row
                Core.MinMaxLocResult mm = Core.minMaxLoc(probs); // get largest softmax output

                double confidence = detections.get(i, 2)[0];
                    if (confidence > THRESHOLD) {
                        int classId = (int) detections.get(i, 1)[0];

                        int xLeftBottom = (int) (detections.get(i, 3)[0] * cols);
                        int yLeftBottom = (int) (detections.get(i, 4)[0] * rows);
                        int xRightTop = (int) (detections.get(i, 5)[0] * cols);
                        int yRightTop = (int) (detections.get(i, 6)[0] * rows);

                        // Draw rectangle around detected object.
                        Imgproc.rectangle(capturedFace, new Point(xLeftBottom, yLeftBottom),
                                new Point(xRightTop, yRightTop),
                                new Scalar(0, 255, 0));
                        String label = classId + ": result = mm.maxLoc.x; //gender or age group
                Log.i(TAG, "Result is: " + confidence;
                        // Write class name and confidence.
                        Imgproc.putText(capturedFace, label, new Point(xLeftBottom, yLeftBottom),
                                Core.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(0, 0, 0));
                    }
                }
result);
            }
        } catch (Exception e) {
            Log.e(TAG, "Error", e);
        }
    }
    return mRgba;
}

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

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;
}