error: (-215: Assertion failed) inputs.size() == requiredOutputs in function 'getMemoryShapes'

asked 2020-01-05 18:36:07 -0600

aditya_mangalampalli gravatar image

Hello all, I was trying to run an OpenCV program to run a DNN on an Android device using the OpenCv DNN module with Tensorflow and an SSD. However, I keep receiving this error when I try to run the program. CvException - cv::Exception: OpenCV(4.1.0) /build/master_pack-android/opencv/modules/dnn/src/dnn.cpp:692: error: (-2.15:Assertion failed) inputs.size() == requiredOutputs in function 'getMemoryShapes' I do not know how to fix this error because as far as I know, my code to access the protobuf and protobuf text file seems correct. My code to access the variables and read from them seem correct and the other OpenCV loaders are in other classes which seem to work fine. My code to access the pb and the pbtxt file are listed below:

public Mat processFrame(Mat inputFrame) {
    final int IN_WIDTH = 300;
    final int IN_HEIGHT = 300;
    final double IN_SCALE_FACTOR = 1;
    final double MEAN_VAL = 0;
    final double THRESHOLD = 0.85;

    // Get a new frame
    Imgproc.cvtColor(inputFrame, inputFrame, Imgproc.COLOR_RGBA2RGB);

    // Forward image through network.
    Mat blob = Dnn.blobFromImage(inputFrame, IN_SCALE_FACTOR, new Size(IN_WIDTH, IN_HEIGHT), new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), true, false);
    net.setInput(blob);

    List<List<Double>> blobList = new ArrayList<>();

    Mat detections = net.forward();

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


    detections = detections.reshape(1, (int) detections.total() / 7);
    for (int i = 0; i < detections.rows(); ++i) {
        System.out.println(detections);
        double confidence = detections.get(i, 2)[0];

        if (confidence > THRESHOLD) {
            int classId = (int) detections.get(i, 1)[0];
            int left = (int) (detections.get(i, 3)[0] * cols);
            int top = (int) (detections.get(i, 4)[0] * rows);
            int right = (int) (detections.get(i, 5)[0] * cols);
            int bottom = (int) (detections.get(i, 6)[0] * rows);

            List<Double> list = new ArrayList<>();
            list.add(confidence);
            list.add(Double.valueOf(left));
            list.add(Double.valueOf(top));
            list.add(Double.valueOf(right));
            list.add(Double.valueOf(bottom));
            list.add(Double.valueOf(classId));

            blobList.add(list);
        }
    }

    Collections.sort(blobList, new Comparator<List<Double>>() {
        @Override
        public int compare(List<Double> a, List<Double> b) {
            return a.get(0) > b.get(0) ? 1 : -1;
        }
    });

    Collections.reverse(blobList);

    int maxIndex = blobList.size() > 6 ? 6 : blobList.size();
    int numOfSkystone = 0;

    for (int i = 0; i < 6; i++) {
        List<Double> blobStuff = blobList.get(i);
        String detectedObj = "";


        double v = blobStuff.get(5).doubleValue();
        if (v == 3.0) {
            detectedObj = "Skystone";
            numOfSkystone++;
        } else if (v == 4.0) {
            detectedObj = "Stone";
        } else if (v == 2.0) {
            detectedObj = "Red Foundation";
        } else if (v == 1.0) {
            detectedObj = "Blue Foundation";
        } else {
            detectedObj = "Unknown";
        }

        String label = detectedObj + ": " + blobStuff.get(0);
        int[] baseLine = new int[1];
        Size labelSize = Imgproc.getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, baseLine);
        Imgproc.rectangle(inputFrame, new Point((int) blobStuff.get(1).intValue(), (int) blobStuff.get(2).intValue() - labelSize.height),
                new Point((int) blobStuff.get(1).intValue() + labelSize.width, (int) blobStuff.get(2).intValue() + baseLine[0]),
                new Scalar(255, 255, 255), Imgproc.FILLED);
        // Write class name and confidence.
        Imgproc.putText(inputFrame, label, new Point(blobStuff.get(0), blobStuff.get(2)),
                FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(0, 0, 0));
    }
    return ...
(more)
edit retag flag offensive close merge delete