face_recognition python flask 未处理通过的图像
face_recognition python flask not processing passed image
我制作了一个 Flask API,我想将图像从我的 Flutter 应用程序传递到 API POST 端点。我成功传输图像,但是当我想在
中使用它时
first_image = face_recognition.load_image_file(image_from_post_request or down below image1)
它只会给我错误!
当我使用本地保存的图像尝试此功能时,它起作用了。例如:
first_image = face_recognition.load_image_file('./img/mj.png')
我试过在本地保存那个 post 图像然后解析它,但它不起作用。我是这方面的初学者,所以任何帮助将不胜感激。
谢谢
我的代码:
@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'})
错误:
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
由于我没有足够的声誉来评论,我会在这里添加
请添加您收到的错误消息
来自 Flask Documentation,“请注意,如果请求方法是 POST、PUT 或 PATCH 并且发布到请求有 enctype="multipart/form-data"。否则它将为空。"
根据我从上面的代码片段中得到的信息,您似乎缺少 POST、PUT 或 PATCH
的请求方法
更新
请看这个好像是,如果在图像中找不到人脸,编码数组将为空。因此,在尝试访问数组的第一个元素(即元素 [0])之前,请检查数组的长度。
例如:
encodings = face_recognition.face_encodings(known_image)
if len(encodings) > 0:
biden_encoding = encodings[0]
else:
print("No faces found in the image!")
quit()
我制作了一个 Flask API,我想将图像从我的 Flutter 应用程序传递到 API POST 端点。我成功传输图像,但是当我想在
中使用它时 first_image = face_recognition.load_image_file(image_from_post_request or down below image1)
它只会给我错误!
当我使用本地保存的图像尝试此功能时,它起作用了。例如:
first_image = face_recognition.load_image_file('./img/mj.png')
我试过在本地保存那个 post 图像然后解析它,但它不起作用。我是这方面的初学者,所以任何帮助将不胜感激。
谢谢
我的代码:
@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'})
错误:
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
由于我没有足够的声誉来评论,我会在这里添加
请添加您收到的错误消息
来自 Flask Documentation,“请注意,如果请求方法是 POST、PUT 或 PATCH 并且发布到请求有 enctype="multipart/form-data"。否则它将为空。"
根据我从上面的代码片段中得到的信息,您似乎缺少 POST、PUT 或 PATCH
的请求方法更新
请看这个好像是,如果在图像中找不到人脸,编码数组将为空。因此,在尝试访问数组的第一个元素(即元素 [0])之前,请检查数组的长度。
例如:
encodings = face_recognition.face_encodings(known_image)
if len(encodings) > 0:
biden_encoding = encodings[0]
else:
print("No faces found in the image!")
quit()