使用面部作为聊天网站的密码
Using face as password for chat site
我是 Django 的新手,我正在通过 Django 2.0 制作一个演示聊天网站。我的动机是在人们注册时保存他们的照片,并在后端 运行 人脸认证 python 脚本(我已经通过 face_recognition 上的开源库 face_recognition Python) 在用户登录时识别用户。我的脚本现在使用 cv2 点击照片并将其发送到人脸识别引擎。
我必须将用户的照片保存在服务器端的目录中,并将图像的名称作为用户的名称 当他们注册时,所以我可以 运行 一个人脸认证器对我文件夹中的人脸列表进行 for-loop 以找到匹配的人脸。当它找到时,我可以 return 名称来查询我的数据库并为该特定用户 创建一个会话。 (我知道这很费时间和资源,但由于它是一个演示,我想我可以通过它。也请建议是否有更快的基于人脸的身份验证方法)
我的用户模型是这样的
class User(models.Model):
username = models.CharField(max_length=100)
name = models.CharField(max_length=100)
age = models.CharField(max_length=100)
image = models.ImageFile()
任何有 Django 经验的人都可以指导我完成此操作的途径和所需的步骤吗?
我假设您为 python 安装了 OpenCV
、cv2
,当然 numpy
,face_recognition
utils.py
def recognize_face(id_image, q_image):
"""
:param image: image of the face in the id
:param q_image: image of the face from the cam
:return:
"""
q_face_encoding = face_recognition.face_encodings(id_image)[0]
face_locations = face_recognition.face_locations(q_image)
face_encodings = face_recognition.face_encodings(q_image, face_locations)
# Loop through each face in this image
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
match = face_recognition.compare_faces([q_face_encoding], face_encoding)
result = False
if match[0]:
result = True
return result
def _grab_image(path=None, stream=None, url=None):
# if the path is not None, then load the image from disk
if path is not None:
image = cv2.imread(path)
# otherwise, the image does not reside on disk
else:
# if the URL is not None, then download the image
if url is not None:
resp = requests.get(url)
data = resp.text()
# if the stream is not None, then the image has been uploaded
elif stream is not None:
data = stream.read()
# convert the image to a NumPy array and then read it into
# OpenCV format
image = np.asarray(bytearray(data), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
# return the image
return image
views.py
class FaceVerifyAPIView(APIView):
http_method_names = ['get', 'post', 'options']
parser_classes = (parsers.MultiPartParser, parsers.FormParser)
renderer_classes = (renderers.JSONRenderer, )
def post(self, request, *args, **kwargs):
serializer = FaceDectSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
data = serializer.validated_data
id_image = data['id_image']
q_image = data['q_image'] # OR from request.user.image
id_image = _grab_image(stream=id_image)
q_image = _grab_image(stream=q_image)
result = recognize_face(id_image, q_image)
return JsonResponse({'message': str(result)}, status=200)
def get(self, request, *args, **kwargs):
return JsonResponse({'message': 'You\'re here but use post method'}, status=201)
拍一张他的脸,拍这张reference
我是 Django 的新手,我正在通过 Django 2.0 制作一个演示聊天网站。我的动机是在人们注册时保存他们的照片,并在后端 运行 人脸认证 python 脚本(我已经通过 face_recognition 上的开源库 face_recognition Python) 在用户登录时识别用户。我的脚本现在使用 cv2 点击照片并将其发送到人脸识别引擎。
我必须将用户的照片保存在服务器端的目录中,并将图像的名称作为用户的名称 当他们注册时,所以我可以 运行 一个人脸认证器对我文件夹中的人脸列表进行 for-loop 以找到匹配的人脸。当它找到时,我可以 return 名称来查询我的数据库并为该特定用户 创建一个会话。 (我知道这很费时间和资源,但由于它是一个演示,我想我可以通过它。也请建议是否有更快的基于人脸的身份验证方法)
我的用户模型是这样的
class User(models.Model):
username = models.CharField(max_length=100)
name = models.CharField(max_length=100)
age = models.CharField(max_length=100)
image = models.ImageFile()
任何有 Django 经验的人都可以指导我完成此操作的途径和所需的步骤吗?
我假设您为 python 安装了 OpenCV
、cv2
,当然 numpy
,face_recognition
utils.py
def recognize_face(id_image, q_image):
"""
:param image: image of the face in the id
:param q_image: image of the face from the cam
:return:
"""
q_face_encoding = face_recognition.face_encodings(id_image)[0]
face_locations = face_recognition.face_locations(q_image)
face_encodings = face_recognition.face_encodings(q_image, face_locations)
# Loop through each face in this image
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
match = face_recognition.compare_faces([q_face_encoding], face_encoding)
result = False
if match[0]:
result = True
return result
def _grab_image(path=None, stream=None, url=None):
# if the path is not None, then load the image from disk
if path is not None:
image = cv2.imread(path)
# otherwise, the image does not reside on disk
else:
# if the URL is not None, then download the image
if url is not None:
resp = requests.get(url)
data = resp.text()
# if the stream is not None, then the image has been uploaded
elif stream is not None:
data = stream.read()
# convert the image to a NumPy array and then read it into
# OpenCV format
image = np.asarray(bytearray(data), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
# return the image
return image
views.py
class FaceVerifyAPIView(APIView):
http_method_names = ['get', 'post', 'options']
parser_classes = (parsers.MultiPartParser, parsers.FormParser)
renderer_classes = (renderers.JSONRenderer, )
def post(self, request, *args, **kwargs):
serializer = FaceDectSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
data = serializer.validated_data
id_image = data['id_image']
q_image = data['q_image'] # OR from request.user.image
id_image = _grab_image(stream=id_image)
q_image = _grab_image(stream=q_image)
result = recognize_face(id_image, q_image)
return JsonResponse({'message': str(result)}, status=200)
def get(self, request, *args, **kwargs):
return JsonResponse({'message': 'You\'re here but use post method'}, status=201)
拍一张他的脸,拍这张reference