Numpy:轴1超出了1维数组的范围

时间:2018-01-28 18:08:48

标签: python-3.x numpy multidimensional-array face-recognition

@shared_task()
def get_encodings_and_compare_with_friends(pk):
    photo = Photo.objects.get(pk=pk)
    persons = Person.objects.all()
    image = face_recognition.load_image_file(photo.image)
    encodings = face_recognition.face_encodings(image)
    for encoding in encodings:
        for person in persons:
            person_encoding=np.loads(person.face_encodings)
            if face_recognition.compare_faces(encoding,person_encoding):
                tag = Tags(photo= photo, tag = person.pk, is_user=True)
                tag.save()
    return

我正在尝试从数据库加载face_encodings,它存储为二进制blob。然后将来自图像的这种编码和编码与支持向量机进行比较,以发现它们是相等的。但是我在标题中提到了这个错误。

这是我正在使用的库https://github.com/ageitgey/face_recognition/

回溯

  File "/home/firefly/.local/share/virtualenvs/Inshare-0sncsKh-/lib/python3.5/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/home/firefly/.local/share/virtualenvs/Inshare-0sncsKh-/lib/python3.5/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/firefly/.local/share/virtualenvs/Inshare-0sncsKh-/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/firefly/.local/share/virtualenvs/Inshare-0sncsKh-/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/firefly/.local/share/virtualenvs/Inshare-0sncsKh-/lib/python3.5/site-packages/django/views/generic/base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/firefly/.local/share/virtualenvs/Inshare-0sncsKh-/lib/python3.5/site-packages/rest_framework/views.py", line 494, in dispatch
    response = self.handle_exception(exc)
  File "/home/firefly/.local/share/virtualenvs/Inshare-0sncsKh-/lib/python3.5/site-packages/rest_framework/views.py", line 454, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/firefly/.local/share/virtualenvs/Inshare-0sncsKh-/lib/python3.5/site-packages/rest_framework/views.py", line 491, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/firefly/.local/share/virtualenvs/Inshare-0sncsKh-/lib/python3.5/site-packages/rest_framework/generics.py", line 192, in post
    return self.create(request, *args, **kwargs)
  File "/home/firefly/.local/share/virtualenvs/Inshare-0sncsKh-/lib/python3.5/site-packages/rest_framework/mixins.py", line 21, in create
    self.perform_create(serializer)
  File "/home/firefly/SourceCode/Inshare/gallery/views.py", line 21, in perform_create
    get_encodings_and_compare_with_friends(photo.pk)
  File "/home/firefly/.local/share/virtualenvs/Inshare-0sncsKh-/lib/python3.5/site-packages/celery/local.py", line 191, in __call__
    return self._get_current_object()(*a, **kw)
  File "/home/firefly/.local/share/virtualenvs/Inshare-0sncsKh-/lib/python3.5/site-packages/celery/app/task.py", line 380, in __call__
    return self.run(*args, **kwargs)
  File "/home/firefly/SourceCode/Inshare/gallery/tasks.py", line 29, in get_encodings_and_compare_with_friends
    if face_recognition.compare_faces(encoding,person_encoding):
  File "/home/firefly/.local/share/virtualenvs/Inshare-0sncsKh-/lib/python3.5/site-packages/face_recognition/api.py", line 203, in compare_faces
    return list(face_distance(known_face_encodings, face_encoding_to_check) <= tolerance)
  File "/home/firefly/.local/share/virtualenvs/Inshare-0sncsKh-/lib/python3.5/site-packages/face_recognition/api.py", line 70, in face_distance
    return np.linalg.norm(face_encodings - face_to_compare, axis=1)
  File "/home/firefly/.local/share/virtualenvs/Inshare-0sncsKh-/lib/python3.5/site-packages/numpy/linalg/linalg.py", line 2287, in norm
    return sqrt(add.reduce(s, axis=axis, keepdims=keepdims))
numpy.core._internal.AxisError: axis 1 is out of bounds for array of dimension 1

1 个答案:

答案 0 :(得分:1)

face_recognition.compare_faces([encoding],person_encoding)

将编码作为列表解决问题