【问题标题】:ML Kit OCR in Fotoapparat returns nonsenseFotoapparat 中的 ML Kit OCR 返回废话
【发布时间】:2019-03-01 22:29:21
【问题描述】:

我正在尝试进行自定义帧处理,以创建 ML-Kit OCR 应用。我首先使用 FotoApparat 创建了一个简单的相机应用程序。

然后我在 FotoApparat 的 m 初始化中添加了一个自定义帧处理匿名函数。

   private fun createFotoapparat(){
        val cameraView = findViewById<CameraView>(R.id.camera_view)
        fotoapparat = Fotoapparat
            .with(this)
            .into(cameraView)
            .previewScaleType(ScaleType.CenterCrop)
            .lensPosition(back())
            .logger(loggers(logcat()))
            .cameraErrorCallback({error -> println("Recorder errors: $error")})
            .frameProcessor { frame ->
                Log.d("Frameprocessor", "Fired")
                val rotation = getRotationCompensation("0", this, baseContext)
                val BAimage = frame.image
                val metadata = FirebaseVisionImageMetadata.Builder()
                    .setWidth(480)   // 480x360 is typically sufficient for
                    .setHeight(360)  // image recognition
                    .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
                    .setRotation(rotation)
                    .build()
                var FBimage = FirebaseVisionImage.fromByteArray(BAimage, metadata)
                val detector = FirebaseVision.getInstance()
                    .onDeviceTextRecognizer
                val result = detector.processImage(FBimage)
                    .addOnSuccessListener { firebaseVisionText ->
                        Log.d("OnSuccess", "Triggered")
                        for (block in firebaseVisionText.textBlocks){
                            val blockText = block.text
                            val blockConfidence = block.confidence
                            Log.d("newframe", blockText)
                            Log.d(blockText, blockConfidence.toString())
                        }
                    }
                    .addOnFailureListener {
                        Log.e("err", "line 114", it)
                    }
            }.build()
    }

我的问题是它返回的是废话,置信度为空值。这是一些 logcat 输出,当它查看带有少量输入文本的简单图像时。

2019-03-01 14:24:56.735 16117-16117/me.paxana.myapplication D/newframe: 111
2019-03-01 14:24:56.735 16117-16117/me.paxana.myapplication D/111: null

我可以根据需要发布更多代码或更多 logcat,但我觉得我在这里遗漏了一些重要的东西。

【问题讨论】:

  • 当我切换到 cloudTextRecognizer 时,我得到了准确的结果,但仍然没有信心

标签: android image-processing android-camera ocr firebase-mlkit


【解决方案1】:

我部分想通了。我的旋转算法是错误的,我必须以 90 度角拍摄照片,然后才能完美运行。这是我的旋转算法,我会在它工作时更新。

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Throws(CameraAccessException::class)
    private fun getRotationCompensation(cameraId: String, activity: Activity, context: Context): Int {
        // Get the device's current rotation relative to its "native" orientation.
        // Then, from the ORIENTATIONS table, look up the angle the image must be
        // rotated to compensate for the device's rotation.
        val deviceRotation = activity.windowManager.defaultDisplay.rotation
        var rotationCompensation = ORIENTATIONS.get(deviceRotation)

        // On most devices, the sensor orientation is 90 degrees, but for some
        // devices it is 270 degrees. For devices with a sensor orientation of
        // 270, rotate the image an additional 180 ((270 + 270) % 360) degrees.
        val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
        val sensorOrientation = cameraManager
            .getCameraCharacteristics(cameraId)
            .get(CameraCharacteristics.SENSOR_ORIENTATION)!!
        rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360

        // Return the corresponding FirebaseVisionImageMetadata rotation value.
        val result: Int
        when (rotationCompensation) {
            0 -> result = FirebaseVisionImageMetadata.ROTATION_0
            90 -> result = FirebaseVisionImageMetadata.ROTATION_90
            180 -> result = FirebaseVisionImageMetadata.ROTATION_180
            270 -> result = FirebaseVisionImageMetadata.ROTATION_270
            else -> {
                result = FirebaseVisionImageMetadata.ROTATION_0
                Log.e("Err", "Bad rotation value: $rotationCompensation")
            }
        }
        return result
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-28
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多