简体   繁体   English

face_recognition python flask 不处理通过的图像

[英]face_recognition python flask not processing passed image

I have made a Flask API and I want to pass an image from my Flutter app to the API POST endpoint.我已经制作了 Flask API 并且我想将图像从我的 Flutter 应用程序传递到 ZDB974238714CA8DE634POSTACE 端点。 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由于我没有足够的声誉发表评论,我将在此处添加

  1. Please add error message you are getting请添加您收到的错误消息

  2. 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. "Flask 文档中,“请注意,如果请求方法是 POST、PUT 或 PATCH,并且发布到请求的文件具有 enctype="multipart/form-data",则文件将仅包含数据。否则它将为空。

From what I can garner from the above code snippet you seem to be missing a request method of POST,PUT, OR PATCH从我可以从上面的代码片段中获得的信息,您似乎缺少 POST、PUT 或 PATCH 的请求方法

Update更新

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 (ie element [0]) of the array.因此,在尝试访问数组的第一个元素(即元素 [0])之前,请检查数组的长度。

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()

`` Github link to similar issue `` Github 链接到类似问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM