YOLO ...程序没​​有经过循环来分析每一帧

问题描述

最近,我正在尝试实现基于YOLO的图像对象检测工具。首先,我使用了代码here。听起来一切正常,除了该程序未通过以下代码行(第72行)并且不会进入循环。 :

                if (cap.read(frame)) 

换句话说,如果在该行上放置了一个断点,程序将不会继续进行下一步。任何想法如何解决此问题?

package yoloexample;

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.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

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

public class Yoloexample {

    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)));//unfold and create R-CNN layers from the loaded YOLO model//
        System.out.println(names);
        return names;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // Todo code application logic here

        System.load("\\opencv\\opencv\\build\\java\\x64\\opencv_java420.dll"); // Load the openCV 4.0 dll //
        String modelWeights = "g:\\yolov3.weights"; //Download and load only wights for YOLO,this is obtained from official YOLO site//
        String modelConfiguration = "g:\\yolov3.cfg";//Download and load cfg file for YOLO,can be obtained from official site//
        String filePath = "test.mp4"; //My video  file to be analysed//
        VideoCapture cap = new VideoCapture(filePath);// Load video using the videocapture method//
        Mat frame = new Mat(); // define a matrix to extract and store pixel info from video//

        //cap.read(frame);
        JFrame jframe = new JFrame("Video"); // the lines below create a frame to display the resultant video with object detection and localization//
        JLabel vidpanel = new JLabel();
        jframe.setContentPane(vidpanel);
        jframe.setSize(600,600);
        jframe.setVisible(true);// we instantiate the frame here//

        Net net = Dnn.readNetFromDarknet(modelConfiguration,modelWeights); //OpenCV DNN supports models trained from varIoUs frameworks like Caffe and TensorFlow. It also supports varIoUs networks architectures based on YOLO//
        //Thread.sleep(5000);

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

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

        while (true) {

            if (cap.read(frame)) {

                Mat blob = Dnn.blobFromImage(frame,0.00392,sz,new Scalar(0),true,false); // We Feed one frame of video into the network at a time,we have to convert the image to a blob. A blob is a pre-processed image that serves as the input.//
                net.setInput(blob);

                net.forward(result,outBlobNames); //Feed forward the model to get output //

           // outBlobNames.forEach(System.out::println);
                // result.forEach(System.out::println);
                float confThreshold = 0.6f; //Insert thresholding beyond which the model will detect objects//
                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.maxLoc;
                        if (confidence > confThreshold) {
                            int centerX = (int) (row.get(0,0)[0] * frame.cols()); //scaling for drawing the bounding Boxes//
                            int centerY = (int) (row.get(0,1)[0] * frame.rows());
                            int width = (int) (row.get(0,2)[0] * frame.cols());
                            int height = (int) (row.get(0,3)[0] * frame.rows());
                            int left = centerX - width / 2;
                            int top = centerY - height / 2;

                            clsIds.add((int) classIdPoint.x);
                            confs.add((float) confidence);
                            rects.add(new Rect(left,top,height));
                        }
                    }
                }
                float nmsThresh = 0.5f;
                MatOfFloat confidences = new MatOfFloat(Converters.vector_float_to_Mat(confs));
                Rect[] BoxesArray = rects.toArray(new Rect[0]);
                MatOfRect Boxes = new MatOfRect(BoxesArray);
                MatOfInt indices = new MatOfInt();
                Dnn.NMSBoxes(Boxes,confidences,confThreshold,nmsThresh,indices); //We draw the bounding Boxes for objects here//

                int[] ind = indices.toArray();
                int j = 0;
                for (int i = 0; i < ind.length; ++i) {
                    int idx = ind[i];
                    Rect Box = BoxesArray[idx];
                    Imgproc.rectangle(frame,Box.tl(),Box.br(),new Scalar(0,255),2);
                //i=j;

                    System.out.println(idx);
                }
           // Imgcodecs.imwrite("D://out.png",image);
                //System.out.println("Image Loaded");
                ImageIcon image = new ImageIcon(Mat2bufferedImage(frame)); //setting the results into a frame and initializing it //
                vidpanel.setIcon(image);
                vidpanel.repaint();
                System.out.println(j);
                System.out.println("Done");

            }
        }
    }

    private static BufferedImage Mat2bufferedImage(Mat image) {   // The class described here  takes in matrix and renders the video to the frame  //
        MatOfByte bytemat = new MatOfByte();
        Imgcodecs.imencode(".jpg",image,bytemat);
        byte[] bytes = bytemat.toArray();
        InputStream in = new ByteArrayInputStream(bytes);
        BufferedImage img = null;
        try {
            img = ImageIO.read(in);
        } catch (IOException e) {
            // Todo Auto-generated catch block
            e.printstacktrace();
        }
        return img;
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)