【问题标题】:Google Mobile Vision Text API Example谷歌移动视觉文本 API 示例
【发布时间】:2016-11-10 07:06:28
【问题描述】:

我目前正在编写代码,该代码应该能够查看文本图片,然后从基于 Android 的设备的图片中提取文本。我在网上做了一些研究,发现谷歌提供了他们自己的 API,称为“Mobile Vision”(一个包含许多项目的包,即文本识别、面部识别等)。然而,在他们的演示中,他们只演示了实时文本识别。我想知道是否有人可以给我一个使用 Mobile Vision API 对静止图像进行文本识别的示例。欢迎任何帮助。谢谢。

【问题讨论】:

    标签: android android-vision text-recognition


    【解决方案1】:

    Google Play Services Mobile Vision API 文档描述了如何执行此操作,您可以使用 TextRecognizer 类来检测 Frames 中的文本。获得位图图像后,您可以将其转换为帧并对其进行处理。请参阅下面的示例。

    // 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" />
    

    【讨论】:

    • 嗨,通过使用上面的代码,它没有检测到所有的行。我需要从“textBlocks”对象中获取所有行,还需要从“textBlock”中获取所有值。请尽快帮助我。在此先感谢...
    • 要从文本块中获取行,只需:List&lt;? extends Text&gt; textComponents = mText.getComponents(); for(Text currentText : textComponents) { // Do your thing here }
    • @我们可以限制只使用 vision api 阅读英文文本吗?
    • @AndroidDeveloperWorld 不,它可以识别超过 18 种不同的语言。在此处查看支持哪些语言:developers.google.com/vision/text-overview
    • @w3bshark 在我的场景中,不应在 Aadhar 卡上阅读泰卢固语文本。它应该只读英文。
    猜你喜欢
    • 1970-01-01
    • 2017-02-04
    • 2019-11-24
    • 2017-06-30
    • 2017-06-14
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 2017-02-23
    相关资源
    最近更新 更多