Google Mobile Vision Text API示例 [英] Google Mobile Vision Text API Example

查看:227
本文介绍了Google Mobile Vision Text API示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently writing code that should be able to view a picture of text and then extract the text from the picture for android based devices. I did some research online and found that Google provides their own API called "Mobile Vision" (a package with many items i.e. text recognition, facial recognition, etc). However, in their demos they only demonstrate live text recognition. I was wondering if anyone could give me an example of text recognition on a still image using the Mobile Vision API. Any help is welcome. Thanks.

推荐答案

Google Play服务移动视觉API文档介绍了如何执行此操作,您可以使用 TextRecognizer 类以检测框架。获得位图图像后,您可以将其转换为帧并对其进行处理。

The Google Play Services Mobile Vision API Documentation describes how to do this, you can use the TextRecognizer class to detect text in Frames. Once you have the Bitmap image you can then convert it into a frame and do processing on it. See below for an example.

// 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中包含移动视觉依赖项

You also need to ensure you include the mobile vision dependency in the module's build.gradle

dependencies {
    compile 'com.google.android.gms:play-services-vision:9.4.0'
} 

并在应用的Android清单中添加以下内容

And also include the following in the app's Android Manifest

<meta-data
    android:name="com.google.android.gms.vision.DEPENDENCIES"
    android:value="ocr" />

这篇关于Google Mobile Vision Text API示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆