Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
mikrise2 committed Jan 4, 2024
1 parent 3b59a18 commit d0fdd85
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 210 deletions.
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ kinference = "0.2.16"
ktor = "2.3.4"
javacv = "1.5.9"
slf4j = "1.7.35"
opencv = "4.7.0-0"
#opencv = "4.7.0-0"
changelog = "2.1.2"
postgres = "42.3.1"
logback = "1.4.11"
Expand All @@ -28,7 +28,7 @@ csv = { module = "org.apache.commons:commons-csv", version.ref = "csv" }
joda = { module = "joda-time:joda-time", version.ref = "joda" }
kinference = { module = "io.kinference:inference-core-jvm", version.ref = "kinference" }
javacv = { module = "org.bytedeco:javacv-platform", version.ref = "javacv" }
opencv = { module = "org.openpnp:opencv", version.ref = "opencv" }
#opencv = { module = "org.openpnp:opencv", version.ref = "opencv" }
ktor-client-cio = { module = "io.ktor:ktor-client-cio", version.ref = "ktor" }
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
ktor-client-json = { module = "io.ktor:ktor-client-json", version.ref = "ktor" }
Expand Down
4 changes: 2 additions & 2 deletions ij-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ dependencies {
implementation(rootProject.libs.kaml)
implementation(rootProject.libs.snakeyaml)
implementation(rootProject.libs.kinference)
implementation(rootProject.libs.javacv)
// implementation(rootProject.libs.javacv)
implementation(rootProject.libs.ktor.client.cio)
implementation(rootProject.libs.ktor.client.core)
implementation(rootProject.libs.ktor.client.json)
implementation(rootProject.libs.ktor.client.serialization)
implementation(rootProject.libs.ktor.client.content.negotiation)
implementation(rootProject.libs.ktor.serialization.kotlinx.json)
implementation(rootProject.libs.slf4j)
implementation(rootProject.libs.opencv)
// implementation(rootProject.libs.opencv)
}

intellij {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package org.jetbrains.research.tasktracker.activities

import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity
import com.intellij.openapi.startup.StartupActivity
import org.jetbrains.research.tasktracker.TaskTrackerPlugin
import org.jetbrains.research.tasktracker.config.MainTaskTrackerConfig

// Put into plugin.xml
class InitActivity : StartupActivity {
class InitActivity : ProjectActivity {
private val logger: Logger = Logger.getInstance(javaClass)

init {
override suspend fun execute(project: Project) {
logger.info("${MainTaskTrackerConfig.PLUGIN_NAME}: startup activity")
TaskTrackerPlugin.initPlugin()
}

override fun runActivity(project: Project) = Unit
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jetbrains.research.tasktracker.modelInference

import org.jetbrains.research.tasktracker.config.emotion.EmotionConfig
import org.opencv.core.Mat
//import org.opencv.core.Mat

class EmoPrediction(val probabilities: Map<Int, Double>, private val thresholds: Map<Int, Double>) {

Expand All @@ -21,5 +21,5 @@ interface EmoPredictor {

val emotionConfig: EmotionConfig

suspend fun predict(image: Mat): EmoPrediction
// suspend fun predict(image: Mat): EmoPrediction
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package org.jetbrains.research.tasktracker.modelInference

import org.opencv.core.Mat
import org.opencv.core.Size
import org.opencv.imgproc.Imgproc

fun resizeImage(image: Mat, pixels: Double = 64.0): Mat {
val resizedImage = Mat()
Imgproc.resize(image, resizedImage, Size(pixels, pixels))

return resizedImage
}

fun Mat.prepare(): Mat {
val gImage = this.toGrayImage()
return resizeImage(gImage)
}

fun Mat.toGrayImage(): Mat {
val grayImage = Mat()
Imgproc.cvtColor(this, grayImage, Imgproc.COLOR_RGB2GRAY)

return grayImage
}

fun getPixel(tensor: IntArray, image: Mat, i: Int = 2, j: Int = 3): Float {
return image.get(tensor[i], tensor[j])[0].toFloat()
}
//import org.opencv.core.Mat
//import org.opencv.core.Size
//import org.opencv.imgproc.Imgproc
//
//fun resizeImage(image: Mat, pixels: Double = 64.0): Mat {
// val resizedImage = Mat()
// Imgproc.resize(image, resizedImage, Size(pixels, pixels))
//
// return resizedImage
//}
//
//fun Mat.prepare(): Mat {
// val gImage = this.toGrayImage()
// return resizeImage(gImage)
//}
//
//fun Mat.toGrayImage(): Mat {
// val grayImage = Mat()
// Imgproc.cvtColor(this, grayImage, Imgproc.COLOR_RGB2GRAY)
//
// return grayImage
//}
//
//fun getPixel(tensor: IntArray, image: Mat, i: Int = 2, j: Int = 3): Float {
// return image.get(tensor[i], tensor[j])[0].toFloat()
//}
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
package org.jetbrains.research.tasktracker.modelInference.model

import io.kinference.core.KIEngine
import io.kinference.core.data.tensor.KITensor
import io.kinference.core.data.tensor.asTensor
import io.kinference.core.model.KIModel
import io.kinference.ndarray.arrays.FloatNDArray
import kotlinx.coroutines.runBlocking
import org.jetbrains.research.tasktracker.config.emotion.EmotionConfig
import org.jetbrains.research.tasktracker.modelInference.EmoPrediction
import org.jetbrains.research.tasktracker.modelInference.EmoPredictor
import org.jetbrains.research.tasktracker.modelInference.getPixel
import org.jetbrains.research.tasktracker.modelInference.prepare
import org.opencv.core.Mat

class EmoModel(override val emotionConfig: EmotionConfig) : EmoPredictor {

init {
runBlocking {
loadModel()
}
}

private lateinit var model: KIModel
private suspend fun loadModel() {
model = KIEngine.loadModel(
EmoModel::class.java
.getResource(emotionConfig.modelFilename)?.readBytes()
?: error("${emotionConfig.modelFilename} must exist")
)
}

override suspend fun predict(image: Mat): EmoPrediction {
val prepImage = image.prepare()
val tensor = FloatNDArray(INPUT_SHAPE) { idx: IntArray ->
getPixel(idx, prepImage)
}

val outputs = model.predict(listOf(tensor.asTensor(emotionConfig.modelInputGate)))
val output = outputs[emotionConfig.modelOutputGate]
val softmaxOutput = ((output as KITensor).data as FloatNDArray).softmax()
val outputArray = softmaxOutput.array.toArray()

val probabilities = outputArray.mapIndexed { index: Int, prob: Float -> index to prob.toDouble() }.toMap()
return EmoPrediction(probabilities, emotionConfig.modelPositionToThreshold)
}

// TODO maybe we need to find a better solution for face detection?
companion object {
private val INPUT_SHAPE = intArrayOf(1, 1, 64, 64)
}
}
//package org.jetbrains.research.tasktracker.modelInference.model
//
//import io.kinference.core.KIEngine
//import io.kinference.core.data.tensor.KITensor
//import io.kinference.core.data.tensor.asTensor
//import io.kinference.core.model.KIModel
//import io.kinference.ndarray.arrays.FloatNDArray
//import kotlinx.coroutines.runBlocking
//import org.jetbrains.research.tasktracker.config.emotion.EmotionConfig
//import org.jetbrains.research.tasktracker.modelInference.EmoPrediction
//import org.jetbrains.research.tasktracker.modelInference.EmoPredictor
//import org.jetbrains.research.tasktracker.modelInference.getPixel
//import org.jetbrains.research.tasktracker.modelInference.prepare
//import org.opencv.core.Mat
//
//class EmoModel(override val emotionConfig: EmotionConfig) : EmoPredictor {
//
// init {
// runBlocking {
// loadModel()
// }
// }
//
// private lateinit var model: KIModel
// private suspend fun loadModel() {
// model = KIEngine.loadModel(
// EmoModel::class.java
// .getResource(emotionConfig.modelFilename)?.readBytes()
// ?: error("${emotionConfig.modelFilename} must exist")
// )
// }
//
// override suspend fun predict(image: Mat): EmoPrediction {
// val prepImage = image.prepare()
// val tensor = FloatNDArray(INPUT_SHAPE) { idx: IntArray ->
// getPixel(idx, prepImage)
// }
//
// val outputs = model.predict(listOf(tensor.asTensor(emotionConfig.modelInputGate)))
// val output = outputs[emotionConfig.modelOutputGate]
// val softmaxOutput = ((output as KITensor).data as FloatNDArray).softmax()
// val outputArray = softmaxOutput.array.toArray()
//
// val probabilities = outputArray.mapIndexed { index: Int, prob: Float -> index to prob.toDouble() }.toMap()
// return EmoPrediction(probabilities, emotionConfig.modelPositionToThreshold)
// }
//
// // TODO maybe we need to find a better solution for face detection?
// companion object {
// private val INPUT_SHAPE = intArrayOf(1, 1, 64, 64)
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ class WebCamService : Disposable {

fun startTakingPhotos(emoPredictor: EmoPredictor, trackerLogger: WebCamLogger) {
var photosMade = 0
timerToMakePhoto.scheduleAtFixedRate(
timerTask {
makePhoto()?.let {
photosMade++
runBlocking {
it.guessEmotionAndLog(emoPredictor, trackerLogger)
}
}
},
TIME_TO_PHOTO_DELAY,
TIME_TO_PHOTO_DELAY
)
// timerToMakePhoto.scheduleAtFixedRate(
// timerTask {
// makePhoto()?.let {
// photosMade++
// runBlocking {
//// it.guessEmotionAndLog(emoPredictor, trackerLogger)
// }
// }
// },
// TIME_TO_PHOTO_DELAY,
// TIME_TO_PHOTO_DELAY
// )
}

fun stopTakingPhotos() {
Expand Down
Loading

0 comments on commit d0fdd85

Please sign in to comment.