我一直在关注这个关于iOS的Firebase MLKit文本识别(OCR)的链接,它似乎工作得很好,除了选择的照片(通过相机或图库)是在肖像中拍摄的时候。处理人像中的照片时,图像上未检测到任何内容。
我一直在从与物体相同的距离拍摄照片,并确保图像清晰且聚焦。
这是设备上MLKit文本识别的限制,还是我忽略了某些设置?
我需要操作图像并旋转它吗?这看起来很奇怪!
如有任何指示,我们将不胜感激。
发布于 2019-11-09 00:47:37
图像必须以“正确的方式向上”放入识别器。详情请参考the iOS quick start sample。
发布于 2019-11-09 00:51:53
所以这是由于方向的原因。这个解决方案是从其他网站获取的..
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;
}https://stackoverflow.com/questions/55908324
复制相似问题