Last active
February 3, 2026 20:25
-
-
Save dacr/8f9e4501c98ce80e539343ef7f630f03 to your computer and use it in GitHub Desktop.
image classification try / published by https://github.com/dacr/code-examples-manager #32c4a82b-8a40-480a-bad9-5390bceed3dd/45e3088da3de825c63cbeed6dbe8c725a84842a8
This file contains hidden or 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 : image classification try | |
| // keywords : djl, machine-learning, tutorial, detection, ai, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 32c4a82b-8a40-480a-bad9-5390bceed3dd | |
| // created-on : 2022-03-09T20:17:01+01:00 | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| // --------------------- | |
| //> using scala "3.7.2" | |
| //> using dep "org.slf4j:slf4j-api:2.0.17" | |
| //> using dep "org.slf4j:slf4j-simple:2.0.17" | |
| //> using dep "net.java.dev.jna:jna:5.17.0" | |
| //> using dep "ai.djl:api:0.33.0" | |
| //> using dep "ai.djl:basicdataset:0.33.0" | |
| //> using dep "ai.djl:model-zoo:0.33.0" | |
| //> using dep "ai.djl.huggingface:tokenizers:0.33.0" | |
| //> using dep "ai.djl.mxnet:mxnet-engine:0.33.0" | |
| //> using dep "ai.djl.mxnet:mxnet-model-zoo:0.33.0" | |
| //> using dep "ai.djl.pytorch:pytorch-engine:0.33.0" | |
| //> using dep "ai.djl.pytorch:pytorch-model-zoo:0.33.0" | |
| //> using dep "ai.djl.tensorflow:tensorflow-engine:0.33.0" | |
| //> using dep "ai.djl.tensorflow:tensorflow-model-zoo:0.33.0" | |
| //> using dep "ai.djl.onnxruntime:onnxruntime-engine:0.33.0" | |
| // --------------------- | |
| //System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "debug") | |
| import ai.djl.Application | |
| import ai.djl.engine.Engine | |
| import ai.djl.modality.Classifications | |
| import ai.djl.modality.Classifications.Classification | |
| import ai.djl.modality.cv.Image | |
| import ai.djl.modality.cv.ImageFactory | |
| 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.* | |
| // ---------------------------------------------------------------------------------------------- | |
| val criteria = | |
| Criteria.builder | |
| .optApplication(Application.CV.IMAGE_CLASSIFICATION) | |
| .setTypes(classOf[Image], classOf[Classifications]) | |
| // ------------------------------------ | |
| //.optFilter("flavor","v1") | |
| //.optFilter("dataset","cifar10") | |
| // ------------------------------------ | |
| //.optFilter("flavor","v3_large") | |
| //.optFilter("dataset","imagenet") | |
| // ------------------------------------ | |
| .optFilter("flavor","v1d") | |
| .optFilter("dataset","imagenet") | |
| .optProgress(new ProgressBar) | |
| .build | |
| val model = ModelZoo.loadModel(criteria) | |
| val predictor = model.newPredictor() | |
| val inputImageURL = "https://data.code-examples.org/ai/images-samples/example-001.jpg" | |
| val img = ImageFactory.getInstance().fromUrl(inputImageURL) | |
| val found: Classifications = predictor.predict(img) | |
| found.items() | |
| .asScala | |
| .toList | |
| .asInstanceOf[List[Classification]] | |
| .filter(_.getProbability > 0.5d) | |
| .foreach{cl => | |
| println(cl.getClassName+" "+cl.getProbability) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment