Reputation: 205
I got this code using Python 3.6.5 - Flask 0.12.2 exposing a service and receiving images files:
@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)
When I run the program using this CURL command, everything works fine as expected:
curl -F "[email protected]" http://localhost:5000/image
My goal is to pass using same POST method an image URL instead local file, something like:
curl -F "image=https://i.ytimg.com/vi/aeLgjgoy_kE/maxresdefault.jpg" http://localhost:5000/image
If I do so I receive following error message:
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
Shall I use other method or library? Thank you Regs S
I did modify as:
@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'
After tring CURL:
curl -F "image_url=@https://i.ytimg.com/vi/aeLgjgoy_kE/maxresdefault.jpg" http://localhost:5000/image_url
I receive following error:
Warning: setting file https://i.ytimg.com/vi/aeLgjgoy_kE/maxresdefault.jpg
Warning: failed!
curl: (26) read function returned funny value
Upvotes: 1
Views: 6150
Reputation: 205
I did modify the method to make it works:
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'
So now I've use simple CURL command:
curl http://localhost:5000/image_url?image_url=https://i.ytimg.com/vi/aeLgjgoy_kE/maxresdefault.jpg
and it works! Thx for your help
Upvotes: 0
Reputation: 5589
If you use curl with a filename it will not be stored in request.files
but in request.values
. So to get the image url you need to call
image_url = request.value['image']
Now you will need to download the image, e.g. by using urlretrieve:
import urllib.request
local_filename, headers = urllib.request.urlretrieve(image_url)
The image is now stored in a temporary file that you can access over the local_filename
.
Upvotes: 1