<p>您可能希望改用Vision API的<a href="https://googlecloudplatform.github.io/google-cloud-python/" rel="nofollow noreferrer">^{<cd1>} client library</a>。</p>
<p>要使用正确的作用域进行身份验证,您需要在云控制台中生成一个服务帐户,并从代码(或环境变量)指向它。有关详细信息,请参见<a href="https://googlecloudplatform.github.io/google-cloud-python/stable/vision-usage.html#authentication-and-configuration" rel="nofollow noreferrer">the Vision auth section</a>:</p>
<p>从云控制台中的<a href="https://console.cloud.google.com/apis/credentials" rel="nofollow noreferrer">credentials manager</a>获取服务帐户。然后指向您环境中的项目和JSON凭据文件:</p>
<pre><code>$ export GOOGLE_CLOUD_PROJECT="your-project-id-here"
$ export GOOGLE_APPLICATION_CREDENTIALS="/path/to/keyfile.json"
</code></pre>
<p>您试图运行的演示(标记图像)变成:</p>
<pre><code>>>> from google.cloud import vision
>>> client = vision.Client()
>>> image = client.image(source_uri='gs://my-storage-bucket/image.jpg')
>>> labels = image.detect_labels(limit=3)
>>> labels[0].description
'automobile'
>>> labels[0].score
0.9863683
</code></pre>
<p>(代码片段取自<a href="https://googlecloudplatform.github.io/google-cloud-python/stable/vision-usage.html#label-detection" rel="nofollow noreferrer">docs</a>)</p>