2017-07-18 5 views
2

グーグルビジョンは入力画像のサイズがrestrictionsなので、まず入力画像のサイズを変更してからdetect_labels()関数を使用します。彼らは、画像ファイルを開くためにioを使用して自分のsample codePython画像のサイズを変更してGoogleビジョン関数に送信

def detect_labels(path): 
"""Detects labels in the file.""" 
vision_client = vision.Client() 

with io.open(path, 'rb') as image_file: 
    content = image_file.read() 

image = vision_client.image(content=content) 

labels = image.detect_labels() 
print('Labels:') 

for label in labels: 
    print(label.description) 

がここにあります。私はこのように画像のメモリにリサイズしてからdetect_labels()と呼ぶのだろうか?

+0

with io.open(path, 'rb') as image_file: content = image_file.read() 

を置き換えます特大のイメージ? – kristaps

+0

はい、画像が大きすぎると言います: 'google.gax.errors.RetryError:GaxError(RPCが) ' – user21

答えて

1

あなたはPIL /枕を介して画像のサイズを変更して、クライアントにそれを渡すことができます:

あなたがそれを渡すとき、クライアントは任意の例外をスローしてい

# Warning: untested code, may require some fiddling 
import Image, StringIO 

img = Image.open(path) 
img.thumbnail((512, 512)) 
buffer = StringIO.StringIO() 
img.save(buffer, "PNG") 

content = buffer.getvalue() 
+0

サイズ変更操作の上限を設定する方法はありますか(cf https://stackoverflow.com/questions/45322213/python-set-maximum-file-size-when-converting-pdf- to-jpeg-eg-wand)? – jtlz2

関連する問題