Last active
May 3, 2025 09:32
-
-
Save dacr/45d6ec5905eb5034bb76078e98057550 to your computer and use it in GitHub Desktop.
image to text try / published by https://github.com/dacr/code-examples-manager #b35b9b44-ec04-46f5-b904-43605e2b2107/c18faf5c76f96856c21d7d96e34a42af7873b0fa
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 to text try | |
// keywords : djl, machine-learning, image-to-text, 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 : b35b9b44-ec04-46f5-b904-43605e2b2107 | |
// created-on : 2024-07-20T08:42:43+02:00 | |
// 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.29.0" | |
//> using dep "ai.djl:basicdataset:0.29.0" | |
//> using dep "ai.djl:model-zoo:0.29.0" | |
//> using dep "ai.djl.huggingface:tokenizers:0.29.0" | |
//> using dep "ai.djl.mxnet:mxnet-engine:0.29.0" | |
//> using dep "ai.djl.mxnet:mxnet-model-zoo:0.29.0" | |
//> using dep "ai.djl.pytorch:pytorch-engine:0.29.0" | |
//> using dep "ai.djl.pytorch:pytorch-model-zoo:0.29.0" | |
//> using dep "ai.djl.tensorflow:tensorflow-engine:0.29.0" | |
//> using dep "ai.djl.tensorflow:tensorflow-model-zoo:0.29.0" | |
//> using dep "ai.djl.onnxruntime:onnxruntime-engine:0.29.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