Python 人脸识别未处理通过的图像

Python 人脸识别未处理通过的图像,python,flask,image-processing,face-recognition,flask-restful,Python,Flask,Image Processing,Face Recognition,Flask Restful,我已经制作了一个FlaskAPI,我想将一个图像从我的Flatter应用程序传递到API POST端点。我成功地传输了图像,但是当我想在 first_image = face_recognition.load_image_file(image_from_post_request or down below image1) 它只会给我错误 当我使用本地保存的图像尝试此功能时,它可以工作。例如: first_image = face_recognition.load_image_file(

我已经制作了一个FlaskAPI,我想将一个图像从我的Flatter应用程序传递到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')
我曾尝试在本地保存该帖子图像,然后对其进行解析,但不起作用。我是这方面的初学者,所以任何帮助都将不胜感激

多谢各位

我的代码:

@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

由于我没有足够的声誉发表评论,我将在这里补充这一点

  • 请添加您收到的错误消息

  • 从“请注意,如果请求方法是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()
    
    ``

    谢谢,但问题不在POST请求中,而是在图像处理中。您能澄清一下哪个功能似乎失败了吗?1.第一个图像=人脸识别。加载图像文件('./img/mj.png')2。first_image_encoding=人脸识别。人脸编码(first_image)[0]从堆栈跟踪来看,失败发生的地方似乎是2号,但您最初的评论似乎说1号是您认为失败的地方?我问这个问题是因为我无法运行face_recognition来测试我自己@BiloSuper