谷歌的Vision API可以接受来自任何外部服务器的URL并返回该图像的响应吗?

时间:2017-04-26 10:40:08

标签: python python-2.7 gcloud vision

我已经能够在本地存储的图像上成功运行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'

1 个答案:

答案 0 :(得分:0)

你不应该在这里使用io.open尝试使用requests模块

import requests  # the lib that handles the url stuff

data = requests.get(file_name)
content = data.content

您无法使用io.open功能打开远程文件。阅读有关请求模块的更多信息,以处理对远程文件的请求。