|
| 1 | +package com.swmansion.rnexecutorch |
| 2 | + |
| 3 | +import android.util.Log |
| 4 | +import com.facebook.react.bridge.Arguments |
| 5 | +import com.facebook.react.bridge.Promise |
| 6 | +import com.facebook.react.bridge.ReactApplicationContext |
| 7 | +import com.swmansion.rnexecutorch.utils.ETError |
| 8 | +import com.swmansion.rnexecutorch.utils.ImageProcessor |
| 9 | +import org.opencv.android.OpenCVLoader |
| 10 | +import com.swmansion.rnexecutorch.models.ocr.Recognizer |
| 11 | +import com.swmansion.rnexecutorch.models.ocr.VerticalDetector |
| 12 | +import com.swmansion.rnexecutorch.models.ocr.utils.CTCLabelConverter |
| 13 | +import com.swmansion.rnexecutorch.models.ocr.utils.Constants |
| 14 | +import com.swmansion.rnexecutorch.models.ocr.utils.RecognizerUtils |
| 15 | +import org.opencv.core.Core |
| 16 | +import org.opencv.core.Mat |
| 17 | + |
| 18 | +class VerticalOCR(reactContext: ReactApplicationContext) : |
| 19 | + NativeVerticalOCRSpec(reactContext) { |
| 20 | + |
| 21 | + private lateinit var detectorLarge: VerticalDetector |
| 22 | + private lateinit var detectorNarrow: VerticalDetector |
| 23 | + private lateinit var recognizer: Recognizer |
| 24 | + private lateinit var converter: CTCLabelConverter |
| 25 | + private var independentCharacters = true |
| 26 | + |
| 27 | + companion object { |
| 28 | + const val NAME = "VerticalOCR" |
| 29 | + } |
| 30 | + |
| 31 | + init { |
| 32 | + if (!OpenCVLoader.initLocal()) { |
| 33 | + Log.d("rn_executorch", "OpenCV not loaded") |
| 34 | + } else { |
| 35 | + Log.d("rn_executorch", "OpenCV loaded") |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + override fun loadModule( |
| 40 | + detectorLargeSource: String, |
| 41 | + detectorNarrowSource: String, |
| 42 | + recognizerSource: String, |
| 43 | + symbols: String, |
| 44 | + independentCharacters: Boolean, |
| 45 | + promise: Promise |
| 46 | + ) { |
| 47 | + try { |
| 48 | + this.independentCharacters = independentCharacters |
| 49 | + detectorLarge = VerticalDetector(false, reactApplicationContext) |
| 50 | + detectorLarge.loadModel(detectorLargeSource) |
| 51 | + detectorNarrow = VerticalDetector(true, reactApplicationContext) |
| 52 | + detectorNarrow.loadModel(detectorNarrowSource) |
| 53 | + recognizer = Recognizer(reactApplicationContext) |
| 54 | + recognizer.loadModel(recognizerSource) |
| 55 | + |
| 56 | + converter = CTCLabelConverter(symbols) |
| 57 | + |
| 58 | + promise.resolve(0) |
| 59 | + } catch (e: Exception) { |
| 60 | + promise.reject(e.message!!, ETError.InvalidModelSource.toString()) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + override fun forward(input: String, promise: Promise) { |
| 65 | + try { |
| 66 | + val inputImage = ImageProcessor.readImage(input) |
| 67 | + val result = detectorLarge.runModel(inputImage) |
| 68 | + val largeDetectorSize = detectorLarge.getModelImageSize() |
| 69 | + val resizedImage = ImageProcessor.resizeWithPadding( |
| 70 | + inputImage, |
| 71 | + largeDetectorSize.width.toInt(), |
| 72 | + largeDetectorSize.height.toInt() |
| 73 | + ) |
| 74 | + val predictions = Arguments.createArray() |
| 75 | + for (box in result) { |
| 76 | + val cords = box.bBox |
| 77 | + val boxWidth = cords[2].x - cords[0].x |
| 78 | + val boxHeight = cords[2].y - cords[0].y |
| 79 | + |
| 80 | + val boundingBox = RecognizerUtils.extractBoundingBox(cords) |
| 81 | + val croppedImage = Mat(resizedImage, boundingBox) |
| 82 | + |
| 83 | + val paddings = RecognizerUtils.calculateResizeRatioAndPaddings( |
| 84 | + inputImage.width(), |
| 85 | + inputImage.height(), |
| 86 | + largeDetectorSize.width.toInt(), |
| 87 | + largeDetectorSize.height.toInt() |
| 88 | + ) |
| 89 | + |
| 90 | + var text = "" |
| 91 | + var confidenceScore = 0.0 |
| 92 | + val boxResult = detectorNarrow.runModel(croppedImage) |
| 93 | + val narrowDetectorSize = detectorNarrow.getModelImageSize() |
| 94 | + |
| 95 | + val croppedCharacters = mutableListOf<Mat>() |
| 96 | + |
| 97 | + for (characterBox in boxResult) { |
| 98 | + val boxCords = characterBox.bBox |
| 99 | + val paddingsBox = RecognizerUtils.calculateResizeRatioAndPaddings( |
| 100 | + boxWidth.toInt(), |
| 101 | + boxHeight.toInt(), |
| 102 | + narrowDetectorSize.width.toInt(), |
| 103 | + narrowDetectorSize.height.toInt() |
| 104 | + ) |
| 105 | + |
| 106 | + var croppedCharacter = RecognizerUtils.cropImageWithBoundingBox( |
| 107 | + inputImage, |
| 108 | + boxCords, |
| 109 | + cords, |
| 110 | + paddingsBox, |
| 111 | + paddings |
| 112 | + ) |
| 113 | + |
| 114 | + if (this.independentCharacters) { |
| 115 | + croppedCharacter = RecognizerUtils.cropSingleCharacter(croppedCharacter) |
| 116 | + croppedCharacter = RecognizerUtils.normalizeForRecognizer(croppedCharacter, 0.0, true) |
| 117 | + val recognitionResult = recognizer.runModel(croppedCharacter) |
| 118 | + val predIndex = recognitionResult.first |
| 119 | + val decodedText = converter.decodeGreedy(predIndex, predIndex.size) |
| 120 | + text += decodedText[0] |
| 121 | + confidenceScore += recognitionResult.second |
| 122 | + } else { |
| 123 | + croppedCharacters.add(croppedCharacter) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if (this.independentCharacters) { |
| 128 | + confidenceScore /= boxResult.size |
| 129 | + } else { |
| 130 | + var mergedCharacters = Mat() |
| 131 | + Core.hconcat(croppedCharacters, mergedCharacters) |
| 132 | + mergedCharacters = ImageProcessor.resizeWithPadding( |
| 133 | + mergedCharacters, |
| 134 | + Constants.LARGE_MODEL_WIDTH, |
| 135 | + Constants.MODEL_HEIGHT |
| 136 | + ) |
| 137 | + mergedCharacters = RecognizerUtils.normalizeForRecognizer(mergedCharacters, 0.0) |
| 138 | + |
| 139 | + val recognitionResult = recognizer.runModel(mergedCharacters) |
| 140 | + val predIndex = recognitionResult.first |
| 141 | + val decodedText = converter.decodeGreedy(predIndex, predIndex.size) |
| 142 | + |
| 143 | + text = decodedText[0] |
| 144 | + confidenceScore = recognitionResult.second |
| 145 | + } |
| 146 | + |
| 147 | + for (bBox in box.bBox) { |
| 148 | + bBox.x = |
| 149 | + (bBox.x - paddings["left"] as Int) * paddings["resizeRatio"] as Float |
| 150 | + bBox.y = |
| 151 | + (bBox.y - paddings["top"] as Int) * paddings["resizeRatio"] as Float |
| 152 | + } |
| 153 | + |
| 154 | + val resMap = Arguments.createMap() |
| 155 | + |
| 156 | + resMap.putString("text", text) |
| 157 | + resMap.putArray("bbox", box.toWritableArray()) |
| 158 | + resMap.putDouble("confidence", confidenceScore) |
| 159 | + |
| 160 | + predictions.pushMap(resMap) |
| 161 | + } |
| 162 | + |
| 163 | + promise.resolve(predictions) |
| 164 | + } catch (e: Exception) { |
| 165 | + Log.d("rn_executorch", "Error running model: ${e.message}") |
| 166 | + promise.reject(e.message!!, e.message) |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + override fun getName(): String { |
| 171 | + return NAME |
| 172 | + } |
| 173 | +} |
0 commit comments