【问题标题】:How to extract text form a specific block of the image? [ML-Kit]如何从图像的特定块中提取文本? [机器学习套件]
【发布时间】:2020-04-16 14:47:54
【问题描述】:

我很绝望。我有 16 个文本视图。目标是为处方拍照。我想要的是为我拥有的每个 Textview 提取正确的文本。这是我的最后一个问题。

这是拍摄照片、创建文件、识别文本并将其放入 TextView 的代码。

private fun dispatchTakePictureIntent() {
    Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
        // Ensure that there's a camera activity to handle the intent
        takePictureIntent.resolveActivity(packageManager)?.also {
            // Create the File where the photo should go

            val photoFile: File? = try {
                createImageFile()
            } catch (ex: IOException) {
                // Error occurred while creating the File
                null
            }
            // Continue only if the File was successfully created
            photoFile?.also {
                photoURI = FileProvider.getUriForFile(
                    this,
                    "com.example.android.fileprovider2",
                    it
                )
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO)
                /*if (REQUEST_TAKE_PHOTO == 1)
                {
                    return imageView.setImageURI(photoURI)
                }*/

            }
        }
    }
}



@Throws(IOException::class)
private fun createImageFile(): File {
    // Create an image file name
    val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
    val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
    return File.createTempFile(
        "JPEG_${timeStamp}_", /* prefix */
        ".jpg", /* suffix */
        storageDir /* directory */
    ).apply {
        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = absolutePath

    }
}











fun startRecognizing(v: View) {
    if (imageView.drawable != null) {
        editText.setText("")
        v.isEnabled = false
        val bitmap = (imageView.drawable as BitmapDrawable).bitmap
        val image = FirebaseVisionImage.fromBitmap(bitmap)
        val detector = FirebaseVision.getInstance().onDeviceTextRecognizer


        detector.processImage(image)
            .addOnSuccessListener { firebaseVisionText ->
                v.isEnabled = true
                processResultText(firebaseVisionText)
            }
            .addOnFailureListener {
                v.isEnabled = true
                editText.setText("Failed")
            }
    } else {
        Toast.makeText(this, "Select an Image First", Toast.LENGTH_LONG).show()
    }

}

private fun String.toEditable(): Editable =  Editable.Factory.getInstance().newEditable(this)


private fun processResultText(resultText: FirebaseVisionText) {
    if (resultText.textBlocks.size == 0) {
        editText.setText("No Text Found")
        return
    }
    for (blocks in resultText.textBlocks) {
        val blockText = blocks.text
        val stringText  = resultText.text
        val stringTextSplit = stringText.lines()
        krankenkasse.text = stringTextSplit[1].toEditable()
        et2.text = stringTextSplit[4].toEditable()
        et3.text = stringTextSplit[5].toEditable()
        et4.text = stringTextSplit[6].toEditable()
        et5.text = stringTextSplit[7].toEditable()
        et6.text = stringTextSplit[8].toEditable()
        et7.text = stringTextSplit[9].toEditable()
        et8.text = stringTextSplit[11].toEditable()
        editText.append(blockText + "\n")
    }
}

如果您有任何问题。请问一下。

【问题讨论】:

  • 您拥有 16 个 TextView 与问题无关。请描述您遇到的确切问题,包括代码的 sn-ps,重点描述您尝试过的内容以及为什么它不起作用。
  • 我做到了。我不想检测和提取整张图片,我想提取图片特定区域的文字。我正在使用 Intent。也许使用 Camera2 或 cameraX 会更好

标签: android kotlin android-camera firebase-mlkit


【解决方案1】:

如果您想检测图像中的某些特定区域,您可以根据区域裁剪图像,并将裁剪后的图像发送到 ML Kit。

另一种选择是检测整个图像,但过滤掉不在区域中的文本。每个检测到的文本都有自己的边界框坐标。

https://firebase.google.com/docs/reference/android/com/google/firebase/ml/vision/text/FirebaseVisionText.TextBlock#getBoundingBox()

【讨论】:

    猜你喜欢
    • 2021-05-12
    • 2023-03-09
    • 2020-07-23
    • 2019-06-03
    • 2020-11-11
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多