-
Notifications
You must be signed in to change notification settings - Fork 400
/
TextRecognitionActivity.kt
190 lines (173 loc) · 7.54 KB
/
TextRecognitionActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.google.firebase.example.mlkit.kotlin
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.ml.vision.FirebaseVision
import com.google.firebase.ml.vision.common.FirebaseVisionImage
import com.google.firebase.ml.vision.common.FirebaseVisionImageMetadata
import com.google.firebase.ml.vision.document.FirebaseVisionCloudDocumentRecognizerOptions
import com.google.firebase.ml.vision.document.FirebaseVisionDocumentText
import com.google.firebase.ml.vision.document.FirebaseVisionDocumentTextRecognizer
import com.google.firebase.ml.vision.text.FirebaseVisionCloudTextRecognizerOptions
import com.google.firebase.ml.vision.text.FirebaseVisionText
class TextRecognitionActivity : AppCompatActivity() {
private fun recognizeText(image: FirebaseVisionImage) {
// [START get_detector_default]
val detector = FirebaseVision.getInstance()
.onDeviceTextRecognizer
// [END get_detector_default]
// [START fml_run_detector]
val result = detector.processImage(image)
.addOnSuccessListener { firebaseVisionText ->
// Task completed successfully
// [START_EXCLUDE]
// [START get_text]
for (block in firebaseVisionText.textBlocks) {
val boundingBox = block.boundingBox
val cornerPoints = block.cornerPoints
val text = block.text
for (line in block.lines) {
// ...
for (element in line.elements) {
// ...
}
}
}
// [END get_text]
// [END_EXCLUDE]
}
.addOnFailureListener { e ->
// Task failed with an exception
// ...
}
// [END fml_run_detector]
}
private fun recognizeTextCloud(image: FirebaseVisionImage) {
// [START set_detector_options_cloud]
val options = FirebaseVisionCloudTextRecognizerOptions.Builder()
.setLanguageHints(listOf("en", "hi"))
.build()
// [END set_detector_options_cloud]
// [START get_detector_cloud]
val detector = FirebaseVision.getInstance().cloudTextRecognizer
// Or, to change the default settings:
// val detector = FirebaseVision.getInstance().getCloudTextRecognizer(options)
// [END get_detector_cloud]
// [START fml_run_detector_cloud]
val result = detector.processImage(image)
.addOnSuccessListener { firebaseVisionText ->
// Task completed successfully
// [START_EXCLUDE]
// [START get_text_cloud]
for (block in firebaseVisionText.textBlocks) {
val boundingBox = block.boundingBox
val cornerPoints = block.cornerPoints
val text = block.text
for (line in block.lines) {
// ...
for (element in line.elements) {
// ...
}
}
}
// [END get_text_cloud]
// [END_EXCLUDE]
}
.addOnFailureListener { e ->
// Task failed with an exception
// ...
}
// [END fml_run_detector_cloud]
}
private fun processTextBlock(result: FirebaseVisionText) {
// [START mlkit_process_text_block]
val resultText = result.text
for (block in result.textBlocks) {
val blockText = block.text
val blockConfidence = block.confidence
val blockLanguages = block.recognizedLanguages
val blockCornerPoints = block.cornerPoints
val blockFrame = block.boundingBox
for (line in block.lines) {
val lineText = line.text
val lineConfidence = line.confidence
val lineLanguages = line.recognizedLanguages
val lineCornerPoints = line.cornerPoints
val lineFrame = line.boundingBox
for (element in line.elements) {
val elementText = element.text
val elementConfidence = element.confidence
val elementLanguages = element.recognizedLanguages
val elementCornerPoints = element.cornerPoints
val elementFrame = element.boundingBox
}
}
}
// [END mlkit_process_text_block]
}
private fun getLocalDocumentRecognizer(): FirebaseVisionDocumentTextRecognizer {
// [START mlkit_local_doc_recognizer]
val detector = FirebaseVision.getInstance()
.cloudDocumentTextRecognizer
// [END mlkit_local_doc_recognizer]
return detector
}
private fun getCloudDocumentRecognizer(): FirebaseVisionDocumentTextRecognizer {
// [START mlkit_cloud_doc_recognizer]
// Or, to provide language hints to assist with language detection:
// See https://cloud.google.com/vision/docs/languages for supported languages
val options = FirebaseVisionCloudDocumentRecognizerOptions.Builder()
.setLanguageHints(listOf("en", "hi"))
.build()
val detector = FirebaseVision.getInstance()
.getCloudDocumentTextRecognizer(options)
// [END mlkit_cloud_doc_recognizer]
return detector
}
private fun processDocumentImage() {
// Dummy variables
val detector = getLocalDocumentRecognizer()
val myImage = FirebaseVisionImage.fromByteArray(
byteArrayOf(),
FirebaseVisionImageMetadata.Builder().build(),
)
// [START mlkit_process_doc_image]
detector.processImage(myImage)
.addOnSuccessListener { firebaseVisionDocumentText ->
// Task completed successfully
// ...
}
.addOnFailureListener { e ->
// Task failed with an exception
// ...
}
// [END mlkit_process_doc_image]
}
private fun processDocumentTextBlock(result: FirebaseVisionDocumentText) {
// [START mlkit_process_document_text_block]
val resultText = result.text
for (block in result.blocks) {
val blockText = block.text
val blockConfidence = block.confidence
val blockRecognizedLanguages = block.recognizedLanguages
val blockFrame = block.boundingBox
for (paragraph in block.paragraphs) {
val paragraphText = paragraph.text
val paragraphConfidence = paragraph.confidence
val paragraphRecognizedLanguages = paragraph.recognizedLanguages
val paragraphFrame = paragraph.boundingBox
for (word in paragraph.words) {
val wordText = word.text
val wordConfidence = word.confidence
val wordRecognizedLanguages = word.recognizedLanguages
val wordFrame = word.boundingBox
for (symbol in word.symbols) {
val symbolText = symbol.text
val symbolConfidence = symbol.confidence
val symbolRecognizedLanguages = symbol.recognizedLanguages
val symbolFrame = symbol.boundingBox
}
}
}
}
// [END mlkit_process_document_text_block]
}
}