Last active
May 5, 2019 02:39
-
-
Save ApolloZhu/9adca213338acb9d78f5b53920a2ebb0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun runTextRecognition(bitmap: Bitmap) { | |
val image = FirebaseVisionImage.fromBitmap(bitmap) | |
// FirebaseVisionTextRecognizer will be used | |
val detector = FirebaseVision.getInstance().visionTextDetector | |
detector.detectInImage(image) | |
.addOnSuccessListener { | |
texts -> | |
val blocks = texts.blocks | |
if (blocks.size == 0) { /*nothing found*/ return } | |
blocks.forEach { | |
block -> // a bunch of text | |
block.lines.forEach { | |
line -> // a line of text | |
line.elements.forEach { | |
element -> // each token | |
element.text // OCR result | |
element.boundingBox // where it is in image | |
} | |
} | |
} | |
} | |
.addOnFailureListener { | |
e -> e.printStackTrace() | |
} | |
} | |
/* | |
If using cloud detector instead, processing would be similar, | |
because we are using FirebaseVisionDocumentTextRecognizer, | |
structure of the returned result would be: | |
pages -> blocks -> words -> symbols | |
So we'll need to join symbols together back to a word: | |
*/ | |
fun toString(word: FirebaseVisionCloudText.Word): String | |
= word.symbols.joinToString("") { it.text } | |
/* | |
Notice: @ symbol will be a separate word, that's why the author joined | |
2 words together to be a single token for the regex matching function. | |
That's unnecessary for other use cases, so only zipWithNext if needed. | |
Why cloud? It should have higher accuracy, but cost you money, so nah. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
最终效果:
MLKit 的组成: