use of ai.djl.modality.cv.Image in project djl-demo by deepjavalibrary.
the class ImageClassification method main.
public static void main(String[] args) throws ModelException, IOException, TranslateException {
// java default ImageIO doesn't work with GraalVM
ImageFactory.setImageFactory(new GraalvmImageFactory());
Image image;
if (args.length == 0) {
image = ImageFactory.getInstance().fromUrl(IMAGE_URL);
} else {
image = ImageFactory.getInstance().fromUrl(args[0]);
}
Criteria<Image, Classifications> criteria;
if ("TensorFlow".equals(Engine.getInstance().getEngineName())) {
Translator<Image, Classifications> translator = ImageClassificationTranslator.builder().addTransform(new Resize(224)).addTransform(new Normalize(MEAN, STD)).build();
criteria = Criteria.builder().setTypes(Image.class, Classifications.class).optArtifactId("resnet").optTranslator(translator).optProgress(new ProgressBar()).build();
} else {
criteria = Criteria.builder().setTypes(Image.class, Classifications.class).optArtifactId("resnet").optProgress(new ProgressBar()).build();
}
try (ZooModel<Image, Classifications> model = ModelZoo.loadModel(criteria);
Predictor<Image, Classifications> predictor = model.newPredictor()) {
Classifications result = predictor.predict(image);
System.out.println(result.toString());
}
}
use of ai.djl.modality.cv.Image in project djl-demo by deepjavalibrary.
the class WebCam method main.
public static void main(String[] args) throws IOException, ModelException, TranslateException {
ZooModel<Image, DetectedObjects> model = loadModel();
Predictor<Image, DetectedObjects> predictor = model.newPredictor();
OpenCV.loadShared();
VideoCapture capture = new VideoCapture(0);
if (!capture.isOpened()) {
System.out.println("No camera detected");
return;
}
Mat image = new Mat();
boolean captured = false;
for (int i = 0; i < 10; ++i) {
captured = capture.read(image);
if (captured) {
break;
}
try {
Thread.sleep(50);
} catch (InterruptedException ignore) {
// ignore
}
}
if (!captured) {
JOptionPane.showConfirmDialog(null, "Failed to capture image from WebCam.");
}
ViewerFrame frame = new ViewerFrame(image.width(), image.height());
ImageFactory factory = ImageFactory.getInstance();
while (capture.isOpened()) {
if (!capture.read(image)) {
break;
}
Image img = factory.fromImage(image);
DetectedObjects detections = predictor.predict(img);
img.drawBoundingBoxes(detections);
frame.showImage(toBufferedImage((Mat) img.getWrappedImage()));
}
capture.release();
predictor.close();
model.close();
System.exit(0);
}
use of ai.djl.modality.cv.Image in project djl-demo by deepjavalibrary.
the class CanaryTest method main.
public static void main(String[] args) throws IOException, ModelException, TranslateException {
logger.info("");
logger.info("----------Environment Variables----------");
System.getenv().forEach((k, v) -> logger.info(k + ": " + v));
logger.info("");
logger.info("----------Default Engine----------");
Engine.debugEnvironment();
logger.info("");
logger.info("----------Device information----------");
int gpuCount = CudaUtils.getGpuCount();
logger.info("GPU Count: {}", gpuCount);
if (gpuCount > 0) {
logger.info("CUDA: {}", CudaUtils.getCudaVersionString());
logger.info("ARCH: {}", CudaUtils.getComputeCapability(0));
}
String djlEngine = System.getenv("DJL_ENGINE");
if (djlEngine == null) {
djlEngine = "mxnet-native-auto";
}
Device device = NDManager.newBaseManager().getDevice();
if (djlEngine.contains("-native-cu") && !device.isGpu()) {
throw new AssertionError("Expecting load engine on GPU.");
} else if (djlEngine.startsWith("tensorrt")) {
testTensorrt();
return;
} else if (djlEngine.startsWith("onnxruntime")) {
testOnnxRuntime();
return;
} else if (djlEngine.startsWith("xgboost")) {
testXgboost();
return;
} else if (djlEngine.startsWith("tflite")) {
testTflite();
return;
} else if (djlEngine.startsWith("python")) {
testPython();
return;
} else if (djlEngine.startsWith("dlr")) {
testDlr();
// similar to DLR, fastText and SentencePiece only support Mac and Ubuntu 16.04+
testFastText();
testSentencePiece();
return;
} else if (djlEngine.startsWith("paddle")) {
testPaddle();
return;
}
logger.info("");
logger.info("----------Test inference----------");
String url = "https://resources.djl.ai/images/dog_bike_car.jpg";
Image img = ImageFactory.getInstance().fromUrl(url);
String backbone = "resnet50";
Map<String, String> options = null;
if ("TensorFlow".equals(Engine.getInstance().getEngineName())) {
backbone = "mobilenet_v2";
options = new ConcurrentHashMap<>();
options.put("Tags", "");
}
Criteria<Image, DetectedObjects> criteria = Criteria.builder().optApplication(Application.CV.OBJECT_DETECTION).setTypes(Image.class, DetectedObjects.class).optFilter("backbone", backbone).optOptions(options).build();
try (ZooModel<Image, DetectedObjects> model = ModelZoo.loadModel(criteria)) {
try (Predictor<Image, DetectedObjects> predictor = model.newPredictor()) {
DetectedObjects detection = predictor.predict(img);
logger.info("{}", detection);
}
}
}
use of ai.djl.modality.cv.Image in project djl-demo by deepjavalibrary.
the class CanaryTest method testDlr.
private static void testDlr() throws ModelException, IOException, TranslateException {
String os;
if (System.getProperty("os.name").toLowerCase().startsWith("mac")) {
os = "osx";
} else if (System.getProperty("os.name").toLowerCase().startsWith("linux")) {
os = "linux";
} else {
throw new AssertionError("DLR only work on mac and Linux.");
}
ImageClassificationTranslator translator = ImageClassificationTranslator.builder().addTransform(new Resize(224, 224)).addTransform(new ToTensor()).build();
Criteria<Image, Classifications> criteria = Criteria.builder().setTypes(Image.class, Classifications.class).optApplication(Application.CV.IMAGE_CLASSIFICATION).optFilter("layers", "50").optFilter("os", os).optTranslator(translator).optEngine("DLR").optProgress(new ProgressBar()).build();
String url = "https://resources.djl.ai/images/kitten.jpg";
Image image = ImageFactory.getInstance().fromUrl(url);
try (ZooModel<Image, Classifications> model = ModelZoo.loadModel(criteria);
Predictor<Image, Classifications> predictor = model.newPredictor()) {
Classifications classifications = predictor.predict(image);
logger.info("{}", classifications);
}
}
use of ai.djl.modality.cv.Image in project djl-demo by deepjavalibrary.
the class CanaryTest method testTflite.
private static void testTflite() throws ModelException, IOException, TranslateException {
if (System.getProperty("os.name").startsWith("Win")) {
throw new AssertionError("TFLite only work on macOS and Linux.");
}
Criteria<Image, Classifications> criteria = Criteria.builder().setTypes(Image.class, Classifications.class).optEngine("TFLite").optFilter("dataset", "aiyDish").build();
try (ZooModel<Image, Classifications> model = ModelZoo.loadModel(criteria);
Predictor<Image, Classifications> predictor = model.newPredictor()) {
Image image = ImageFactory.getInstance().fromUrl("https://resources.djl.ai/images/sachertorte.jpg");
Classifications prediction = predictor.predict(image);
logger.info(prediction.toString());
if (!"Sachertorte".equals(prediction.best().getClassName())) {
throw new AssertionError("Wrong prediction result");
}
}
}
Aggregations