【问题标题】:Python - Flask - pass image URL instead of filePython - Flask - 传递图像 URL 而不是文件
【发布时间】:2018-04-10 06:26:52
【问题描述】:

我使用 Python 3.6.5 - Flask 0.12.2 获得此代码,公开服务并接收图像文件:

@app.route('/image', methods=['POST'])
def image():
    try:
        image_file = request.files['image']  # get the image

        # Set an image confidence threshold value to limit returned data
        threshold = request.form.get('threshold')
        if threshold is None:
            threshold = 0.5
        else:
            threshold = float(threshold)

        # finally run the image through tensor flow object detection`
        image_object = Image.open(image_file)
        objects = od_ws_api.get_objects(image_object, threshold)
        return objects

    except Exception as e:
        print(e)

当我使用这个 CURL 命令运行程序时,一切正常:

curl -F "image=@xxx.jpg" http://localhost:5000/image

我的目标是使用相同的 POST 方法传递图像 URL 而不是本地文件,例如:

curl -F "image=https://i.ytimg.com/vi/aeLgjgoy_kE/maxresdefault.jpg" http://localhost:5000/image

如果这样做,我会收到以下错误消息:

400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
127.0.0.1 - - [10/Apr/2018 08:20:52] "POST /image HTTP/1.1" 200

我应该使用其他方法或库吗? 谢谢 注册 S


我确实修改为:

@app.route('/image_url', methods=['POST'])
def image_url():
    try:
        image_url = request.value['image_url']  # get the image URL
        local_filename='c:/tensorflow/temp.jpg'
        local_filename, headers = urllib.request.urlretrieve(image_url)
        # Set an image confidence threshold value to limit returned data
        threshold = request.form.get('threshold')
        if threshold is None:
            threshold = 0.5
        else:
            threshold = float(threshold)

        # finally run the image through tensor flow object detection`
        image_object = Image.open(image_file)
        objects = od_ws_api.get_objects(image_object, threshold)
        return objects

    except Exception as e:
        print(e)
        return 'error'

曲折后:

curl -F "image_url=@https://i.ytimg.com/vi/aeLgjgoy_kE/maxresdefault.jpg" http://localhost:5000/image_url

我收到以下错误:

Warning: setting file https://i.ytimg.com/vi/aeLgjgoy_kE/maxresdefault.jpg
Warning: failed!
curl: (26) read function returned funny value

【问题讨论】:

  • 你试过不带https的图片URL吗?
  • 可以,但是不行

标签: python curl flask


【解决方案1】:

如果您使用带有文件名的 curl,它将不会存储在 request.files 中,而是存储在 request.values 中。因此,要获取您需要调用的图片网址

image_url = request.value['image']

现在您需要下载图像,例如通过使用urlretrieve:

import urllib.request
local_filename, headers = urllib.request.urlretrieve(image_url)

图像现在存储在一个临时文件中,您可以通过local_filename 访问该文件。

【讨论】:

  • 你好,很抱歉我的帖子有点乱。基本上我做了上面的修改,但在检索临时文件上的图像时仍然遇到问题。
  • 您尝试打开远程图像文件。将image_object = Image.open(image_file) 替换为image_object = Image.open(local_filename)
  • 完成,但更改后我收到此错误:127.0.0.1 - - [10/Apr/2018 14:15:11] "POST /image_url HTTP/1.1" 200 - 'Request' object没有属性“值”
【解决方案2】:

我确实修改了方法以使其工作:

import requests
@app.route('/image_url', methods=['GET'])
def image_url():
    try:
        f = open('c:/tensorflow1/temp.jpg','wb')
        image_url = request.args['image_url']  # get the image URL
        f.write(requests.get(image_url).content)
        f.close()
        # Set an image confidence threshold value to limit returned data
        threshold = request.form.get('threshold')
        if threshold is None:
            threshold = 0.5
        else:
            threshold = float(threshold)

        # finally run the image through tensor flow object detection`
        image_object = Image.open('c:/tensorflow1/temp.jpg')
        objects = od_ws_api.get_objects(image_object, threshold)
        return objects

    except Exception as e:
        print(e)
        return 'error'

所以现在我使用了简单的 CURL 命令:

curl http://localhost:5000/image_url?image_url=https://i.ytimg.com/vi/aeLgjgoy_kE/maxresdefault.jpg

它有效! 谢谢你的帮助

【讨论】:

    猜你喜欢
    • 2020-09-11
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 2014-02-14
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    相关资源
    最近更新 更多