Last active
May 25, 2024 10:19
-
-
Save dacr/bdae9e6c5a9c47a72d4297b72384df22 to your computer and use it in GitHub Desktop.
Things (objects, people, animals) detection using DJL / published by https://github.com/dacr/code-examples-manager #55ebb071-12a6-4268-b6b2-2a6d835eddab/a323084620a542ecd2c97da678c76c5be4a9cde6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : Things (objects, people, animals) detection using DJL | |
// keywords : djl, machine-learning, tutorial, detection, ai, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 55ebb071-12a6-4268-b6b2-2a6d835eddab | |
// created-on : 2021-03-05T17:40:29Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "org.slf4j:slf4j-api:2.0.13" | |
//> using dep "org.slf4j:slf4j-simple:2.0.13" | |
//> using dep "net.java.dev.jna:jna:5.14.0" | |
//> using dep "ai.djl:api:0.28.0" | |
//> using dep "ai.djl:basicdataset:0.28.0" | |
//> using dep "ai.djl:model-zoo:0.28.0" | |
//> using dep "ai.djl.huggingface:tokenizers:0.28.0" | |
//> using dep "ai.djl.mxnet:mxnet-engine:0.28.0" | |
//> using dep "ai.djl.mxnet:mxnet-model-zoo:0.28.0" | |
//> using dep "ai.djl.pytorch:pytorch-engine:0.28.0" | |
//> using dep "ai.djl.pytorch:pytorch-model-zoo:0.28.0" | |
//> using dep "ai.djl.tensorflow:tensorflow-engine:0.28.0" | |
//> using dep "ai.djl.tensorflow:tensorflow-model-zoo:0.28.0" | |
////> using dep "ai.djl.paddlepaddle:paddlepaddle-engine:0.28.0" | |
////> using dep "ai.djl.paddlepaddle:paddlepaddle-model-zoo:0.28.0" | |
//> using dep "ai.djl.onnxruntime:onnxruntime-engine:0.28.0" | |
// --------------------- | |
// inspired from https://docs.djl.ai/examples/docs/object_detection.html | |
//System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "debug") | |
import ai.djl.Application | |
import ai.djl.engine.Engine | |
import ai.djl.modality.cv.Image | |
import ai.djl.modality.cv.ImageFactory | |
import ai.djl.modality.cv.output.DetectedObjects | |
import ai.djl.modality.cv.output.DetectedObjects.DetectedObject | |
import ai.djl.repository.zoo.Criteria | |
import ai.djl.repository.zoo.ModelZoo | |
import ai.djl.repository.zoo.ZooModel | |
import ai.djl.training.util.ProgressBar | |
import java.nio.file.Files | |
import java.nio.file.Path | |
import java.nio.file.Paths | |
import scala.jdk.CollectionConverters.* | |
// ---------------------------------------------------------------------------------------------- | |
def saveBoundingBoxImage(img: Image, detection: DetectedObjects, outputFile: Path): Unit = { | |
val newImage = img.duplicate() | |
newImage.drawBoundingBoxes(detection) | |
import java.nio.file.Files | |
newImage.save(Files.newOutputStream(outputFile), "png") | |
} | |
def basename(filename: String): String = { | |
filename | |
.split("[/](?=[^/]*$)", 2) | |
.last | |
.split("[.]", 2) | |
.head | |
} | |
// ---------------------------------------------------------------------------------------------- | |
val inputImageURL = "https://mapland.fr/data/ai/images-samples/example-016.jpg" | |
val outputDir = Paths.get("build/output") | |
Files.createDirectories(outputDir) | |
val outputImageFile = outputDir.resolve("detected-objects-" + basename(inputImageURL) + ".png") | |
// ---------------------------------------------------------------------------------------------- | |
//val engineName = Engine.getDefaultEngineName() | |
//val engineName = "TensorFlow" | |
//val engineName = "PyTorch" | |
//println(s"Using engine name : $engineName (default is ${Engine.getDefaultEngineName()})") | |
val criteria = | |
Criteria.builder | |
.optApplication(Application.CV.OBJECT_DETECTION) | |
.setTypes(classOf[Image], classOf[DetectedObjects]) | |
// .optFilters(Map("backbone" -> "mobilenet1.0", "imageSize" -> "416", "dataset" -> "coco").asJava) | |
// .optFilter("backbone", "resnet50") | |
// .optFilter("backbone", "vgg16") | |
.optFilters(Map("backbone" -> "darknet53", "imageSize" -> "416", "dataset" -> "coco").asJava) | |
// .optFilter("backbone", "mobilenet_v2") // TensorFlow | |
// .optEngine(engineName) | |
// .optEngine("OnnxRuntime") | |
.optProgress(new ProgressBar) | |
.build | |
val model = ModelZoo.loadModel(criteria) | |
println(s"Using ${model.getName} ${model.getModelPath}") | |
val predictor = model.newPredictor() | |
val img = ImageFactory.getInstance().fromUrl(inputImageURL) | |
val detection: DetectedObjects = predictor.predict(img) | |
println("number of detected object : " + detection.getNumberOfObjects) | |
detection | |
.items() | |
.asScala | |
.toList | |
.asInstanceOf[List[DetectedObject]] | |
// .filter(_.getProbability > 0.4d) | |
.foreach { ob => | |
println(ob.getClassName + " " + ob.getProbability) | |
} | |
saveBoundingBoxImage(img, detection, outputImageFile) | |
println("images with detected things bounding box saves as " + outputImageFile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment