Python-Flask-pass图像URL而不是文件
我使用Python 3.6.5-Flask 0.12.2获得了这段代码,公开了一个服务并接收图像文件:Python-Flask-pass图像URL而不是文件,python,curl,flask,Python,Curl,Flask,我使用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
@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
我应该使用其他方法还是库?
非常感谢。
雷格斯
我确实修改为:
@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
如果将curl与文件名一起使用,它将不会存储在
request.files
中,而是存储在request.values
中。因此,要获取图像url,您需要调用
image_url = request.value['image']
现在您需要下载图像,例如使用:
图像现在存储在一个临时文件中,您可以通过
本地\u文件名访问该文件
我确实修改了该方法以使其正常工作:
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
而且它有效!
谢谢您的帮助您是否尝试过前面没有https的图像URL?是的,但它不起作用您好,很抱歉我的帖子有点乱。基本上我已经按照上面所述进行了修改,但在临时文件中检索图像时仍然有问题。您尝试打开远程图像文件。将
image\u object=image.open(image\u file)
替换为image\u object=image.open(local\u filename)
完成,但更改后我收到此错误:127.0.0.1---[10/Apr/2018 14:15:11]“POST/image\u url HTTP/1.1”200-“Request”对象没有属性“value”
curl http://localhost:5000/image_url?image_url=https://i.ytimg.com/vi/aeLgjgoy_kE/maxresdefault.jpg