我试图使用python从googlecloudvisionapi调用函数“detectweb”。它的最好的一个标签叫“我猜不出你”。当我试图调用这个方法时,它抛出了一个错误
“AttributeError:'WebDetection'对象没有“best\u guess\u labels”属性:
https://cloud.google.com/docs/authentication/getting-started
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="WebDetection.json"
“detect web”的功能取自此链接-->
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\u guess\u labels”不是Json文件的一部分。我不确定json文件是否损坏了,但是我试图重做这个练习,但是仍然得到相同的错误。
是什么导致了这个问题?