generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mikrise2
committed
Jan 4, 2024
1 parent
3b59a18
commit d0fdd85
Showing
10 changed files
with
209 additions
and
210 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
52 changes: 26 additions & 26 deletions
52
ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/modelInference/ImageUtils.kt
This file contains 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
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() | ||
//} |
104 changes: 52 additions & 52 deletions
104
...lugin/src/main/kotlin/org/jetbrains/research/tasktracker/modelInference/model/EmoModel.kt
This file contains 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
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) | ||
// } | ||
//} |
This file contains 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
Oops, something went wrong.