how to process the text returned by firebase ML text recognition
I am using the firebase ML vision to read RC details from the image.
implementation "com.google.android.gms:play-services-mlkit-text-recognition:17.0.1"
private fun Activity.recognizeText(imageSting: String) {
var image: InputImage? = null
try {
image = InputImage.fromFilePath(getContext(), Uri.fromFile(File(imageSting)))
} catch (e: IOException) {
e.printStackTrace()
}
// [START get_detector_default]
val recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)
// [END get_detector_default]
// [START run_detector]
val result = recognizer.process(image)
.addOnSuccessListener { visionText ->
processTextBlock(visionText)
}
.addOnFailureListener { e ->
Log.d("test", e.localizedMessage);
}
}
it works fine and text is returned successfully.
list of data is returened with cornerPoints and boundingBox how map data correclty .. ex- Reg. No. with actual value
Government of 1Tamil Nadu
Certificate of Registration
O
Reg. No.
Date of Reg.
TNO6P7094
30-06-2015
Reg. Valid Till
29-06-2030
Chassis No.
ME4JF504FFT459059
Engine No.
JF50ET 2460128
Owner
02
Sr. No.
Owner Name
NIKIL KUMAR ROY
Fuel Used
PETROL
Son/Daughter/Wife of
MANIK ROY
Address
NO 456/HPTRAILWAY
COLONY 4TH STREET
AYANAVARAM Chennai TN 600023
Solution 1:
Well if you want reg.no. you could go several ways:
- Filter out from all that text by regex
val regex = Regex("[A-Z]{3}[0-9]{1}[A-Z]{1}[0-9]{4}")
text.textBlocks.map {
it.lines.filter {
regex.matches(it.text)
}
}
- Filter out from area of picture (probably then with regex additionally)
// this is camera preview
previewCamera.getGlobalVisibleRect(previewCameraRect)
// here you would calculate the position on the picture, where you would expect needed thing. Do not forget to calculate on change of rotation
Rect(left.roundToInt(), top.roundToInt(), right.roundToInt(), bottom.roundToInt())
text.textBlocks.filter { textBlock ->
textBlock.boundingBox?.let { rect.contains(it) } ?: false
}
- Combination of those two. You probably can make more elaborate algorithm that would combine position and text matching. So something like
// Pseudocode
foreach (textBlocks as textBlock) {
if (textBlock.text.equals("Reg.No")) {
position = textBlock.boundingBox
continue
}
if (isNear(textBlock.boundingBox, position) && regex.matches(textBlock.text)) {
// this is part of text you are looking for
}
}