2018-02-19 09:23:35 -05:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertFalse;
|
2018-02-15 12:37:38 -05:00
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
|
2018-02-19 09:23:35 -05:00
|
|
|
import java.awt.List;
|
2018-02-15 07:46:02 -05:00
|
|
|
import java.io.File;
|
2018-02-19 09:23:35 -05:00
|
|
|
import java.util.ArrayList;
|
2018-02-15 12:37:38 -05:00
|
|
|
|
2018-02-15 07:46:02 -05:00
|
|
|
import org.opencv.core.Core;
|
|
|
|
import org.opencv.core.CvType;
|
|
|
|
import org.opencv.core.Mat;
|
2018-02-15 12:37:38 -05:00
|
|
|
import org.opencv.core.Scalar;
|
|
|
|
import org.opencv.core.Size;
|
2018-02-19 09:23:35 -05:00
|
|
|
import org.opencv.dnn.DictValue;
|
2018-02-15 07:46:02 -05:00
|
|
|
import org.opencv.dnn.Dnn;
|
2018-02-19 09:23:35 -05:00
|
|
|
import org.opencv.dnn.Layer;
|
2018-02-15 07:46:02 -05:00
|
|
|
import org.opencv.dnn.Net;
|
2018-02-15 12:37:38 -05:00
|
|
|
import org.opencv.imgcodecs.Imgcodecs;
|
|
|
|
import org.opencv.imgproc.Imgproc;
|
2018-02-15 07:46:02 -05:00
|
|
|
|
|
|
|
public class ObjectDetector {
|
|
|
|
|
|
|
|
String inputImagePath;
|
|
|
|
String inputModelPath;
|
2018-02-15 12:37:38 -05:00
|
|
|
String inputModelArgumentsPath;
|
|
|
|
|
|
|
|
Net net;
|
2018-02-15 07:46:02 -05:00
|
|
|
|
2018-02-19 09:23:35 -05:00
|
|
|
public ObjectDetector() throws Exception {
|
2018-02-15 12:37:38 -05:00
|
|
|
this.inputImagePath = "/home/dpapp/tensorflow-1.5.0/models/raccoon_dataset/test_images/ironOre_test_9.jpg";
|
2018-02-19 09:23:35 -05:00
|
|
|
this.inputModelPath = "/home/dpapp/tensorflow-1.5.0/models/raccoon_dataset/results/checkpoint_23826/frozen_graph_inference.pb";
|
|
|
|
this.inputModelArgumentsPath = "/home/dpapp/tensorflow-1.5.0/models/raccoon_dataset/generated_graph.pbtxt";
|
|
|
|
|
|
|
|
File f = new File(inputImagePath);
|
|
|
|
if(!f.exists()) throw new Exception("Test image is missing: " + inputImagePath);
|
|
|
|
File f1 = new File(inputModelPath);
|
|
|
|
if(!f1.exists()) throw new Exception("Test image is missing: " + inputModelPath);
|
|
|
|
File f2 = new File(inputModelArgumentsPath);
|
|
|
|
if(!f2.exists()) throw new Exception("Test image is missing: " + inputModelArgumentsPath);
|
|
|
|
|
2018-02-15 12:37:38 -05:00
|
|
|
net = Dnn.readNetFromTensorflow(inputModelPath, inputModelArgumentsPath);
|
2018-02-15 07:46:02 -05:00
|
|
|
}
|
|
|
|
|
2018-02-19 09:23:35 -05:00
|
|
|
public void testGetLayerTypes() {
|
|
|
|
ArrayList<String> layertypes = new ArrayList();
|
|
|
|
net.getLayerTypes(layertypes);
|
|
|
|
|
|
|
|
assertFalse("No layer types returned!", layertypes.isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetLayer() {
|
|
|
|
ArrayList<String> layernames = (ArrayList<String>) net.getLayerNames();
|
|
|
|
|
|
|
|
assertFalse("Test net returned no layers!", layernames.isEmpty());
|
|
|
|
|
|
|
|
String testLayerName = layernames.get(0);
|
|
|
|
|
|
|
|
DictValue layerId = new DictValue(testLayerName);
|
|
|
|
|
|
|
|
assertEquals("DictValue did not return the string, which was used in constructor!", testLayerName, layerId.getStringValue());
|
|
|
|
|
|
|
|
Layer layer = net.getLayer(layerId);
|
|
|
|
|
|
|
|
assertEquals("Layer name does not match the expected value!", testLayerName, layer.get_name());
|
|
|
|
}
|
|
|
|
|
2018-02-15 12:37:38 -05:00
|
|
|
public void testImage() throws Exception {
|
|
|
|
Mat rawImage = Imgcodecs.imread(inputImagePath);
|
2018-02-19 09:23:35 -05:00
|
|
|
Mat grayImage = new Mat();
|
|
|
|
Imgproc.cvtColor(rawImage, grayImage, Imgproc.COLOR_RGB2GRAY);
|
|
|
|
assertNotNull("Loading image from file failed!", rawImage);
|
|
|
|
|
|
|
|
|
2018-02-15 12:37:38 -05:00
|
|
|
Mat image = new Mat();
|
2018-02-19 09:23:35 -05:00
|
|
|
|
|
|
|
Imgproc.resize(grayImage, image, new Size(224, 224));
|
2018-02-15 12:37:38 -05:00
|
|
|
|
|
|
|
Mat inputBlob = Dnn.blobFromImage(image);
|
2018-02-19 09:23:35 -05:00
|
|
|
|
2018-02-15 12:37:38 -05:00
|
|
|
assertNotNull("Converting image to blob failed!", inputBlob);
|
|
|
|
|
|
|
|
Mat inputBlobP = new Mat();
|
2018-02-19 09:23:35 -05:00
|
|
|
|
2018-02-15 12:37:38 -05:00
|
|
|
Core.subtract(inputBlob, new Scalar(117.0), inputBlobP);
|
2018-02-19 09:23:35 -05:00
|
|
|
|
2018-02-15 12:37:38 -05:00
|
|
|
|
|
|
|
net.setInput(inputBlobP);
|
|
|
|
|
|
|
|
Mat result = net.forward();
|
|
|
|
|
|
|
|
assertNotNull("Net returned no result!", result);
|
2018-02-15 07:46:02 -05:00
|
|
|
}
|
2018-02-19 09:23:35 -05:00
|
|
|
|
2018-02-15 07:46:02 -05:00
|
|
|
|
2018-02-15 12:37:38 -05:00
|
|
|
public static void main( String[] args ) throws Exception {
|
2018-02-15 07:46:02 -05:00
|
|
|
System.out.println("Reading model from TensorFlow...");
|
|
|
|
|
|
|
|
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
|
|
|
|
|
|
|
ObjectDetector objectDetector = new ObjectDetector();
|
2018-02-19 09:23:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
objectDetector.testGetLayerTypes();
|
|
|
|
objectDetector.testGetLayer();
|
2018-02-15 12:37:38 -05:00
|
|
|
objectDetector.testImage();
|
2018-02-15 07:46:02 -05:00
|
|
|
|
|
|
|
System.out.println("Done...");
|
|
|
|
}
|
|
|
|
}
|