我尝试用Google Vision API标记图像-这段代码通常工作得很好:
from google.cloud import vision
import os
import sys
if __name__ == '__main__':
path = os.path.abspath(os.path.dirname(sys.argv[0]))
credsFN = os.path.join(path, "creds.json")
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credsFN
imageURL = "https://www.24mantra.com/wp-content/uploads/2019/10/banner-3.jpg"
client = vision.ImageAnnotatorClient()
image = vision.Image()
image.source.image_uri = imageURL
features = [vision.Feature.Type.LABEL_DETECTION]
features = [vision.Feature(type_=feature_type) for feature_type in features]
requ = vision.AnnotateImageRequest(image=image, features=features)
resp = client.annotate_image(request=requ)
for label in resp.label_annotations:
print(f"{label.score:4.0%}: {label.description}")
现在我尝试了一些其他的url,如:
https://www.juicer.io/api/posts/459631754/images.jpg?external_id=CiE264cqZcw& s = 199 f57b090efba5ef332c3ff09d7b14554f84bfc
(当您点击链接时,您将看到图像在浏览器中正常打开)
但是当我用这个链接运行上面的程序时,它不再工作了,我得到一个空的响应?我怎么能得到的结果也从谷歌的Vison API与此链接?
有错误的链接有一个错误代码:7与错误消息:"We're not allowed to access the URL on your behalf. Please download the content and pass it in."
可能的解决方法是存储你的图片在Google Cloud Storage(GCS)用gs://uri
代替。参考