-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResNet18v2.kt
36 lines (30 loc) · 1.2 KB
/
ResNet18v2.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package live_cv.object_detection
import com.github.sarxos.webcam.Webcam
import com.github.sarxos.webcam.WebcamResolution
import live_cv.ImageFrame
import modelHub
import org.jetbrains.kotlinx.dl.api.inference.imagerecognition.ImageRecognitionModel
import org.jetbrains.kotlinx.dl.api.inference.onnx.ONNXModels
import org.jetbrains.kotlinx.dl.dataset.image.ColorMode
import toFloatArray
import java.awt.image.BufferedImage
import kotlin.time.ExperimentalTime
import kotlin.time.measureTime
@OptIn(ExperimentalTime::class)
fun main(args: Array<String>){
val resNet18v2 = modelHub[ONNXModels.CV.ResNet18v2()]
val webcam = Webcam.getWebcams()[args.getOrNull(0)?.toIntOrNull() ?: 2]
print(Webcam.getWebcams())
webcam.viewSize = WebcamResolution.VGA.size
webcam.open()
val frame = ImageFrame(webcam.viewSize.width, webcam.viewSize.height)
while (webcam.isOpen){
measureTime {
frame.showImage(webcam.image)
println(resNet18v2.predictObjectForRN18V2(webcam.image))
}.apply(::println)
}
}
fun ImageRecognitionModel.predictObjectForRN18V2(buff: BufferedImage, ):String? {
return imageNetClassLabels[predict(buff.toFloatArray(ColorMode.BGR,224 to 224))]
}