-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [Android] replacing MLKit with TensorFlow lib
- Loading branch information
1 parent
9a39373
commit b8b1939
Showing
7 changed files
with
162 additions
and
194 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
179 changes: 92 additions & 87 deletions
179
...bjectdetection/realtimeobjectdetectionprocessor/RealtimeObjectDetectionProcessorPlugin.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,109 +1,114 @@ | ||
package com.visioncamerarealtimeobjectdetection.realtimeobjectdetectionprocessor | ||
|
||
import android.graphics.Matrix | ||
import android.graphics.RectF | ||
import androidx.camera.core.ImageProxy | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReadableMap | ||
import com.facebook.react.bridge.WritableNativeArray | ||
import com.facebook.react.bridge.WritableNativeMap | ||
import com.facebook.react.bridge.ReadableMap | ||
import com.google.android.gms.tasks.Tasks | ||
import com.google.mlkit.common.model.LocalModel | ||
import com.google.mlkit.vision.common.InputImage | ||
import com.google.mlkit.vision.objects.ObjectDetection | ||
import com.google.mlkit.vision.objects.ObjectDetector | ||
import com.google.mlkit.vision.objects.custom.CustomObjectDetectorOptions | ||
import com.google.android.odml.image.MediaMlImageBuilder | ||
import com.mrousavy.camera.frameprocessor.FrameProcessorPlugin | ||
import org.tensorflow.lite.task.core.BaseOptions | ||
import org.tensorflow.lite.task.vision.detector.ObjectDetector | ||
|
||
class RealtimeObjectDetectionProcessorPlugin : FrameProcessorPlugin("detectObjects") { | ||
class RealtimeObjectDetectionProcessorPlugin(reactContext: ReactApplicationContext) : | ||
FrameProcessorPlugin("detectObjects") { | ||
private val _context: ReactApplicationContext = reactContext | ||
private var _detector: ObjectDetector? = null | ||
|
||
fun rotateRect(rect: RectF, degrees: Int): RectF { | ||
val matrix = Matrix() | ||
matrix.postRotate(degrees.toFloat(), rect.centerX(), rect.centerY()) | ||
val rotatedRect = RectF(rect) | ||
matrix.mapRect(rotatedRect) | ||
return rotatedRect | ||
} | ||
|
||
fun getDetectorWithModelFile(config: ReadableMap): ObjectDetector { | ||
if (_detector == null) { | ||
val modelFile = config.getString("modelFile") | ||
val localModel = LocalModel.Builder().setAssetFilePath("custom/$modelFile").build() | ||
|
||
val classificationConfidenceThreshold = config.getDouble("classificationConfidenceThreshold") | ||
val maxPerObjectLabelCount = config.getInt("maxPerObjectLabelCount") | ||
val customObjectDetectorOptions = | ||
CustomObjectDetectorOptions.Builder(localModel) | ||
.setDetectorMode(CustomObjectDetectorOptions.SINGLE_IMAGE_MODE) | ||
.enableClassification() | ||
.enableMultipleObjects() | ||
.setClassificationConfidenceThreshold(classificationConfidenceThreshold.toFloat()) | ||
.setMaxPerObjectLabelCount(maxPerObjectLabelCount) | ||
.build() | ||
|
||
_detector = ObjectDetection.getClient(customObjectDetectorOptions) | ||
|
||
val scoreThreshold = config.getDouble("scoreThreshold").toFloat() | ||
val maxResults = config.getInt("maxResults") | ||
val numThreads = config.getInt("numThreads") | ||
|
||
val baseOptionsBuilder = BaseOptions.builder().setNumThreads(numThreads) | ||
|
||
val optionsBuilder = | ||
ObjectDetector.ObjectDetectorOptions.builder() | ||
.setBaseOptions(baseOptionsBuilder.build()) | ||
.setScoreThreshold(scoreThreshold) | ||
.setMaxResults(maxResults) | ||
|
||
_detector = | ||
ObjectDetector.createFromFileAndOptions( | ||
_context, | ||
"custom/$modelFile", | ||
optionsBuilder.build() | ||
) | ||
} | ||
return _detector!! | ||
} | ||
|
||
override fun callback(frame: ImageProxy, params: Array<Any>): WritableNativeArray { | ||
val mediaImage = frame.image | ||
if (mediaImage != null) { | ||
val config = params[0] as ReadableMap; | ||
val image = InputImage.fromMediaImage(mediaImage, frame.imageInfo.rotationDegrees) | ||
val task = getDetectorWithModelFile(config).process(image) | ||
val results = WritableNativeArray() | ||
|
||
val frameWidth = | ||
if (frame.imageInfo.rotationDegrees == 90 || frame.imageInfo.rotationDegrees == 270) | ||
mediaImage.width | ||
else mediaImage.height | ||
val frameHeight = | ||
if (frame.imageInfo.rotationDegrees == 90 || frame.imageInfo.rotationDegrees == 270) | ||
mediaImage.height | ||
else mediaImage.width | ||
|
||
try { | ||
val objects = Tasks.await(task) | ||
|
||
for (detectedObject in objects) { | ||
val labels = WritableNativeArray() | ||
|
||
for (label in detectedObject.labels) { | ||
val labelMap = WritableNativeMap() | ||
|
||
labelMap.putInt("index", label.index) | ||
labelMap.putString("label", label.text) | ||
labelMap.putDouble("confidence", label.confidence.toDouble()) | ||
|
||
labels.pushMap(labelMap) | ||
} | ||
|
||
if (labels.size() > 0) { | ||
val objectMap = WritableNativeMap() | ||
|
||
objectMap.putArray("labels", labels) | ||
objectMap.putDouble( | ||
"top", | ||
(detectedObject.boundingBox.top.toFloat() / frameWidth).toDouble() | ||
) | ||
objectMap.putDouble( | ||
"left", | ||
(detectedObject.boundingBox.left.toFloat() / frameHeight).toDouble() | ||
) | ||
objectMap.putDouble( | ||
"width", | ||
((detectedObject.boundingBox.right - detectedObject.boundingBox.left) | ||
.toFloat() / frameHeight) | ||
.toDouble() | ||
) | ||
objectMap.putDouble( | ||
"height", | ||
((detectedObject.boundingBox.bottom - detectedObject.boundingBox.top) | ||
.toFloat() / frameWidth) | ||
.toDouble() | ||
) | ||
|
||
results.pushMap(objectMap) | ||
} | ||
} | ||
|
||
return results | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
|
||
if (mediaImage == null) { | ||
return WritableNativeArray() | ||
} | ||
|
||
return WritableNativeArray() | ||
val config = params[0] as ReadableMap | ||
|
||
val mlImage = MediaMlImageBuilder(mediaImage).build() | ||
|
||
val frameWidth = | ||
if (frame.imageInfo.rotationDegrees == 90 || frame.imageInfo.rotationDegrees == 270) | ||
mediaImage.width | ||
else mediaImage.height | ||
val frameHeight = | ||
if (frame.imageInfo.rotationDegrees == 90 || frame.imageInfo.rotationDegrees == 270) | ||
mediaImage.height | ||
else mediaImage.width | ||
|
||
val results = WritableNativeArray() | ||
val detectedObjects = getDetectorWithModelFile(config).detect(mlImage) | ||
|
||
for (detectedObject in detectedObjects) { | ||
val labels = WritableNativeArray() | ||
|
||
for (label in detectedObject.categories) { | ||
val labelMap = WritableNativeMap() | ||
|
||
labelMap.putInt("index", label.index) | ||
labelMap.putString("label", label.label) | ||
labelMap.putDouble("confidence", label.score.toDouble()) | ||
|
||
labels.pushMap(labelMap) | ||
} | ||
|
||
if (labels.size() > 0) { | ||
val objectMap = WritableNativeMap() | ||
|
||
objectMap.putArray("labels", labels) | ||
|
||
val boundingBox = | ||
rotateRect(detectedObject.boundingBox, frame.imageInfo.rotationDegrees) | ||
|
||
objectMap.putDouble("top", (boundingBox.top.toFloat() / frameHeight).toDouble()) | ||
objectMap.putDouble("left", (boundingBox.left.toFloat() / frameWidth).toDouble()) | ||
objectMap.putDouble( | ||
"width", | ||
((boundingBox.right - boundingBox.left).toFloat() / frameWidth).toDouble() | ||
) | ||
objectMap.putDouble( | ||
"height", | ||
((boundingBox.bottom - boundingBox.top).toFloat() / frameHeight).toDouble() | ||
) | ||
|
||
results.pushMap(objectMap) | ||
} | ||
} | ||
return results | ||
} | ||
} |
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
Oops, something went wrong.