【发布时间】:2018-12-09 11:54:52
【问题描述】:
我正在尝试使用 python 从 Google Cloud Vision API 调用函数“检测网络”。但是,我无法调用其名为“best_guess_labels”的方法之一。当我尝试调用该方法时,它会抛出一个错误 "AttributeError: 'WebDetection' 对象没有属性 'best_guess_labels':
WebDetection 是一个使用此链接创建并存储到本地文件夹中的 json 文件 ==> https://cloud.google.com/docs/authentication/getting-started
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="WebDetection.json"
“检测网页”功能取自此链接 --> https://cloud.google.com/vision/docs/detecting-web
这是从上述链接复制的功能,供您参考。
def detect_web(path):
"""Detects web annotations given an image."""
client = vision.ImageAnnotatorClient()
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.web_detection(image=image)
annotations = response.web_detection
if annotations.best_guess_labels:
for label in annotations.best_guess_labels:
print('\nBest guess label: {}'.format(label.label))
if annotations.pages_with_matching_images:
print('\n{} Pages with matching images found:'.format(
len(annotations.pages_with_matching_images)))
for page in annotations.pages_with_matching_images:
print('\n\tPage url : {}'.format(page.url))
if page.full_matching_images:
print('\t{} Full Matches found: '.format(
len(page.full_matching_images)))
for image in page.full_matching_images:
print('\t\tImage url : {}'.format(image.url))
if page.partial_matching_images:
print('\t{} Partial Matches found: '.format(
len(page.partial_matching_images)))
for image in page.partial_matching_images:
print('\t\tImage url : {}'.format(image.url))
if annotations.web_entities:
print('\n{} Web entities found: '.format(
len(annotations.web_entities)))
for entity in annotations.web_entities:
print('\n\tScore : {}'.format(entity.score))
print(u'\tDescription: {}'.format(entity.description))
if annotations.visually_similar_images:
print('\n{} visually similar images found:\n'.format(
len(annotations.visually_similar_images)))
for image in annotations.visually_similar_images:
print('\tImage url : {}'.format(image.url))
但是,当我使用此代码执行上述功能时
detect_web("download.jpg")
我收到以下错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-71-6d38dd9b3a76> in <module>()
----> 1 detect_web("download.jpg")
<ipython-input-70-c127dc709a32> in detect_web(path)
13 annotations = response.web_detection
14
---> 15 if annotations.best_guess_labels:
16 for label in annotations.best_guess_labels:
17 print('\nBest guess label: {}'.format(label.label))
AttributeError: 'WebDetection' object has no attribute 'best_guess_labels'
我尝试调试,发现“best_guess_labels”不是Json文件的一部分。我不确定 json 文件是否损坏,但我尝试重做练习,但我仍然遇到同样的错误。
可能是什么导致了这个问题?
【问题讨论】:
-
是你的 google-cloud-vision==0.32.0
-
@TorryYang - 它是 0.29.0
-
请尝试更新
-
@TorryYang - 我已经更新到 32。但我仍然面临同样的问题。
标签: python-3.x google-cloud-platform google-cloud-vision