使用Django登录的人脸识别

时间:2019-07-18 16:49:07

标签: django python-3.x web

我正在尝试实现面部识别登录系统,但是出现错误“无法将操作数与形状(128,)(0,)一起广播”,并且我不知道该如何解决。这是我已执行的view.py和facedetector.py以及从服务器获取的错误:

链接到完整的项目https://github.com/Vampboy/Face-Recognition-Login-System
以前有人Operands could not be broadcast together with shapes (128,) (0,) error曾问过这个问题,但没有得到太多回应。 可能是因为他没有提供完整的代码。

我正在得到错误提示:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/base/

Django Version: 2.2.3
Python Version: 3.6.8
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'pages']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/home/tiktok/.local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/tiktok/.local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/home/tiktok/.local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/var/www/django_app/djangoproject/mysite/pages/views.py" in base
  54.                                 if facedect(user.userprofile.head_shot.url):

File "/var/www/django_app/djangoproject/mysite/pages/views.py" in facedect
  33.                 check=face_recognition.compare_faces(face_1_face_encoding, face_encodings)

File "/home/tiktok/.local/lib/python3.6/site-packages/face_recognition/api.py" in compare_faces
  222.     return list(face_distance(known_face_encodings, face_encoding_to_check) <= tolerance)

File "/home/tiktok/.local/lib/python3.6/site-packages/face_recognition/api.py" in face_distance
  72.     return np.linalg.norm(face_encodings - face_to_compare, axis=1)

Exception Type: ValueError at /base/
Exception Value: operands could not be broadcast together with shapes (128,) (0,) 

1 个答案:

答案 0 :(得分:0)

face_recognition.compare_faces函数的第一个参数应为stated in the documentation的列表。将您的djangoproject/mysite/pages/views.py第33行更改为:

check=face_recognition.compare_faces([face_1_face_encoding], face_encodings)

解决异常的原因。