Google Mobile Vision API 未在纵向模式下检测文本
Google Mobile Vision API not detecting text in portrait mode
我有一个 activity,它使用 Fotoapparat Library 获取图片并将其保存到文件中。接下来,我使用 Google Mobile Vision API 创建该文件的位图并检测文本。我使用了为此提供的标准代码。
TextRecognizer ocrFrame = new TextRecognizer.Builder(context).build();
Frame frame = new Frame.Builder().setBitmap(BitmapFactory.decodeFile(pathToPhoto)).build();
SparseArray<TextBlock> sparseTextBlocks = ocrFrame.detect(frame);
if (sparseTextBlocks.size() <= 0)
return null;
ArrayList<TextBlock> textBlocks = new ArrayList<>();
for (int i = 0; i < sparseTextBlocks.size(); i++) {
textBlocks.add(sparseTextBlocks.get(sparseTextBlocks.keyAt(i)));
}
OCR 在横向模式下完美运行,但在纵向模式下它几乎检测不到任何文本。我已经通过显示图像验证图像在纵向模式下没有反转。它给出了一个垂直图像。我真的不明白为什么会这样。有什么线索吗?
这是实现移动视觉的另一种选择API
// imageBitmap is the Bitmap image you're trying to process for text
if(imageBitmap != null) {
TextRecognizer textRecognizer = new TextRecognizer.Builder(this).build();
if(!textRecognizer.isOperational()) {
// Note: The first time that an app using a Vision API is installed on a
// device, GMS will download a native libraries to the device in order to do detection.
// Usually this completes before the app is run for the first time. But if that
// download has not yet completed, then the above call will not detect any text,
// barcodes, or faces.
// isOperational() can be used to check if the required native libraries are currently
// available. The detectors will automatically become operational once the library
// downloads complete on device.
Log.w(LOG_TAG, "Detector dependencies are not yet available.");
// Check for low storage. If there is low storage, the native library will not be
// downloaded, so detection will not become operational.
IntentFilter lowstorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
boolean hasLowStorage = registerReceiver(null, lowstorageFilter) != null;
if (hasLowStorage) {
Toast.makeText(this,"Low Storage", Toast.LENGTH_LONG).show();
Log.w(LOG_TAG, "Low Storage");
}
}
Frame imageFrame = new Frame.Builder()
.setBitmap(imageBitmap)
.build();
SparseArray<TextBlock> textBlocks = textRecognizer.detect(imageFrame);
for (int i = 0; i < textBlocks.size(); i++) {
TextBlock textBlock = textBlocks.get(textBlocks.keyAt(i));
Log.i(LOG_TAG, textBlock.getValue());
// Do something with value
}
}
您需要确保在模块的 build.gradle
中包含移动视觉依赖项
dependencies {
compile 'com.google.android.gms:play-services-vision:9.4.0'
}
并将其包含在应用程序的 Android 清单中
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="ocr" />
总的来说你的代码看起来不错,我认为这可能是你的库保存图片方向的方式可能与 Mobile Vision 冲突 API,尝试在一侧使用原生 android 捕获项目或其他库,如果您的应用程序仍然无法正常工作,请尝试将静止图像保存为横向,即使它们是纵向拍摄的,这也可能有所帮助
希望对您有所帮助
我有一个 activity,它使用 Fotoapparat Library 获取图片并将其保存到文件中。接下来,我使用 Google Mobile Vision API 创建该文件的位图并检测文本。我使用了为此提供的标准代码。
TextRecognizer ocrFrame = new TextRecognizer.Builder(context).build();
Frame frame = new Frame.Builder().setBitmap(BitmapFactory.decodeFile(pathToPhoto)).build();
SparseArray<TextBlock> sparseTextBlocks = ocrFrame.detect(frame);
if (sparseTextBlocks.size() <= 0)
return null;
ArrayList<TextBlock> textBlocks = new ArrayList<>();
for (int i = 0; i < sparseTextBlocks.size(); i++) {
textBlocks.add(sparseTextBlocks.get(sparseTextBlocks.keyAt(i)));
}
OCR 在横向模式下完美运行,但在纵向模式下它几乎检测不到任何文本。我已经通过显示图像验证图像在纵向模式下没有反转。它给出了一个垂直图像。我真的不明白为什么会这样。有什么线索吗?
这是实现移动视觉的另一种选择API
// imageBitmap is the Bitmap image you're trying to process for text
if(imageBitmap != null) {
TextRecognizer textRecognizer = new TextRecognizer.Builder(this).build();
if(!textRecognizer.isOperational()) {
// Note: The first time that an app using a Vision API is installed on a
// device, GMS will download a native libraries to the device in order to do detection.
// Usually this completes before the app is run for the first time. But if that
// download has not yet completed, then the above call will not detect any text,
// barcodes, or faces.
// isOperational() can be used to check if the required native libraries are currently
// available. The detectors will automatically become operational once the library
// downloads complete on device.
Log.w(LOG_TAG, "Detector dependencies are not yet available.");
// Check for low storage. If there is low storage, the native library will not be
// downloaded, so detection will not become operational.
IntentFilter lowstorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
boolean hasLowStorage = registerReceiver(null, lowstorageFilter) != null;
if (hasLowStorage) {
Toast.makeText(this,"Low Storage", Toast.LENGTH_LONG).show();
Log.w(LOG_TAG, "Low Storage");
}
}
Frame imageFrame = new Frame.Builder()
.setBitmap(imageBitmap)
.build();
SparseArray<TextBlock> textBlocks = textRecognizer.detect(imageFrame);
for (int i = 0; i < textBlocks.size(); i++) {
TextBlock textBlock = textBlocks.get(textBlocks.keyAt(i));
Log.i(LOG_TAG, textBlock.getValue());
// Do something with value
}
}
您需要确保在模块的 build.gradle
中包含移动视觉依赖项dependencies {
compile 'com.google.android.gms:play-services-vision:9.4.0'
}
并将其包含在应用程序的 Android 清单中
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="ocr" />
总的来说你的代码看起来不错,我认为这可能是你的库保存图片方向的方式可能与 Mobile Vision 冲突 API,尝试在一侧使用原生 android 捕获项目或其他库,如果您的应用程序仍然无法正常工作,请尝试将静止图像保存为横向,即使它们是纵向拍摄的,这也可能有所帮助
希望对您有所帮助