Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

refactor: replace LLM-based OCR with on-device ML Kit text recognition for improved performance and privacy

Rishabh Bajpaie39fabbef6ae
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index ca181af..6c09e6d 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -138,6 +138,9 @@ dependencies {
exclude(group = "com.atlassian.commonmark", module = "commonmark")
}
+ // ML Kit on-device text recognition (for Lens mode OCR)
+ implementation("com.google.mlkit:text-recognition:16.0.1")
+
// CameraX for automated photo capture
val cameraxVersion = "1.4.0"
implementation("androidx.camera:camera-core:$cameraxVersion")
diff --git a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt b/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
index 0192373..1b5f961 100644
--- a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
+++ b/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
@@ -297,8 +297,8 @@ class AssistiveBallService : Service() {
onImagePrompt = { base64, prompt ->
openCompanionWithImageAndQuery(base64, prompt)
},
- onOcrToClipboard = { base64 ->
- ocrCropToClipboard(base64)
+ onOcrToClipboard = { bitmap ->
+ ocrCropToClipboard(bitmap)
},
onError = { overlay.showError(it) }
)
@@ -440,29 +440,24 @@ class AssistiveBallService : Service() {
}
}
- /** OCR a Lens crop via the LLM and copy the recognized text to the clipboard. */
- private fun ocrCropToClipboard(base64Jpeg: String) {
+ /** OCR a Lens crop via ML Kit on-device text recognition and copy to clipboard. */
+ private fun ocrCropToClipboard(bitmap: android.graphics.Bitmap) {
overlay.showError("Reading text…")
- val history = listOf(
- com.gotcha.llm.ChatMessage(
- "system",
- kotlinx.serialization.json.JsonPrimitive(
- "You are an OCR engine. Return ONLY the exact text visible in the image, " +
- "with no commentary, labels, or quotes. If there is no text, return an empty string."
- )
- ),
- com.gotcha.llm.visionUserMessage("Extract all text from this image.", base64Jpeg, "jpeg")
- )
scope.launch {
try {
- val response = llmClient.chat(history)
- val text = response.choices.firstOrNull()?.message?.textContent?.trim().orEmpty()
+ val recognizer = com.google.mlkit.vision.text.TextRecognition.getClient(
+ com.google.mlkit.vision.text.latin.TextRecognizerOptions.DEFAULT_OPTIONS
+ )
+ val image = com.google.mlkit.vision.common.InputImage.fromBitmap(bitmap, 0)
+ val result = withContext(Dispatchers.Default) {
+ com.google.android.gms.tasks.Tasks.await(recognizer.process(image))
+ }
+ val text = result.text.trim()
if (text.isBlank()) {
overlay.showError("No text found in selection")
} else {
val clipboard = getSystemService(CLIPBOARD_SERVICE) as android.content.ClipboardManager
clipboard.setPrimaryClip(android.content.ClipData.newPlainText("Lens text", text))
- overlay.showError("Text copied")
}
} catch (e: Exception) {
overlay.showError("Couldn't read text: ${e.message}")
diff --git a/app/src/main/java/com/gotcha/service/ScreenLensController.kt b/app/src/main/java/com/gotcha/service/ScreenLensController.kt
index 751c04b..94a6851 100644
--- a/app/src/main/java/com/gotcha/service/ScreenLensController.kt
+++ b/app/src/main/java/com/gotcha/service/ScreenLensController.kt
@@ -53,7 +53,7 @@ class ScreenLensController(
/** Open the companion panel with the crop attached AND auto-send [prompt] (OCR/translate). */
private val onImagePrompt: (base64Jpeg: String, prompt: String) -> Unit,
/** OCR the crop and copy the recognized text to the clipboard (async). */
- private val onOcrToClipboard: (base64Jpeg: String) -> Unit,
+ private val onOcrToClipboard: (crop: Bitmap) -> Unit,
private val onError: (String) -> Unit
) {
private val appContext = context.applicationContext
@@ -248,9 +248,9 @@ class ScreenLensController(
finishMenu(recycle = true)
} else {
// No accessibility text — OCR the crop, then copy the result.
- val base64 = pendingCrop?.let { encodeJpeg(it) }
+ val crop = pendingCrop?.let { it.copy(it.config, false) }
finishMenu(recycle = true)
- if (base64 != null) onOcrToClipboard(base64) else onError("Couldn't read selection")
+ if (crop != null) onOcrToClipboard(crop) else onError("Couldn't read selection")
}
}
)