我已经能够在本地存储的图像上成功运行 Google 的 Vision API。但是,每当我在存储在外部服务器上的图像上运行脚本时。我收到错误。
import io
import os
from google.cloud import vision
vision_client = vision.Client()
file_name = "https://static.pexels.com/photos/36753/flower-purple-lical-blosso.jpg"
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = vision_client.image(content=content, )
labels = image.detect_labels()
for label in labels:
print(label.description)
错误提示
Traceback (most recent call last):
File "visionex.py", line 8, in <module>
with io.open(file_name, 'rb') as image_file:
IOError: [Errno 2] No such file or directory:
'https://static.pexels.com/photos/36753/flower-purple-lical-blosso.jpg'
你不应该在这里使用 io.open 尝试使用 requests 模块
import requests # the lib that handles the url stuff
data = requests.get(file_name)
content = data.content
您无法使用 io.open 函数打开远程文件。阅读有关请求模块的更多信息以处理对远程文件的请求。