Get the model file for Yolo4

yolov4.cfg
yolov4.weights
coco.names
Copy the code

The sample

public static void testYOLO(Mat image) throws IOException { String config = "E: / opencv4.4.0 / age - and - gender - classification/yolo/yolov4 CFG"; String weights = "E: / opencv4.4.0 / age - and - gender - classification/yolo/yolov4 weights". String classesFile = "E: / opencv4.4.0 / age - and - gender - classification/yolo/coco. Names". List<String> classes = new ArrayList<String>(); InputStream in = new FileInputStream(classesFile); int iAvail = in.available(); // Returns the number of bytes when multiple bytes are read locally. byte[] bytes = new byte[iAvail]; in.read(bytes); String allContent = new String(bytes); String[] tempContent = allContent.trim().split("\n"); String[] tempContent = allContent.trim().split("\n"); // allContent is separated by a newline character. // Iterate over tempContent and add it to the list classes that holds the class name. for (int i = 0; i < tempContent.length; i++) { classes.add(tempContent[i]); } System.out.println(classes.size()); Net net = Dnn.readNetFromDarknet(config, weights); net.setPreferableBackend(Dnn.DNN_BACKEND_OPENCV); // Don't understand command line argument "-cl-no-subgroup-ifp" // net.setPreferableTarget(Dnn.DNN_TARGET_OPENCL); // change to net.setpreferableTarget (dnn.dnn_target_CPU); Mat im = image.clone(); If (im.empty()) {system.out.println (" image failed to load "); } Size sz = new Size(416, 416); List<Mat> outs = new ArrayList<>(); Mat blob = dnN. blobFromImage(Image, 0.00392, SZ, New Scalar(0), true, false); Mat blob = dnN. blobFromImage(image, 0.00392, Sz, New Scalar(0), true, false); net.setInput(blob); net.forward(outs, getOutputNames(net)); Float confThreshold = 0.8 f; List<Rect2d> boxes = new ArrayList<Rect2d>(); List<Integer> classIds = new ArrayList<Integer>(); List<Float> confidences = new ArrayList<Float>(); For (int I = 0; i < outs.size(); ++i) { Mat level = outs.get(i); for (int j = 0; j < level.rows(); ++j) { Mat row = level.row(j); Class1,class2] [x,y,h,w,c,class1,class2] 0 Mat scores = row.colrange (5, level.cols()); Core.MinMaxLocResult mm = Core.minMaxLoc(scores); float confidence = (float) mm.maxVal; Point classIdPoint = mm.maxLoc; int size = (int) (level.cols() * level.channels()); float[] data = new float[size]; level.get(j, 0, data); if (confidence > confThreshold) { float x = data[0]; Float y = data[1]; float y = data[1]; Float width = data[2]; // Float width = data[2]; Float height = data[3]; float height = data[3]; // High float xLeftBottom = (x-width / 2) * im.cols(); Float yLeftBottom = (y-height / 2) * im.rows(); float yLeftBottom = (y-height / 2) * im.rows(); Float xRightTop = (x + width / 2) * im.cols(); float xRightTop = (x + width / 2) * im.cols(); Float yRightTop = (y + height / 2) * im.rows(); float yRightTop = (y + height / 2) * im.rows(); // Box list: Rect object, Add (new Rect2d(new Point(xLeftBottom, yLeftBottom), new Point(xRightTop, yRightTop))); confidences.add(confidence); classIds.add((int) classIdPoint.x); // Imgproc.rectangle(image, new Point(xLeftBottom, yLeftBottom), // new Point(xRightTop, yRightTop), new Scalar(255, 0, 0)); // Imgproc.putText(image, classes.get((int)classIdPoint.x)+" "+confidence, new Point(xLeftBottom, YLeftBottom),Imgproc.FONT_HERSHEY_PLAIN,0.8, new Scalar(255, 0, 0)); } } } System.out.println(classIds); System.out.println(confidences); System.out.println(boxes.size()); System.out.println(boxes); Float nmsThresh = 0.5 f; MatOfFloat confidences2 = new MatOfFloat(Converters.vector_float_to_Mat(confidences)); // Rect2d[] boxesArray = boxes.toArray(new Rect2d[0]); // MatOfRect boxes2 = new MatOfRect(boxesArray); MatOfRect2d m = new MatOfRect2d(); m.fromList(boxes); MatOfInt indices = new MatOfInt(); /** * NMS(Non Maximum Suppression), Rect2d */ DNN. NMSBoxes(M, confidences2, confThreshold, nmsThresh, indices) // We draw the bounding boxes for objects // here// int[] ind = indices.toArray(); for (int i = 0; i < ind.length; ++i) { int index = ind[i]; Rect2d box = boxes.get(index); int idGuy = classIds.get(index); float conf = confidences.get(index); Imgproc.rectangle(image, box.tl(), box.br(), new Scalar(255, 0, 0)); Imgproc.putText(image, classes.get(idGuy) + "" + conf, box.tl(), imgproc. FONT_HERSHEY_PLAIN, 0.8, new Scalar(255, 0, 0)); } HighGui.imshow("yolo", image); }Copy the code

The effect

Have a problem

-cl-no-subgroup-ifp: net.setpreferableTarget (dnn.dnn_target_CPU);Copy the code
Too many matching models Non Maximum Suppression (NMS) Deletes highly redundant Rect2dCopy the code
The NMS needs Rect2d to be changed from Rect to Rect2dCopy the code