Skip to content

Instantly share code, notes, and snippets.

@woxtu
Last active July 13, 2020 12:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woxtu/de819ad63e217118523b5cb3fe334343 to your computer and use it in GitHub Desktop.
Save woxtu/de819ad63e217118523b5cb3fe334343 to your computer and use it in GitHub Desktop.
Object detection in Clojure using Deep Java Library
{:deps
{ai.djl/api {:mvn/version "0.4.1"}
ai.djl.mxnet/mxnet-model-zoo {:mvn/version "0.4.1"}
ai.djl.mxnet/mxnet-native-auto {:mvn/version "1.6.0"}
org.apache.logging.log4j/log4j-slf4j-impl {:mvn/version "2.13.0"}}}
;; ref: https://github.com/awslabs/djl/blob/master/examples/src/main/java/ai/djl/examples/inference/ObjectDetection.java
(require '[clojure.java.io :as io])
(import [ai.djl Application$CV]
[ai.djl.modality.cv ImageVisualization]
[ai.djl.modality.cv.output DetectedObjects]
[ai.djl.repository.zoo Criteria ModelZoo]
[ai.djl.training.util ProgressBar]
[java.awt.image BufferedImage]
[javax.imageio ImageIO])
(let [file (first *command-line-args*)
image (ImageIO/read (io/file file))
criteria (-> (Criteria/builder)
(.optApplication Application$CV/OBJECT_DETECTION)
(.setTypes BufferedImage DetectedObjects)
(.optFilter "backbone" "resnet50")
(.optProgress (ProgressBar.))
(.build))]
(with-open [model (ModelZoo/loadModel criteria)
predictor (.newPredictor model)]
(let [detection (.predict predictor image)]
(ImageVisualization/drawBoundingBoxes image detection)
(ImageIO/write image "png" (io/file (str "detected-" file)))
(println "Detected objects image has been saved in:" (str "detected-" file))
(println detection))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment