use of org.bytedeco.javacpp.opencv_dnn.Net 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]
}
Aggregations