如何在Java中用OpenCV DNN运行YOLOv8?置信度获取异常求助
Java中OpenCV DNN运行YOLOv8目标检测的置信度获取问题
我需要在Java中使用OpenCV的DNN模块运行YOLOv8进行目标检测,官方仅提供了C和Python版本的示例,我尝试将C版本转换为Java代码,但目前无法正确获取置信度,希望得到相关帮助。
我的Java代码如下:
Mat img = Imgcodecs.imread(imgPath); Mat blob = Dnn.blobFromImage(img, 1/255, new Size(640,640), mean, swapRB, crop); //Sets the input to the network net.setInput(blob); //Runs the forward pass to get output of the output layers List<Mat> outputs = new ArrayList<>(); net.forward(outputs); int rows = (int) outputs.get(0).size().height; int dimensions = (int) outputs.get(0).size().width; if (dimensions > rows) { rows = (int) outputs.get(0).size().width; dimensions = (int) outputs.get(0).size().height; outputs.set(0,outputs.get(0).reshape(1, dimensions)); Core.transpose(outputs.get(0), outputs.get(0)); } float[] data = new float[rows * dimensions]; outputs.get(0).get(0, 0, data); float x_factor = img.cols() / 640; float y_factor = img.rows() / 640; List<Float> confs = new ArrayList<>(); List<Rect2d> boxes = new ArrayList<>(); for (int i = 0; i < data.length/4; ++i) { Mat scores = outputs.get(0).row(i); Core.MinMaxLocResult mm = Core.minMaxLoc(scores); float confidence = (float) (mm.maxVal/100); confs.add(confidence); float x = data[0+i*4]; float y = data[1+i*4]; float w = data[2+i*4]; float h = data[3+i*4]; int left = (int) ((x - 0.5 * w) * x_factor); int top = (int) ((y - 0.5 * h) * y_factor); int width = (int) (w * x_factor); int height = (int) (h * y_factor); Rect2d box = new Rect2d(left, top, width, height); boxes.add(box); } Mat converted = Converters.vector_float_to_Mat(confs); MatOfFloat confidences = new MatOfFloat(converted); Rect2d[] boxesArray = boxes.toArray(new Rect2d[0]); MatOfRect2d matBoxes = new MatOfRect2d(boxesArray); //boxes.fromArray(boxesArray); MatOfInt indices = new MatOfInt(); Dnn.NMSBoxes(matBoxes, confidences, confThreshold, nmsThresh, indices); int [] ind = indices.toArray(); List<Rect2d> boxesAfterNMS = new ArrayList<>(); for (int i = 0; i < ind.length; ++i) { int idx = ind[i]; Rect2d box = boxesArray[idx]; int width = (int)box.width; int height = (int)box.height; int left = (int)box.x; int top = (int)box.y; boxesAfterNMS.add(new Rect2d(left,top,width,height)); }
内容的提问来源于stack exchange,提问作者Lihang Xu
相关产品推荐
相关产品推荐

