読者です 読者をやめる 読者になる 読者になる

めもめも

このブログに記載の内容は個人の見解であり、必ずしも所属組織の立場、戦略、意見を代表するものではありません。

Using Cloud Vision API and Cloud Translation API from Google App Engine

qiita.com

This is a Day 7 entry for TensorFlow Advent Calendar 2016 (in Japanese). It's organized by Japan TensorFlow Users Group (TFUG) :)

Recently I had a chance to create a prototype of a photo album application emulating the auto labeling feature of Google Photo. It's running on Google App Engine and uses Cloud Vision API and Cloud Translation API in the background. Cloud Vision API is used to detect appropriate labels for the uploaded photos and Cloud Translation API is used to translate labels in English to some other languages. The target language is specified by the administrator as a deployment option.

Here's a screenshot of the prototype running in the Italian mode. You can see that the labels (tags) are described in Italian.

The full prototype codes is now available on GitHub. I hope you're gonna love it!

github.com

In this note, I will show how you can use those APIs from Python codes running on the GAE standard environment. The good thing is that you don't have to deal with API keys and credentials since API authentication is automatically done on GAE.

First, since the example uses the google-cloud client library, add the following line in requirements.txt.

requirements.txt

google-cloud==0.21.0

Now here's the code snippet for the label detection and the label translation.

from google.cloud import vision, translate

def get_labels(bucket, filename, num=3):
    vision_client = vision.Client()
    image = vision_client.image(
                source_uri = 'gs://%s/%s' % (bucket, filename))
    return image.detect_labels(limit=num)

def translate_text(text, target='en'):
    if target == 'en':
        return text
    translate_client = translate.Client()
    result = translate_client.translate(text, target_language=target)
    return result['translatedText']

It's super easy, yeay!

To use get_label, you specifies the location of an uploaded photo in Google Cloud Storage with the bucket and file names, and the maximum number of labels you need.

To use translate_text, you pass the text string of a label and specify the target language code from the supported languages.

That's all. More details can be found in the library documentation. Enjoy!