I have made a Flask API and I want to pass an image from my Flutter app to the API POST endpoint. I successfully transfer the image, but when I want to use it in
first_image = face_recognition.load_image_file(image_from_post_request or down below image1)
it just gives me errors!
When I try this function with an image that is saved locally it works. For Example:
first_image = face_recognition.load_image_file('./img/mj.png')
I have tried saving that post image locally and then parsing it, but it doesn't work. I am a beginner with this, so any help would be much appreciated.
Thank You
My Code:
@app.route('/compare', methods=['POST'])
def compare():
image1 = request.files['face1']
image2 = request.files['face2']
first_image = face_recognition.load_image_file(image1)
first_image_encoding = face_recognition.face_encodings(first_image)[0]
unknown_image = face_recognition.load_image_file(image2)
second_image_encoding = face_recognition.face_encodings(unknown_image)[0]
results = face_recognition.compare_faces([first_image_encoding], second_image_encoding)
if results[0]:
print('These images are same')
return jsonify({'response' : 'These images are same'})
else:
print('These images are not same')
return jsonify({'response' : 'These images are not same'})
ERROR:
127.0.0.1 - - [25/May/2020 00:07:05] "POST /compare HTTP/1.1" 500 -
Traceback (most recent call last):
File "/Users/bilal/.local/share/virtualenvs/face_reck_api-svbO_hqN/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/bilal/.local/share/virtualenvs/face_reck_api-svbO_hqN/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "/Users/bilal/.local/share/virtualenvs/face_reck_api-svbO_hqN/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/bilal/.local/share/virtualenvs/face_reck_api-svbO_hqN/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/bilal/.local/share/virtualenvs/face_reck_api-svbO_hqN/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/Users/bilal/.local/share/virtualenvs/face_reck_api-svbO_hqN/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/bilal/.local/share/virtualenvs/face_reck_api-svbO_hqN/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/bilal/.local/share/virtualenvs/face_reck_api-svbO_hqN/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/bilal/.local/share/virtualenvs/face_reck_api-svbO_hqN/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/bilal/.local/share/virtualenvs/face_reck_api-svbO_hqN/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/bilal/Desktop/Python/face_reck_api/app.py", line 74, in someting
first_image_encoding = face_recognition.face_encodings(first_image)[0]
IndexError: list index out of range
Since I do not have enough reputation to comment, I will add this here
Please add error message you are getting
From the Flask Documentation, "Note that files will only contain data if the request method was POST, PUT or PATCH and the that posted to the request had enctype="multipart/form-data". It will be empty otherwise."
From what I can garner from the above code snippet you seem to be missing a request method of POST,PUT, OR PATCH
Please take a look at this it seems, that If no face is found in the image, the encodings array will be empty. So check the length of the array before you try to access the first element (i.e. element [0]) of the array.
For example:
encodings = face_recognition.face_encodings(known_image)
if len(encodings) > 0:
biden_encoding = encodings[0]
else:
print("No faces found in the image!")
quit()