Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Darknet-Yolo - extract data from Dnn.forward

Hi I`m trying to write image classifier using yolov3. All i found is examples in Python/Cpp. So my question is - how can i extract labels, bounding boxes from Dnn.result ?

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.dnn.Dnn;
import org.opencv.dnn.Net;
import org.opencv.imgcodecs.Imgcodecs;

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

public class Classifier  {

    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) {

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        String modelWeights = "yolov3.weights";
        String modelConfiguration = "yolov3.cfg";

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

        Imgcodecs imageCodecs = new Imgcodecs();
        Mat image = imageCodecs.imread("video/sample.png");

        Mat blob = Dnn.blobFromImage(image, 1.0, new Size(416, 416), new Scalar(0), false, false);
        net.setInput(blob);

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

        net.forward(result, outBlobNames);

        outBlobNames.forEach(System.out::println);
        result.forEach(System.out::println);
    }
}

This produces

yolo_82
yolo_94
yolo_106
Mat [ 507*85*CV_32FC1, isCont=true, isSubmat=true, nativeObj=0x7f991f39a480, dataAddr=0x7f9892adf040 ]
Mat [ 2028*85*CV_32FC1, isCont=true, isSubmat=true, nativeObj=0x7f991f398b80, dataAddr=0x7f991eb20280 ]
Mat [ 8112*85*CV_32FC1, isCont=true, isSubmat=true, nativeObj=0x7f991f236880, dataAddr=0x7f98a01af040 ]
enter code here
enter code here
enter code here

How should i proccess this data ?

Darknet-Yolo - extract data from Dnn.forward

Hi I`m trying to write image classifier using yolov3. All i found is examples in Python/Cpp. So my question is - how can i extract labels, bounding boxes from Dnn.result Dnn.forward ?

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.dnn.Dnn;
import org.opencv.dnn.Net;
import org.opencv.imgcodecs.Imgcodecs;

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

public class Classifier  {

    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) {

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        String modelWeights = "yolov3.weights";
        String modelConfiguration = "yolov3.cfg";

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

        Imgcodecs imageCodecs = new Imgcodecs();
        Mat image = imageCodecs.imread("video/sample.png");

        Mat blob = Dnn.blobFromImage(image, 1.0, new Size(416, 416), new Scalar(0), false, false);
        net.setInput(blob);

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

        net.forward(result, outBlobNames);

        outBlobNames.forEach(System.out::println);
        result.forEach(System.out::println);
    }
}

This produces

yolo_82
yolo_94
yolo_106
Mat [ 507*85*CV_32FC1, isCont=true, isSubmat=true, nativeObj=0x7f991f39a480, dataAddr=0x7f9892adf040 ]
Mat [ 2028*85*CV_32FC1, isCont=true, isSubmat=true, nativeObj=0x7f991f398b80, dataAddr=0x7f991eb20280 ]
Mat [ 8112*85*CV_32FC1, isCont=true, isSubmat=true, nativeObj=0x7f991f236880, dataAddr=0x7f98a01af040 ]
enter code here
enter code here
enter code here

How should i proccess this data ?