【问题标题】:Android - google cloud vision with OCR: Display of detected textAndroid - 带有 OCR 的谷歌云视觉:显示检测到的文本
【发布时间】:2018-03-10 10:23:13
【问题描述】:

我正在使用 Google 云视觉 OCR 来检测文本。显示的文本始终是 1. 检测到的文本,2. 每个检测到的单词。我只想显示检测到的文本。

我正在使用来自Google Cloud Platform Github 的代码,我在callCloudVision 方法中将类型设置为文本检测labelDetection.setType("TEXT_DETECTION");

我还将convertResponseToString方法修改为:

private String convertResponseToString(BatchAnnotateImagesResponse response) {
        String message = "";

        List<EntityAnnotation> labels = response.getResponses().get(0).getTextAnnotations();
        for (EntityAnnotation label : labels) {
            if (labels != null) {
                System.out.println(label.getDescription());
                message += String.format(Locale.US, "%s", label.getDescription()) + "\n";
            }
            else
            {
                message += "nothing";
            }
        }
        return message;
    }

这是我的毕业典礼:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    defaultConfig {
        applicationId "com.example.mhci"
        minSdkVersion 24
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath false
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/io.netty.versions.properties'
        exclude 'META-INF/INDEX.LIST'
        exclude 'META-INF/DEPENDENCIES'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'

    androidTestCompile('com.android.support.test.espresso:espresso-core:3.0.1', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.android.support:appcompat-v7:25.1.1'
    compile 'com.android.support:design:25.4.0'

    compile 'com.google.api-client:google-api-client-android:1.20.0' exclude module: 'httpclient'
    compile 'com.google.http-client:google-http-client-gson:1.20.0' exclude module: 'httpclient'

    compile 'com.google.apis:google-api-services-vision:v1-rev2-1.21.0'

    compile 'com.android.support:design:25.4.0'

    compile ('com.google.apis:google-api-services-translate:v2-rev47-1.22.0') {
        exclude group: 'com.google.guava'
    }

    compile ('com.google.cloud:google-cloud-translate:0.5.0') {
        exclude group: 'io.grpc', module: 'grpc-all'
        exclude group: 'com.google.protobuf', module: 'protobuf-java'
        exclude group: 'com.google.api-client', module: 'google-api-client-appengine'
    }
}

检测到的this image显示的文字是:

Hello world

Hello
world

但我希望它只显示Hello world

我该怎么做?

【问题讨论】:

标签: java android ocr google-cloud-vision


【解决方案1】:

即使在这个用例中,文本检测也应该可以工作。问题是所有标签都返回了所有内容,这就是它的设计方式。您可以看到,如果您在一张图片中并排显示两个街道标志(“Main Street”和“Park Avenue”),您会希望 API 将所看到的内容分解成多个部分,这样更有意义。如果它只返回一串“Main Street Park Avenue”,则该信息没有用处。这就是为什么它总是返回整个事物,然后是它的所有部分,所以如果你通过返回的字符串进行查询,你会找到相关的图片。

因此,基本上,如果您相信它可以正确完整地读取标签,您可以简单地使用返回数组中的第一个结果而不是全部 3 个。或者您可以实现一些仅显示最长和最受信任结果的逻辑.

所以基本上,操作返回的列表,列表标签,并提取你想要的结果。在您的特定情况下,不要显示整个列表,只需取列表中的第一个值,您就会得到您想要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    • 2019-10-07
    • 2017-02-04
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    相关资源
    最近更新 更多