javascript - 使用人脸作为聊天网站的密码

标签 javascript python django opencv face-recognition

我是 Django 新手,我正在通过 Django 2.0 制作一个演示聊天网站。我的动机是在人们注册时保存他们的照片,并在后端运行一个人脸身份验证 python 脚本(我已经通过 Python 上的开源 Face_recognition 库完成了)以在用户登录时识别用户的照片。我的脚本现在使用 cv2 单击照片并将其发送到面部识别引擎。

我必须将用户的照片保存在服务器端的目录中图像的名称作为用户签名时的名称上,这样我就可以运行面部验证器来循环遍历文件夹中的面部列表以查找匹配的面部。当它发现时,我可以返回名称以查询我的数据库并为该特定用户创建 session 。 (我知道这很耗时且资源密集,但由于它是一个演示,我想我可以通过它。还请建议是否可以有更快的基于面部的身份验证方法)

我的用户模型看起来像这样

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 的 OpenCVcv2numpyface_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

关于javascript - 使用人脸作为聊天网站的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48840810/

相关文章:

python - Flask - 如何使用 RotatingFileHandler 将 werkzeug 日志写入日志文件?

python - XGBoost训练时间似乎太长

python - Scrapy 飞溅^ AttributeError : 'module' object has no attribute 'Spider'

python - (Django) 从 charField 中修剪空格

django - 如何使用多个表对 django rest 中的用户进行身份验证?

javascript - 运行 react-scripts start 后如何停止?

javascript - 适用于 JavaScript API 客户端的适当 OAuth2 流程

javascript - 屏蔽剑道网格中的指定列

javascript - 是否可以根据用户的地理位置替换网页中的图像?

database - Django 原子检查和递减