Open15

Google Cloud Vision APIをUbuntuで動かす

梁震梁震

Ubuntu側の操作

pip install --upgrade google-cloud-vision
梁震梁震
export GOOGLE_APPLICATION_CREDENTIALS="jsonキーのフルパス"

パスを通す。

梁震梁震

永続化は 「linux パス永続化」スクラップを参照

梁震梁震
python -m pip install --upgrade pip

pipは新しいのに

梁震梁震
pip install google-cloud-vision

モジュールをインストール

梁震梁震
from google.cloud import vision
from google.oauth2 import service_account

IMG_URL = "写真ファイルのURL"

# 身元証明書のjson読み込み
credentials = service_account.Credentials.from_service_account_file('key/cloud-vision-api-355607-15efdc8124ab.json')

client = vision.ImageAnnotatorClient(credentials=credentials)
image = vision.Image()
image.source.image_uri = IMG_URL

response = client.label_detection(image=image)
labels = response.label_annotations

for label in labels:
    print(label.description + ":" + str(label.score))

if response.error.message:
    raise Exception(
        '{}\nFor more info on error messages, check: '
        'https://cloud.google.com/apis/design/errors'.format(
            response.error.message))

このコードで写真のラベル付ができる(テスト済み)

梁震梁震

パスが通っていればこれでもいけるはず
公式ドキュメントのコード

import io
import os

# Imports the Google Cloud client library
from google.cloud import vision

# Instantiates a client
client = vision.ImageAnnotatorClient()

# The name of the image file to annotate
file_name = os.path.abspath('resources/wakeupcat.jpg')

# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
    content = image_file.read()

image = vision.Image(content=content)

# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations

print('Labels:')
for label in labels:
    print(label.description)
梁震梁震

クラス作成の時こうすればパスを通さなくてもいいみたい

# ライブラリーの読み込み
from google.oauth2 import service_account

# 認証用jsonファイルのパス
credentials = service_account.Credentials.from_service_account_file('key/cloud-vision-api-355607-15efdc8124ab.json')

# clientというクラスを作る際にcredentialsを入れる
client = vision.ImageAnnotatorClient(credentials=credentials)

梁震梁震

まずは

pip install google-cloud-storage
pip install --upgrade google-cloud-vision

Google Cloud Strageにアクセスできるようにする

梁震梁震
def async_detect_document(gcs_source_uri, gcs_destination_uri):
    """OCR with PDF/TIFF as source files on GCS"""
    import json
    import re
    from google.cloud import vision
    from google.cloud import storage

    # Supported mime_types are: 'application/pdf' and 'image/tiff'
    mime_type = 'application/pdf'

    # How many pages should be grouped into each json output file.
    batch_size = 2

    client = vision.ImageAnnotatorClient()

    feature = vision.Feature(
        type_=vision.Feature.Type.DOCUMENT_TEXT_DETECTION)

    gcs_source = vision.GcsSource(uri=gcs_source_uri)
    input_config = vision.InputConfig(
        gcs_source=gcs_source, mime_type=mime_type)

    gcs_destination = vision.GcsDestination(uri=gcs_destination_uri)
    output_config = vision.OutputConfig(
        gcs_destination=gcs_destination, batch_size=batch_size)

    async_request = vision.AsyncAnnotateFileRequest(
        features=[feature], input_config=input_config,
        output_config=output_config)

    operation = client.async_batch_annotate_files(
        requests=[async_request])

    print('Waiting for the operation to finish.')
    operation.result(timeout=420)

    # Once the request has completed and the output has been
    # written to GCS, we can list all the output files.
    storage_client = storage.Client()

    match = re.match(r'gs://([^/]+)/(.+)', gcs_destination_uri)
    bucket_name = match.group(1)
    prefix = match.group(2)

    bucket = storage_client.get_bucket(bucket_name)

    # List objects with the given prefix, filtering out folders.
    blob_list = [blob for blob in list(bucket.list_blobs(
        prefix=prefix)) if not blob.name.endswith('/')]
    print('Output files:')
    for blob in blob_list:
        print(blob.name)

    # Process the first output file from GCS.
    # Since we specified batch_size=2, the first response contains
    # the first two pages of the input file.
    output = blob_list[0]

    json_string = output.download_as_string()
    response = json.loads(json_string)

    # The actual response for the first page of the input file.
    first_page_response = response['responses'][0]
    annotation = first_page_response['fullTextAnnotation']

    # Here we print the full text from the first page.
    # The response contains more information:
    # annotation/pages/blocks/paragraphs/words/symbols
    # including confidence scores and bounding boxes
    print('Full text:\n')
    print(annotation['text'])

source_uri = 'gs://cloudvisionapibacket/test.pdf'
destination_uri = 'gs://cloudvisionapibacket/'

async_detect_document(source_uri, destination_uri)

これで一発で上手くいった
エラーは出たけどバケットにjsonファイルが出力された