|
| 1 | +package com.swmansion.rnexecutorch.models.ocr |
| 2 | + |
| 3 | +import com.facebook.react.bridge.Arguments |
| 4 | +import com.facebook.react.bridge.ReactApplicationContext |
| 5 | +import com.facebook.react.bridge.WritableArray |
| 6 | +import com.swmansion.rnexecutorch.models.ocr.utils.CTCLabelConverter |
| 7 | +import com.swmansion.rnexecutorch.models.ocr.utils.Constants |
| 8 | +import com.swmansion.rnexecutorch.models.ocr.utils.OCRbBox |
| 9 | +import com.swmansion.rnexecutorch.models.ocr.utils.RecognizerUtils |
| 10 | +import com.swmansion.rnexecutorch.utils.ImageProcessor |
| 11 | +import org.opencv.core.Core |
| 12 | +import org.opencv.core.Mat |
| 13 | + |
| 14 | +class RecognitionHandler( |
| 15 | + symbols: String, |
| 16 | + languageDictPath: String, |
| 17 | + reactApplicationContext: ReactApplicationContext |
| 18 | +) { |
| 19 | + private val recognizerLarge = Recognizer(reactApplicationContext) |
| 20 | + private val recognizerMedium = Recognizer(reactApplicationContext) |
| 21 | + private val recognizerSmall = Recognizer(reactApplicationContext) |
| 22 | + private val converter = CTCLabelConverter(symbols, mapOf(languageDictPath to "key")) |
| 23 | + |
| 24 | + private fun runModel(croppedImage: Mat): Pair<List<Int>, Double> { |
| 25 | + val result: Pair<List<Int>, Double> = if (croppedImage.cols() >= Constants.LARGE_MODEL_WIDTH) { |
| 26 | + recognizerLarge.runModel(croppedImage) |
| 27 | + } else if (croppedImage.cols() >= Constants.MEDIUM_MODEL_WIDTH) { |
| 28 | + recognizerMedium.runModel(croppedImage) |
| 29 | + } else { |
| 30 | + recognizerSmall.runModel(croppedImage) |
| 31 | + } |
| 32 | + |
| 33 | + return result |
| 34 | + } |
| 35 | + |
| 36 | + fun loadRecognizers( |
| 37 | + largeRecognizerPath: String, |
| 38 | + mediumRecognizerPath: String, |
| 39 | + smallRecognizerPath: String, |
| 40 | + onComplete: (Int, Exception?) -> Unit |
| 41 | + ) { |
| 42 | + try { |
| 43 | + recognizerLarge.loadModel(largeRecognizerPath) |
| 44 | + recognizerMedium.loadModel(mediumRecognizerPath) |
| 45 | + recognizerSmall.loadModel(smallRecognizerPath) |
| 46 | + onComplete(0, null) |
| 47 | + } catch (e: Exception) { |
| 48 | + onComplete(1, e) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + fun recognize( |
| 53 | + bBoxesList: List<OCRbBox>, |
| 54 | + imgGray: Mat, |
| 55 | + desiredWidth: Int, |
| 56 | + desiredHeight: Int |
| 57 | + ): WritableArray { |
| 58 | + val res: WritableArray = Arguments.createArray() |
| 59 | + val ratioAndPadding = RecognizerUtils.calculateResizeRatioAndPaddings( |
| 60 | + imgGray.width(), |
| 61 | + imgGray.height(), |
| 62 | + desiredWidth, |
| 63 | + desiredHeight |
| 64 | + ) |
| 65 | + |
| 66 | + val left = ratioAndPadding["left"] as Int |
| 67 | + val top = ratioAndPadding["top"] as Int |
| 68 | + val resizeRatio = ratioAndPadding["resizeRatio"] as Float |
| 69 | + val resizedImg = ImageProcessor.resizeWithPadding( |
| 70 | + imgGray, |
| 71 | + desiredWidth, |
| 72 | + desiredHeight |
| 73 | + ) |
| 74 | + |
| 75 | + for (box in bBoxesList) { |
| 76 | + var croppedImage = RecognizerUtils.getCroppedImage(box, resizedImg, Constants.MODEL_HEIGHT) |
| 77 | + if (croppedImage.empty()) { |
| 78 | + continue |
| 79 | + } |
| 80 | + |
| 81 | + croppedImage = RecognizerUtils.normalizeForRecognizer(croppedImage, Constants.ADJUST_CONTRAST) |
| 82 | + |
| 83 | + var result = runModel(croppedImage) |
| 84 | + var confidenceScore = result.second |
| 85 | + |
| 86 | + if (confidenceScore < Constants.LOW_CONFIDENCE_THRESHOLD) { |
| 87 | + Core.rotate(croppedImage, croppedImage, Core.ROTATE_180) |
| 88 | + val rotatedResult = runModel(croppedImage) |
| 89 | + val rotatedConfidenceScore = rotatedResult.second |
| 90 | + if (rotatedConfidenceScore > confidenceScore) { |
| 91 | + result = rotatedResult |
| 92 | + confidenceScore = rotatedConfidenceScore |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + val predIndex = result.first |
| 97 | + val decodedTexts = converter.decodeGreedy(predIndex, predIndex.size) |
| 98 | + |
| 99 | + for (bBox in box.bBox) { |
| 100 | + bBox.x = (bBox.x - left) * resizeRatio |
| 101 | + bBox.y = (bBox.y - top) * resizeRatio |
| 102 | + } |
| 103 | + |
| 104 | + val resMap = Arguments.createMap() |
| 105 | + |
| 106 | + resMap.putString("text", decodedTexts[0]) |
| 107 | + resMap.putArray("bbox", box.toWritableArray()) |
| 108 | + resMap.putDouble("confidence", confidenceScore) |
| 109 | + |
| 110 | + res.pushMap(resMap) |
| 111 | + } |
| 112 | + |
| 113 | + return res |
| 114 | + } |
| 115 | +} |
0 commit comments