Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from tesseract to google text-recognition #1926

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import org.jetbrains.kotlin.compose.compiler.gradle.ComposeFeatureFlag

plugins {
id("com.android.application")
id("kotlin-android")
Expand Down Expand Up @@ -106,7 +104,7 @@ dependencies {
implementation(libs.androidx.constraintlayout)

implementation(libs.opencv)
implementation(libs.tesseract4android)
implementation(libs.mlkit.text.recognition)

implementation(libs.lifecycle.viewmodel.ktx)
implementation(libs.lifecycle.viewmodel.savedstate)
Expand Down
Binary file removed app/src/main/assets/tessdata/eng.traineddata
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import io.github.fate_grand_automata.accessibility.AccessibilityGestures
import io.github.fate_grand_automata.imaging.TesseractOcrService
import io.github.fate_grand_automata.imaging.GoogleMLOcrService
import io.github.fate_grand_automata.scripts.FgoAutomataApi
import io.github.fate_grand_automata.scripts.FgoGameAreaManager
import io.github.fate_grand_automata.scripts.IFgoAutomataApi
Expand Down Expand Up @@ -50,5 +50,5 @@ abstract class ScriptsModule {

@ScriptScope
@Binds
abstract fun bindOcrService(ocrService: TesseractOcrService): OcrService
abstract fun bindOcrService(ocrService: GoogleMLOcrService): OcrService
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.fate_grand_automata.imaging

import android.content.Context
import com.google.android.gms.tasks.Tasks
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.text.TextRecognition
import com.google.mlkit.vision.text.latin.TextRecognizerOptions
import dagger.hilt.android.qualifiers.ApplicationContext
import io.github.lib_automata.OcrService
import io.github.lib_automata.Pattern
import io.github.lib_automata.dagger.ScriptScope
import javax.inject.Inject


@ScriptScope
class GoogleMLOcrService @Inject constructor(
@ApplicationContext val context: Context
) : OcrService {
private val recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)

override fun detectText(pattern: Pattern): String {
synchronized(recognizer) {
(pattern as DroidCvPattern).asBitmap().use { bmp ->
return InputImage.fromBitmap(bmp, 0).let { image ->
Tasks.await(recognizer.process(image).addOnFailureListener {
it.printStackTrace()
""
}).text
}
}
}
}
}

This file was deleted.

4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ opencv_version = "4.10.0"
navigation_compose_version = "2.8.4"

recyclerview_version = "1.3.2"
tesseract4android_version = "4.7.0"
text-recognition = "16.0.1"

timber_version = "5.0.1"

Expand Down Expand Up @@ -90,7 +90,7 @@ androidx-recyclerview = { module = "androidx.recyclerview:recyclerview", version
androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout_version" }

opencv = { group = "org.opencv", name = "opencv", version.ref = "opencv_version" }
tesseract4android = { module = "cz.adaptech.tesseract4android:tesseract4android", version.ref = "tesseract4android_version" }
mlkit-text-recognition = { module = "com.google.mlkit:text-recognition", version.ref = "text-recognition" }

junit-bom = { group = "org.junit", name = "junit-bom", version.ref = "junit_bom_version" }

Expand Down