Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
litan committed Oct 14, 2024
1 parent 1ad5f24 commit 177c823
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
9 changes: 4 additions & 5 deletions src/main/scala/net/kogics/kojo/aiapp/FaceEmbedder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,14 @@ class FaceEmbedder(modelDir: String) {
faceFeatureModel.toString
}

def faceEmbedding(image: Mat): Array[Float] = {
def faceEmbedding(imageMat: Mat): Array[Float] = {
import ai.djl.pytorch.jni.JniUtils
JniUtils.setGraphExecutorOptimize(false)
Using.Manager { use =>
println("Calculating embedding")
val src = Java2DFrameUtils.toBufferedImage(image)
val faceImage = bufferedImageToDjlImage(src)
println(s"Feeding into net, img(${faceImage.getWidth}, ${faceImage.getHeight})")
val ret = faceFeaturePredictor.predict(faceImage)
val faceDjlImage = matToDjlImage(imageMat)
println(s"Feeding into net, img(${faceDjlImage.getWidth}, ${faceDjlImage.getHeight})")
val ret = faceFeaturePredictor.predict(faceDjlImage)
println("Done")
ret
}.get
Expand Down
8 changes: 3 additions & 5 deletions src/main/scala/net/kogics/kojo/aiapp/ObjectDetector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class ObjectDetector(modelDir: String) {
"resize" -> Boolean.box(true),
"rescale" -> Boolean.box(true),
"optApplyRatio" -> Boolean.box(true),
"toTensor" -> Boolean.box(true),
"threshold" -> Double.box(0.4),
)

Expand All @@ -32,20 +31,19 @@ class ObjectDetector(modelDir: String) {
.setTypes(classOf[Image], classOf[DetectedObjects])
.optModelPath(Paths.get(modelDir))
.optTranslatorFactory(new ai.djl.modality.cv.translator.YoloV5TranslatorFactory())
// .optArguments(args.asJava)
.optArguments(args.asJava)
.build()

val mdl = criteria.loadModel()
println("Done")
(mdl, mdl.newPredictor())
}

def findObjects(image: Mat): (DetectedObjects, BufferedImage) = {
def findObjects(imageMat: Mat): (DetectedObjects, BufferedImage) = {
import ai.djl.pytorch.jni.JniUtils
JniUtils.setGraphExecutorOptimize(false)
Using.Manager { use =>
val src = Java2DFrameUtils.toBufferedImage(image)
val djlImage = bufferedImageToDjlImage(src)
val djlImage = matToDjlImage(imageMat)
val detectedObjects = predictor.predict(djlImage)
djlImage.drawBoundingBoxes(detectedObjects)
(detectedObjects, djlImage.getWrappedImage.asInstanceOf[BufferedImage])
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/net/kogics/kojo/aiapp/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ package object aiapp {
Java2DFrameUtils.toBufferedImage(imageMat)
}

def matToDjlImage(imageMat: Mat): Image = {
bufferedImageToDjlImage(matToBufferedImage(imageMat))
}
}

0 comments on commit 177c823

Please sign in to comment.