Ask Your Question

(-215:Assertion failed) m.dims >= 2 in function 'cv::Mat::Mat'

asked 2019-03-18 06:26:31 -0500


I am trying to load a tin - yolo v3 model into java and do someobject recognition on video streams the following code i wrote with help of this post seems to open files and start image recognition as expected at the very beggining however i get this error after some frames have been analysed

Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: OpenCV(4.0.0-alpha) C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\core\src\matrix.cpp:405: error: (-215:Assertion failed) m.dims >= 2 in function 'cv::Mat::Mat' ] at org.opencv.core.Mat.n_Mat(Native Method) at org.opencv.core.Mat.<init>( at org.opencv.core.MatOfFloat.<init>( at sample.yolo.main(

What might be happening here ? can anyone please help me out?

Here is the code i am trying to run :

package sample;

import org.opencv.core.*;
import org.opencv.dnn.*;
import org.opencv.utils.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.videoio.VideoCapture;

import java.util.ArrayList;
import java.util.List;

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class yolo {

    private static List<String> getOutputNames(Net net) {
        List<String> names = new ArrayList<>();

        List<Integer> outLayers = net.getUnconnectedOutLayers().toList();
        List<String> layersNames = net.getLayerNames();

        outLayers.forEach((item) -> names.add(layersNames.get(item - 1)));
        return names;
    public static void main(String[] args) throws InterruptedException {
         String modelWeights = "D:\\yolov3-tiny.weights";
            String modelConfiguration = "D:\\yolov3-tiny.cfg.txt";
            String filePath = "D:\\test.avi";
            VideoCapture cap = new VideoCapture(filePath);
             Mat frame = new Mat();

             JFrame jframe = new JFrame("Video");
             JLabel vidpanel = new JLabel();
             jframe.setSize(600, 600);

            Net net = Dnn.readNetFromDarknet(modelConfiguration, modelWeights);

            //Mat image = Imgcodecs.imread("D:\\yolo-object-detection\\yolo-object-detection\\images\\soccer.jpg");
            Size sz = new Size(416, 416);

            while (true) {

                if ( {

            Mat blob = Dnn.blobFromImage(frame, 0.00392, sz, new Scalar(0), true, false);

            List<Mat> result = new ArrayList<>();
            List<String> outBlobNames = getOutputNames(net);

            net.forward(result, outBlobNames);


            float confThreshold = 0.6f;
            List<Integer> clsIds = new ArrayList<>();
            List<Float> confs = new ArrayList<>();
            List<Rect> rects = new ArrayList<>();
            for (int i = 0; i < result.size(); ++i)
                // each row is a candidate detection, the 1st 4 numbers are
                // [center_x, center_y, width, height], followed by (N-4) class probabilities
                Mat level = result.get(i);
                for (int j = 0; j < level.rows(); ++j)
                    Mat row = level.row(j);
                    Mat scores = row.colRange(5, level.cols());
                    Core.MinMaxLocResult mm = Core.minMaxLoc(scores);
                    float confidence = (float)mm.maxVal;
                    Point classIdPoint = mm ...
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2020-01-24 08:37:12 -0500

If you is use MatOfFloat to get confidence on search, check if var 'confs' is not empty before to continue process.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-03-18 06:26:31 -0500

Seen: 1,827 times

Last updated: Jan 24 '20