Ask Your Question

Arryyyy's profile - activity

2018-12-20 09:31:24 -0500 received badge  Student (source)
2018-12-20 07:42:20 -0500 received badge  Supporter (source)
2018-12-20 07:42:17 -0500 marked best answer 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.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 ]

How should i proccess this data ?

2018-12-20 07:42:17 -0500 received badge  Scholar (source)
2018-12-20 07:42:09 -0500 commented answer Darknet-Yolo - extract data from Dnn.forward

Thanks :D This works great! 1 more question - how can i get classes names, like 'dog', 'bike' etc ?

2018-12-20 05:40:11 -0500 received badge  Editor (source)
2018-12-20 05:40:11 -0500 edited question Darknet-Yolo - extract data from Dnn.forward

Darknet-Yolo - extract data from Dnn.forward Hi I`m trying to write image classifier using yolov3. All i found is exampl

2018-12-20 05:02:58 -0500 asked a question Darknet-Yolo - extract data from Dnn.forward

Darknet-Yolo - extract data from Dnn.forward Hi I`m trying to write image classifier using yolov3. All i found is exampl