{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Object detection with model zoo model\n", "\n", "In this tutorial, you learn how to use a built-in model zoo model (SSD) to achieve an [object detection](https://en.wikipedia.org/wiki/Object_detection) task.\n", "\n", "## Preparation\n", "\n", "This tutorial requires the installation of Java Kernel. To install Java Kernel, see the [README](https://github.com/awslabs/djl/blob/v0.3.0/jupyter/README.md)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%maven ai.djl:api:0.3.0\n", "%maven ai.djl:repository:0.3.0\n", "%maven ai.djl.mxnet:mxnet-engine:0.3.0\n", "%maven ai.djl.mxnet:mxnet-model-zoo:0.3.0\n", "%maven org.slf4j:slf4j-api:1.7.26\n", "%maven org.slf4j:slf4j-simple:1.7.26\n", "%maven net.java.dev.jna:jna:5.3.0\n", " \n", "// See https://github.com/awslabs/djl/blob/v0.3.0/mxnet/mxnet-engine/README.md\n", "// for more MXNet library selection options\n", "%maven ai.djl.mxnet:mxnet-native-auto:1.6.0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import java.awt.image.*;\n", "import java.nio.file.*;\n", "import ai.djl.modality.cv.*;\n", "import ai.djl.modality.cv.util.*;\n", "import ai.djl.mxnet.zoo.*;\n", "import ai.djl.repository.zoo.*;\n", "import ai.djl.training.util.*;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1: Load image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var img = BufferedImageUtils.fromUrl(\"https://djl-ai.s3.amazonaws.com/resources/images/dog_bike_car.jpg\");\n", "img" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2: Load model zoo model\n", "\n", "In this example, you load a SSD (Single Shot MultiBox Detector) model from the MXNet model zoo.\n", "For more information about model zoo, see the [Model Zoo Documentation](https://github.com/awslabs/djl/blob/v0.3.0/docs/model-zoo.md) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var model = MxModelZoo.SSD.loadModel(new ProgressBar());" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3: Create Predictor and detect an object in the image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var detections = model.newPredictor().predict(img);\n", "\n", "detections" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Check detected result" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ImageVisualization.drawBoundingBoxes(img, detections);\n", "img" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary\n", "\n", "Using the model zoo model provided, you can run inference with just the following three lines of code:\n", "\n", "```\n", "var img = BufferedImageUtils.fromUrl(\"https://djl-ai.s3.amazonaws.com/resources/images/dog_bike_car.jpg\");\n", "var model = MxModelZoo.SSD.loadModel();\n", "var detections = model.newPredictor().predict(img);\n", "```\n", "\n", "You can find full SsdExample source code [here](https://github.com/awslabs/djl/blob/v0.3.0/examples/docs/object_detection.md).\n" ] } ], "metadata": { "kernelspec": { "display_name": "Java", "language": "java", "name": "java" }, "language_info": { "codemirror_mode": "java", "file_extension": ".jshell", "mimetype": "text/x-java-source", "name": "Java", "pygments_lexer": "java", "version": "12.0.2+10" } }, "nbformat": 4, "nbformat_minor": 2 }