Esempio n. 1
0
def loginFaceCheck(request):
    if request.method == "POST" and request.is_ajax():
        # 获取base64格式的图片
        start = time.clock()
        faceImage = request.POST.get('faceImg')
        # 提取出base64格式,并进行转换为图片
        index = faceImage.find('base64,')
        base64Str = faceImage[index + 6:]
        img = base64.b64decode(base64Str)

        # 不能直接将传来的图片转为numpy数组,会报错“'utf-8' codec can't decode byte 0x89...”,用python先保存再读取
        imgName = 'temp.jpg'
        with open(imgName, 'wb') as f:
            f.write(img)
        end = time.clock()
        print("img_get_write:", end - start)

        start = time.clock()
        JsonBackInfo = auth_user(imgName)
        end = time.clock()
        print("auth_user_time:", end - start)

        if JsonBackInfo['AuthName'] != "未授权用户":
            user = User.objects.get_by_natural_key(
                username=JsonBackInfo['AuthName']
            )  # authenticate(username='******', password='******')
            if user is not None:
                if user.is_active:
                    ori_login(request, user)

        return JsonResponse(JsonBackInfo)
Esempio n. 2
0
def month_open_detect(request):
    if request.method == "POST":
        startt = time.time()
        start = time.clock()
        videos = request.FILES['faceVideo']
        fileName = 'temp.webm'
        # file = uploadedfile.File(videos)
        with open(fileName, 'wb') as f:
            for chunk in videos.chunks():
                f.write(chunk)
        end = time.clock()
        print("video_get_write:", end - start)

        start = time.time()
        os.system('python3 mouth_open.py')
        while len(glob.glob(os.path.join('.', "*.npy"))) == 0:
            pass
        num = int(np.load('temp.npy'))
        end = time.time()
        print("Blink_video_stream:", end - start)

        files = glob.glob(os.path.join('.', "*.jpg"))
        imgName = files[int(len(files) / 2)]
        jsonInfo = auth_user(imgName)
        os.system('rm *.mp4 *.npy')
        JsonBackInfo = {
            "canLogin": jsonInfo['canLogin'],
            "AuthName": jsonInfo['AuthName'],
            'open_num': num
        }

        if jsonInfo['AuthName'] != "未授权用户" and num == 2:
            user = User.objects.get_by_natural_key(
                username=jsonInfo['AuthName']
            )  # authenticate(username='******', password='******')
            if user is not None:
                if user.is_active:
                    ori_login(request, user)

        print("total_time:", time.time() - startt)
        return JsonResponse(JsonBackInfo)
    else:
        return render(request, 'random-instruction.html')
Esempio n. 3
0
def test(request):
    canLogin = False
    AuthName = "未授权用户"

    if request.method == "POST" and request.is_ajax():
        video = request.POST.get('faceVideo')
        fileName = 'temp.webm'
        with open(fileName, 'wb') as f:
            f.write(video)
        JsonBackInfo = {"canLogin": canLogin, "AuthName": AuthName}

        if AuthName != "未授权用户":
            user = User.objects.get_by_natural_key(
                username=AuthName
            )  # authenticate(username='******', password='******')
            if user is not None:
                if user.is_active:
                    ori_login(request, user)

        return JsonResponse(JsonBackInfo)
    else:
        return render(request, 'test.html')
Esempio n. 4
0
def loginFaceCheck(request):
    # 人脸登陆验证
    known_people = Faces.objects.all()
    known_face = []
    auth_name = []
    for people in known_people:
        auth_name.append(people.user.username)
        face = face_recognition.load_image_file(people.face_img)
        print(people.face_img)
        face_location = face_recognition.face_locations(face)
        known_face += face_recognition.face_encodings(face, face_location)

    if request.method == "POST" and request.is_ajax():
        # 获取base64格式的图片
        faceImage = request.POST.get('faceImg')
        # 提取出base64格式,并进行转换为图片
        index = faceImage.find('base64,')
        base64Str = faceImage[index + 6:]
        img = base64.b64decode(base64Str)
        # # 将文件保存
        # backupDate = time.strftime("%Y%m%d_%H%M%S")
        # if int(request.POST.get('id')) == 0 :
        #     fileName = BASE_LOGIN_LEFT_PATH +"LeftImg_%s.jpg" % (backupDate)
        # else:
        #     fileName = BASE_LOGIN_RIGHT_PATH + "RightImg_%s.jpg" % (backupDate)
        # fileName = BASE_LOGIN_LEFT_PATH + "LeftImg_%s.jpg" % (backupDate)

        #     # 删除多余的图片
        #     filesLeft = os.listdir(BASE_LOGIN_LEFT_PATH)
        #     filesLeft.sort()
        #     leftImgCount = filesLeft.__len__()
        #     # filesRight = os.listdir(BASE_LOGIN_RIGHT_PATH)
        #     # filesRight.sort()
        #     # RightImgCount = filesRight.__len__()
        #
        #     if leftImgCount > 100:
        #         # 图片超过100个,删除一个
        #         os.unlink(BASE_LOGIN_LEFT_PATH +filesLeft[0])
        #     # if RightImgCount > 100:
        #     #     # 图片超过100个,删除一个
        #     #     os.unlink(BASE_LOGIN_RIGHT_PATH + filesRight[0])

        # 对图片进行人脸识别比对
        canLogin = False
        AuthName = "未授权用户"

        # 不能直接将传来的图片转为numpy数组,会报错“'utf-8' codec can't decode byte 0x89...”,用python先保存再读取
        fileName = 'temp.jpg'
        with open(fileName, 'wb') as f:
            f.write(img)

        # 1> 加载相机刚拍摄的人脸
        unknown_face = face_recognition.load_image_file("temp.jpg")
        locate_unknown_face = face_recognition.face_locations(unknown_face)
        landmards = face_recognition.face_landmarks(unknown_face,
                                                    locate_unknown_face)
        print('left:', landmards[0]['left_eye'])
        print('right:', landmards[0]['right_eye'])
        left_eye = landmards[0]['left_eye']
        right_eye = landmards[0]['right_eye']
        left_ear = eye_aspect_ratio(left_eye)
        right_ear = eye_aspect_ratio(right_eye)
        if left_ear > 0.18:
            print("left eye opened:", left_ear)
        else:
            print('left eye closed:', left_ear)
        if right_ear > 0.18:
            print("right eye opened:", right_ear)
        else:
            print("right eye closed:", right_ear)
        unknown_face_tmp_encoding = []
        try:
            unknown_face_tmp_encoding = face_recognition.face_encodings(
                unknown_face, locate_unknown_face)[0]
        except IndexError:
            canLogin = False  # 图片中未发现人脸

        # 2> 进行比对

        ### 第一种方法
        # results = face_recognition.face_distance(known_face,unknown_face_tmp_encoding)
        # 小于0.6即对比成功。但是效果不好,因此我们设置阈值为0.4,
        # for i, face_distance in enumerate(results):
        #     if face_distance <= 0.4:
        #         canLogin = True
        #         AuthName = os.listdir(BASE_LOGIN_AUTH_PATH)[i][:-4]

        # 第二中方法
        results1 = face_recognition.compare_faces(known_face,
                                                  unknown_face_tmp_encoding,
                                                  0.4)
        for i, face_distance in enumerate(results1):
            if face_distance == True:
                canLogin = True
                AuthName = auth_name[i]
                break

        JsonBackInfo = {"canLogin": canLogin, "AuthName": AuthName}

        if AuthName != "未授权用户":
            user = User.objects.get_by_natural_key(
                username=AuthName
            )  # authenticate(username='******', password='******')
            if user is not None:
                if user.is_active:
                    ori_login(request, user)

        return JsonResponse(JsonBackInfo)