Search in sources :

Example 1 with Blob

use of org.bytedeco.javacpp.opencv_dnn.Blob in project javacv by bytedeco.

the class CaffeGooglenet method main.

public static void main(String[] args) throws Exception {
    String modelTxt = "bvlc_googlenet.prototxt";
    String modelBin = "bvlc_googlenet.caffemodel";
    String imageFile = (args.length > 0) ? args[0] : "space_shuttle.jpg";
    // ! [Create the importer of Caffe model]
    Importer importer = null;
    try {
        // Try to import Caffe GoogleNet model
        importer = createCaffeImporter(modelTxt, modelBin);
    } catch (Exception e) {
        // Importer can throw errors, we will catch them
        e.printStackTrace();
    }
    if (importer == null) {
        System.err.println("Can't load network by using the following files: ");
        System.err.println("prototxt:   " + modelTxt);
        System.err.println("caffemodel: " + modelBin);
        System.err.println("bvlc_googlenet.caffemodel can be downloaded here:");
        System.err.println("http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel");
        System.exit(-1);
    }
    // ! [Initialize network]
    Net net = new Net();
    importer.populateNet(net);
    // We don't need importer anymore
    importer.close();
    // ! [Initialize network]
    // ! [Prepare blob]
    Mat img = imread(imageFile);
    if (img.empty()) {
        System.err.println("Can't read image from the file: " + imageFile);
        System.exit(-1);
    }
    // GoogLeNet accepts only 224x224 RGB-images
    resize(img, img, new Size(224, 224));
    // Convert Mat to 4-dimensional dnn::Blob from image
    Blob inputBlob = Blob.fromImages(img);
    // ! [Prepare blob]
    // ! [Set input blob]
    // set the network input
    net.setBlob(".data", inputBlob);
    // ! [Set input blob]
    // ! [Make forward pass]
    // compute output
    net.forward();
    // ! [Make forward pass]
    // ! [Gather output]
    // gather output of "prob" layer
    Blob prob = net.getBlob("prob");
    Point classId = new Point();
    double[] classProb = new double[1];
    // find the best class
    getMaxClass(prob, classId, classProb);
    // ! [Gather output]
    // ! [Print results]
    List<String> classNames = readClassNames();
    System.out.println("Best class: #" + classId.x() + " '" + classNames.get(classId.x()) + "'");
    System.out.println("Best class: #" + classId.x());
    System.out.println("Probability: " + classProb[0] * 100 + "%");
// ! [Print results]
}
Also used : Mat(org.bytedeco.javacpp.opencv_core.Mat) Blob(org.bytedeco.javacpp.opencv_dnn.Blob) Size(org.bytedeco.javacpp.opencv_core.Size) Point(org.bytedeco.javacpp.opencv_core.Point) Net(org.bytedeco.javacpp.opencv_dnn.Net) IOException(java.io.IOException) org.bytedeco.javacpp.opencv_dnn.createCaffeImporter(org.bytedeco.javacpp.opencv_dnn.createCaffeImporter) Importer(org.bytedeco.javacpp.opencv_dnn.Importer)

Aggregations

IOException (java.io.IOException)1 Mat (org.bytedeco.javacpp.opencv_core.Mat)1 Point (org.bytedeco.javacpp.opencv_core.Point)1 Size (org.bytedeco.javacpp.opencv_core.Size)1 Blob (org.bytedeco.javacpp.opencv_dnn.Blob)1 Importer (org.bytedeco.javacpp.opencv_dnn.Importer)1 Net (org.bytedeco.javacpp.opencv_dnn.Net)1 org.bytedeco.javacpp.opencv_dnn.createCaffeImporter (org.bytedeco.javacpp.opencv_dnn.createCaffeImporter)1