-
Notifications
You must be signed in to change notification settings - Fork 10
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
Showing
7 changed files
with
1,140 additions
and
10 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
android/src/main/java/com/swmansion/rnexecutorch/Classification.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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.swmansion.rnexecutorch | ||
|
||
import android.util.Log | ||
import com.facebook.react.bridge.Promise | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.swmansion.rnexecutorch.models.classification.ClassificationModel | ||
import com.swmansion.rnexecutorch.utils.ETError | ||
import com.swmansion.rnexecutorch.utils.ImageProcessor | ||
import org.opencv.android.OpenCVLoader | ||
import com.facebook.react.bridge.Arguments | ||
import com.facebook.react.bridge.WritableMap | ||
|
||
class Classification(reactContext: ReactApplicationContext) : | ||
NativeClassificationSpec(reactContext) { | ||
|
||
private lateinit var classificationModel: ClassificationModel | ||
|
||
companion object { | ||
const val NAME = "Classification" | ||
init { | ||
if(!OpenCVLoader.initLocal()){ | ||
Log.d("rn_executorch", "OpenCV not loaded") | ||
} else { | ||
Log.d("rn_executorch", "OpenCV loaded") | ||
} | ||
} | ||
} | ||
|
||
override fun loadModule(modelSource: String, promise: Promise) { | ||
try { | ||
classificationModel = ClassificationModel(reactApplicationContext) | ||
classificationModel.loadModel(modelSource) | ||
promise.resolve(0) | ||
} catch (e: Exception) { | ||
promise.reject(e.message!!, ETError.InvalidModelPath.toString()) | ||
} | ||
} | ||
|
||
override fun forward(input: String, promise: Promise) { | ||
try { | ||
val image = ImageProcessor.readImage(input) | ||
val output = classificationModel.runModel(image) | ||
|
||
val writableMap: WritableMap = Arguments.createMap() | ||
|
||
for ((key, value) in output) { | ||
writableMap.putDouble(key, value.toDouble()) | ||
} | ||
|
||
promise.resolve(writableMap) | ||
}catch(e: Exception){ | ||
promise.reject(e.message!!, e.message) | ||
} | ||
} | ||
|
||
override fun getName(): String { | ||
return NAME | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
...oid/src/main/java/com/swmansion/rnexecutorch/models/classification/ClassificationModel.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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.swmansion.rnexecutorch.models.classification | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.swmansion.rnexecutorch.utils.ImageProcessor | ||
import org.opencv.core.Mat | ||
import org.opencv.core.Size | ||
import org.opencv.imgproc.Imgproc | ||
import org.pytorch.executorch.Tensor | ||
import org.pytorch.executorch.EValue | ||
import com.swmansion.rnexecutorch.models.BaseModel | ||
|
||
|
||
class ClassificationModel(reactApplicationContext: ReactApplicationContext) : BaseModel<Mat, Map<String, Float>>(reactApplicationContext) { | ||
private fun getModelImageSize(): Size { | ||
val inputShape = module.getInputShape(0) | ||
val width = inputShape[inputShape.lastIndex] | ||
val height = inputShape[inputShape.lastIndex - 1] | ||
|
||
return Size(height.toDouble(), width.toDouble()) | ||
} | ||
|
||
override fun preprocess(input: Mat): EValue { | ||
Imgproc.resize(input, input, getModelImageSize()) | ||
return ImageProcessor.matToEValue(input, module.getInputShape(0)) | ||
} | ||
|
||
override fun postprocess(output: Array<EValue>): Map<String, Float> { | ||
val tensor = output[0].toTensor() | ||
val probabilities = tensor.dataAsFloatArray | ||
|
||
val result = mutableMapOf<String, Float>() | ||
|
||
for (i in probabilities.indices) { | ||
result[imagenet1k_v1_labels_map[i]!!] = probabilities[i] | ||
} | ||
|
||
return result | ||
} | ||
|
||
override fun runModel(input: Mat): Map<String, Float> { | ||
val modelInput = preprocess(input) | ||
val modelOutput = forward(modelInput) | ||
return postprocess(modelOutput) | ||
} | ||
} |
Oops, something went wrong.