gpt4 book ai didi

android - 如何处理firebase ML文本识别返回的文本

转载 作者:行者123 更新时间:2023-12-04 23:57:18 25 4
gpt4 key购买 nike

我正在使用 firebase ML vision 从图像中读取 RC 细节。

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);
}
}

它工作正常,文本返回成功。

enter image description here

数据列表通过 cornerPointsboundingBox 返回, map 数据如何正确..前注册。有实际值(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

最佳答案

好吧,如果你想要 reg.no。你可以采用多种方式:

  1. 通过正则表达式从所有文本中过滤掉
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)
}
}
  1. 从图片区域过滤掉(可能然后使用正则表达式)
// 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
}
  1. 这两者的结合。您可能可以制作更精细的算法,将位置和文本匹配结合起来。所以像
// 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
}
}

关于android - 如何处理firebase ML文本识别返回的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70617980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com