首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >纵向MLKit文本识别

纵向MLKit文本识别
EN

Stack Overflow用户
提问于 2019-04-30 01:36:10
回答 2查看 539关注 0票数 0

我一直在关注这个关于iOS的Firebase MLKit文本识别(OCR)的链接,它似乎工作得很好,除了选择的照片(通过相机或图库)是在肖像中拍摄的时候。处理人像中的照片时,图像上未检测到任何内容。

我一直在从与物体相同的距离拍摄照片,并确保图像清晰且聚焦。

Firebase MLKit Tutorial

这是设备上MLKit文本识别的限制,还是我忽略了某些设置?

我需要操作图像并旋转它吗?这看起来很奇怪!

如有任何指示,我们将不胜感激。

EN

回答 2

Stack Overflow用户

发布于 2019-11-09 00:47:37

图像必须以“正确的方式向上”放入识别器。详情请参考the iOS quick start sample

票数 0
EN

Stack Overflow用户

发布于 2019-11-09 00:51:53

所以这是由于方向的原因。这个解决方案是从其他网站获取的..

代码语言:javascript
复制
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        if (imageUri == null) {
            Log.d(TAG, "imageUri is null!")
            return
        }
        Log.d(TAG, "$imageUri")
        val bmp = MediaStore.Images.Media.getBitmap(contentResolver, imageUri)

        //we need to rotate the image as cameras can be embedded in the hardware in different orientations.  Make the bitmap upright before we attempt to process it..
        val rotatedImg = rotateImageIfRequired(this, bmp, imageUri!!)

        processImage(rotatedImg) //parse this into the ML vision
    } else {
        Log.d(TAG, "onActivityResult -> resultCode: $resultCode")
    }
}

private fun rotateImageIfRequired(context: Context, img: Bitmap, selectedImage: Uri): Bitmap {
    Log.d(TAG, "rotateImageIfRequired")
    val input = context.getContentResolver().openInputStream(selectedImage);
    var ei: ExifInterface? = null
    if (Build.VERSION.SDK_INT > 23)
        ei = ExifInterface(input);
    else
        ei = ExifInterface(selectedImage.getPath());

    val orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

    when (orientation) {
        ExifInterface.ORIENTATION_ROTATE_90 -> return rotateImage(img, 90f);
        ExifInterface.ORIENTATION_ROTATE_180 -> return rotateImage(img, 180f);
        ExifInterface.ORIENTATION_ROTATE_270 -> return rotateImage(img, 270f);
        else -> return img;
    }
}

private fun rotateImage(img: Bitmap, degree: Float): Bitmap {
    Log.d(TAG, "rotateImage() degree: $degree")
    var matrix = Matrix();
    matrix.postRotate(degree);
    val rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
    img.recycle();
    return rotatedImg;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55908324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档