def verify(self, thresh):  # 识别比对
        global VIDEO_IMAGE
        global isPass
        global first
        new_person = 0  # newperson = 1时则说明有人来
        knownflag = 0
        unknownflag = 0
        currentunknownum = 0
        currentknownum = 0
        idname = []  # 可显示id照片的编号列表
        currentDate = datetime.now().strftime('%Y_%m_%d')
        currentTime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        currentTime_img = datetime.now().strftime('%Y-%m-%d %H-%M-%S')

        # if self.pic_time != None and (datetime.datetime.now() - self.pic_time).seconds > pic_time:
        # self.image_clear()
        image1 = None
        for i in range(VIDEO_IMAGE.qsize()):
            image1 = VIDEO_IMAGE.get()
        #image1 = cv2.imread('./user_pic/13800000001.bmp')  # 加载图片测试
        if image1 is not None:
            print('get image')
            image1 = cv2.flip(image1, 1)
            width = int(image1.shape[1] / 2)
            height = int(image1.shape[0] / 2)
            image = cv2.resize(image1.copy(), (width, height))

            try:
                # total_start = time.time()
                bboxs = self.naivedlib.getALLFaces(image)
                # bbox_end = time.time()
                print('bbox detect took ')
                bboxs = [dlib.rectangle(int(bbox.left() * downsample_ratio), int(bbox.top() * downsample_ratio),
                                        int(bbox.right() * downsample_ratio), int(bbox.bottom() * downsample_ratio))
                         for bbox in bboxs]
                facenum = len(bboxs)

                if bboxs is not None:
                    new_person = 1
                    print(time.strftime('%H:%M:%S', time.localtime(time.time())))
                    for bbox in bboxs:
                        # 对每个人脸进行操作
                        print('bbox is detected')
                        query = []
                        left_eye, right_eye = self.naivedlib.get_eyes(image1, bbox)
                        start = time.time()
                        save_croped, crop_image = crop_rotate_part(image1[:, :, ::-1], bbox, left_eye, right_eye,
                                                                   bbox.width() * 0.895)
                        save_croped = cv2.cvtColor(save_croped, cv2.COLOR_BGR2RGB)

                        # bbox_temp = [bbox.left(),bbox.top(),bbox.right(),bbox.bottom()]
                        # crop_image = crop_only(image1,bbox_temp)
                        # cv2.imwrite('image.jpg',crop_image)
                        end = time.time()
                        print('\tpreprocess took {}'.format(end - start))

                        flip_image = cv2.flip(crop_image.copy(), 1)
                        crop_image = proc_img(crop_image, is_whiten=False)
                        flip_image = proc_img(flip_image, is_whiten=False)
                        query.append(crop_image[0])
                        query.append(flip_image[0])
                        prob_image = np.asarray(query)

                        start = time.time()
                        feature = self.extractor.extract_feature(prob_image)
                        end = time.time()
                        print('\textract feature took {}'.format(end - start))
                        feature = feature / np.linalg.norm(feature)
                        idx_list = self.search_eign.search(feature)  # 获取搜索后的排列
                        idx_list = [idx_list[0][i] for i in range(idx_list.shape[1])]
                        score_list = []
                        name_list = []
                        for idx in idx_list:
                            score = 1 - spatial.distance.cosine(feature, self.feature_data[int(idx)])
                            name = {value: key for key, value in self.index_dict.items()}[int(idx)]
                            score_list.append(score)
                            name_list.append(name)
                        #print(name_list)
                        #print(score_list)

                        score = 1 - spatial.distance.cosine(feature, self.feature_data[int(idx_list[0])])
                        thresh = float(thresh)
                        # print(thresh)
                        if score > thresh:
                            knownflag = 1
                            name = name_list[0]
                            if self.knownum == 0:
                                self.knownum = 1
                                first = 1
                                self.summary["comtime"] = currentTime
                                self.knownlist.append(name)
                                if not os.path.exists('./image_saved/{}'.format(currentDate)):
                                    os.mkdir('./image_saved/{}'.format(currentDate))
                                    os.mkdir('./image_saved/{}/unknown'.format(currentDate))
                                    os.mkdir('./image_saved/{}/known'.format(currentDate))
                                cv2.imwrite('./image_saved/{}/known/{}_{}.jpg'.format(currentDate, currentTime_img,
                                                                                      name),
                                            save_croped)
                                idname.append(name)  # 当前帧中的已知访客name
                                print("欢迎" + name)
                                print("推送")
                                push_info = self.push_summary_known(name)
                                s = json.dumps(push_info)
                                r = requests.post(faceverify_post_url, data=s)
                                print(r)
                                #face.insert_summary()
                                # print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
                            else:

                                if name not in self.knownlist:
                                    self.knownlist.append(name)
                                    self.knownum = self.knownum + 1
                                    cv2.imwrite('./image_saved/{}/known/{}_{}.jpg'.format(currentDate, currentTime_img,
                                                                                          name),
                                                save_croped)
                                    idname.append(name)  # 当前帧中的已知访客name

                                    print("欢迎" + name)
                                    print("推送")
                                    push_info = self.push_summary_known(name)
                                    s = json.dumps(push_info)
                                    r = requests.post(faceverify_post_url, data=s)
                                    print(r)
                                    #face.insert_summary()
                                    # print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

                        else:
                            unknownflag = 1
                            if self.unknownum == 0:
                                print("第一个未知人员")
                                first = 1
                                currentunknownum = currentunknownum + 1
                                self.unknownum = 1
                                if self.summary == []:
                                    self.summary["comtime"] = currentTime
                                if not os.path.exists('./image_saved/{}'.format(currentDate)):
                                    os.mkdir('./image_saved/{}'.format(currentDate))
                                    os.mkdir('./image_saved/{}/unknown'.format(currentDate))
                                    os.mkdir('./image_saved/{}/known'.format(currentDate))

                                cv2.imwrite('./image_saved/{}/unknown/{}_{}.jpg'.format(currentDate, currentTime_img,
                                                                                        self.unknownum), save_croped)
                                self.unknownfeature = feature[np.newaxis, :]
                                print("推送")
                                push_info = self.push_summary_unknown()
                                s = json.dumps(push_info)
                                r = requests.post(faceverify_post_url, data=s)
                                print(r)
                               # face.insert_summary()
                            else:
                                unthresh = 0.5
                                self.search_eign_unknown = Search_engin(self.unknownfeature)  # 未知访客搜索
                                self.search_eign_unknown.train()
                                unknown_idx = self.search_eign_unknown.search(feature)
                                unknown_idx = [unknown_idx[0][i] for i in range(unknown_idx.shape[1])]
                                print(unknown_idx)
                                uscore = 1 - spatial.distance.cosine(feature, self.unknownfeature[int(unknown_idx[0])])
                                print(uscore)
                                if uscore < unthresh:  # 第一次识别出该未知人员
                                    self.unknownum = self.unknownum + 1
                                    currentunknownum = currentunknownum + 1
                                    # self.unknownlist[self.unknownum] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                                    cv2.imwrite(
                                        './image_saved/{}/unknown/{}_{}.jpg'.format(currentDate, currentTime_img,
                                                                                    self.unknownum), save_croped)
                                    self.unknownfeature = np.vstack((self.unknownfeature, feature))
                                    print("推送")
                                    push_info = self.push_summary_unknown()
                                    s = json.dumps(push_info)
                                    r = requests.post(faceverify_post_url, data=s)
                                    print(r)
                                    #face.insert_summary()
                else:
                    new_person = 0
            except:
                pass
        else:
            print("图片为空!")
        return (new_person)
Exemple #2
0
def verify_img(img):
    image1 = img
    p_id_list = []
    api_unknown = 0
    if image1 is not None:
        with open('./data/index_enter.pkl', 'rb') as f:
            index_dict = pkl.load(f)
        with open('./data/database_enter.npy', 'rb') as f:
            feature_data = np.load('./data/database_enter.npy')
        search_eign = Search_engin(feature_data)
        search_eign.train()
        #print('get image')
        image1 = cv2.flip(image1, 1)
        width = int(image1.shape[1] / 2)
        height = int(image1.shape[0] / 2)
        image = cv2.resize(image1.copy(), (width, height))

        try:
            bboxs = naivedlib.getALLFaces(image)
            print('bbox detect took ')
            bboxs = [dlib.rectangle(int(bbox.left() * downsample_ratio), int(bbox.top() * downsample_ratio),
                                    int(bbox.right() * downsample_ratio), int(bbox.bottom() * downsample_ratio))
                     for bbox in bboxs]
            facenum = len(bboxs)
            if bboxs is not None:
                for bbox in bboxs:
                    # 对每个人脸进行操作
                    #print('bbox is detected')
                    query = []
                    left_eye, right_eye = naivedlib.get_eyes(image1, bbox)
                    start = time.time()
                    save_croped, crop_image = crop_rotate_part(image1[:, :, ::-1], bbox, left_eye, right_eye,
                                                               bbox.width() * 0.895)
                    save_croped = cv2.cvtColor(save_croped, cv2.COLOR_BGR2RGB)
                    end = time.time()
                    print('\tpreprocess took {}'.format(end - start))
                    flip_image = cv2.flip(crop_image.copy(), 1)
                    crop_image = proc_img(crop_image, is_whiten=False)
                    flip_image = proc_img(flip_image, is_whiten=False)
                    query.append(crop_image[0])
                    query.append(flip_image[0])
                    prob_image = np.asarray(query)

                    start = time.time()
                    feature = extractor.extract_feature(prob_image)
                    end = time.time()
                    print('\textract feature took {}'.format(end - start))
                    feature = feature / np.linalg.norm(feature)
                    idx_list = search_eign.search(feature)  # 获取搜索后的排列
                    idx_list = [idx_list[0][i] for i in range(idx_list.shape[1])]
                    score_list = []
                    name_list = []
                    for idx in idx_list:
                        score = 1 - spatial.distance.cosine(feature, feature_data[int(idx)])
                        name = {value: key for key, value in index_dict.items()}[int(idx)]
                        score_list.append(score)
                        name_list.append(name)
                    #print(name_list)
                    #print(score_list)
                    score = 1 - spatial.distance.cosine(feature, feature_data[int(idx_list[0])])
                    #print(thresh)
                    if score > thresh:
                        p_id_list.append(name_list[0])
                    else:
                        api_unknown = api_unknown + 1

        except:
            pass
        #print(p_id_list)
    return (p_id_list, api_unknown)
Exemple #3
0
    def verify(self, thresh):
        global VIDEO_IMAGE
        global isPass
        knownflag = 0
        unknownflag = 0
        currentunknownum = 0
        currentknownum = 0
        idname = []  # 可显示id照片的编号列表
        currentDate = datetime.now().strftime('%Y_%m_%d')
        currentTime = datetime.now().strftime('%Y-%m-%d %H-%M-%S')
        if currentDate != self.datetime: #新日期触发
            self.today_clean(self.datetime)
            self.datetime = currentDate
        web = Web_Browser()
        web.setModal(False)
        web.setWindowTitle(_translate("Face_Verification", "比对结果", None))
        # if self.pic_time != None and (datetime.datetime.now() - self.pic_time).seconds > pic_time:
            # self.image_clear()

        for i in range(probe_image.qsize()):
            image1 = probe_image.get()
        #image1 = cv2.imread('./card/2.jpg')  # 加载图片测试
        if image1 is not None:
            print('get image')
            image1 = cv2.flip(image1, 1)
            width = int(image1.shape[1] / 2)
            height = int(image1.shape[0] / 2)
            image = cv2.resize(image1.copy(), (width, height))

            try:
                    #total_start = time.time()
                    bboxs = self.naivedlib.getALLFaces(image)
                    #bbox_end = time.time()
                    print('bbox detect took ')
                    bboxs = [dlib.rectangle(int(bbox.left() * downsample_ratio), int(bbox.top() * downsample_ratio),
                                            int(bbox.right() * downsample_ratio), int(bbox.bottom() * downsample_ratio))
                             for bbox in bboxs]
                    facenum = len(bboxs)

                    #print(facenum)#人脸数
                    # index_list = []
                    #idname = []  # 可显示id照片的编号列表
                    if bboxs is not None:
                        print(time.strftime('%H:%M:%S', time.localtime(time.time())))
                        for bbox in bboxs:
                            # 对每个人脸进行操作
                            print('bbox is detected')
                            query = []
                            left_eye, right_eye = self.naivedlib.get_eyes(image1, bbox)
                            start = time.time()
                            save_croped, crop_image = crop_rotate_part(image1[:, :, ::-1], bbox, left_eye, right_eye,
                                                                       bbox.width() * 0.895)
                            save_croped = cv2.cvtColor(save_croped, cv2.COLOR_BGR2RGB)

                            # bbox_temp = [bbox.left(),bbox.top(),bbox.right(),bbox.bottom()]
                            # crop_image = crop_only(image1,bbox_temp)
                            # cv2.imwrite('image.jpg',crop_image)
                            end = time.time()
                            print('\tpreprocess took {}'.format(end - start))

                            flip_image = cv2.flip(crop_image.copy(), 1)
                            crop_image = proc_img(crop_image, is_whiten=False)
                            flip_image = proc_img(flip_image, is_whiten=False)
                            query.append(crop_image[0])
                            query.append(flip_image[0])
                            prob_image = np.asarray(query)

                            start = time.time()
                            feature = self.extractor.extract_feature(prob_image)
                            end = time.time()
                            print('\textract feature took {}'.format(end - start))
                            feature = feature / np.linalg.norm(feature)
                            idx_list = self.search_eign.search(feature)  # 获取搜索后的排列
                            idx_list = [idx_list[0][i] for i in range(idx_list.shape[1])]
                            score_list = []
                            name_list = []
                            for idx in idx_list:
                                score = 1 - spatial.distance.cosine(feature, self.feature_data[int(idx)])
                                name = {value: key for key, value in self.index_dict.items()}[int(idx)]
                                score_list.append(score)
                                name_list.append(name)
                            print(name_list)
                            print(score_list)
                            #person_list = [name.split('_')[0] for name in name_list]
                            #person_set = list(set(person_list))
                            #person_num = len(person_set)
                            #print(person_num)

                            score = 1 - spatial.distance.cosine(feature, self.feature_data[int(idx_list[0])])
                            thresh = float(thresh)
                            print(thresh)
                            if score > thresh:
                                knownflag = 1
                                name = name_list[0]
                                if self.knownum == 0:
                                    self.knownum = 1
                                    if not os.path.exists('./image_saved/{}'.format(currentDate)):
                                        os.mkdir('./image_saved/{}'.format(currentDate))
                                        os.mkdir('./image_saved/{}/unknown'.format(currentDate))
                                        os.mkdir('./image_saved/{}/known'.format(currentDate))
                                    cv2.imwrite('./image_saved/{}/known/{}_{}.jpg'.format(currentDate, currentTime,
                                                                                            name),
                                                save_croped)
                                    idname.append(name) #当前帧中的已知访客name
                                    print("欢迎" + name)
                                    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
                                else:
                                    img_list = os.listdir(self.known_pic_path)  # 返回指定的文件夹包含的文件或文件夹的名字的列表
                                    knownlist = [item.split('.')[0] for item in img_list]
                                    knownlist = [item.split('_')[1] for item in knownlist]
                                    if name not in knownlist:
                                        cv2.imwrite('./image_saved/{}/known/{}_{}.jpg'.format(currentDate, currentTime,
                                                                                            name),
                                                save_croped)
                                        idname.append(name) #当前帧中的已知访客name
                                        self.knownum = self.knownum + 1
                                        print("欢迎" + name)
                                        print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

                            else:
                                unknownflag = 1
                                if self.unknownum == 0:
                                    print("第一个未知人员")
                                    currentunknownum = currentunknownum + 1
                                    self.unknownum = 1
                                    #self.unknownlist[self.unknownum] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                                    if not os.path.exists('./image_saved/{}'.format(currentDate)):
                                        os.mkdir('./image_saved/{}'.format(currentDate))
                                        os.mkdir('./image_saved/{}/unknown'.format(currentDate))
                                        os.mkdir('./image_saved/{}/known'.format(currentDate))

                                    cv2.imwrite('./image_saved/{}/unknown/{}_{}.jpg'.format(currentDate, currentTime,
                                                                                        self.unknownum),save_croped)
                                    self.unknownfeature = feature[np.newaxis, :]

                                else:
                                    unthresh = 0.6
                                    self.search_eign_unknown = Search_engin(self.unknownfeature)  # 未知访客搜索
                                    self.search_eign_unknown.train()
                                    unknown_idx = self.search_eign_unknown.search(feature)
                                    unknown_idx = [unknown_idx[0][i] for i in range(unknown_idx.shape[1])]
                                    print(unknown_idx)
                                    uscore = 1 - spatial.distance.cosine(feature, self.unknownfeature[int(unknown_idx[0])])
                                    print(uscore)
                                    if uscore < unthresh: # 当天第一次识别出该未知人员
                                        self.unknownum = self.unknownum + 1
                                        currentunknownum = currentunknownum + 1
                                        #self.unknownlist[self.unknownum] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                                        cv2.imwrite('./image_saved/{}/unknown/{}_{}.jpg'.format(currentDate, currentTime,
                                                                                    self.unknownum), save_croped)
                                        self.unknownfeature = np.vstack((self.unknownfeature, feature))
                                        #print("第二个")
                                        #print(time.strftime('%H:%M:%S', time.localtime(time.time())))

                        if knownflag == 1:
                            #print(idname)
                            currentknownum = len(idname)  # 当前帧已知访客数量
                            #self.knownum = self.knownum + currentknownum
                            for i in range(currentknownum):
                                self.id_image = cv2.imread('./user_pic/{}.bmp'.format(idname[i]))
                                self.display_id_image(self.knownum)
                        if unknownflag == 1:
                            print("未知人总数:{}".format(self.unknownum))
                           # print("当前帧未知人数:{}".format(currentunknownum))
                            with open(self.unknownfeature_path, 'wb') as f:
                                self.unknownfeature = np.asarray(self.unknownfeature)
                                np.save(f, self.unknownfeature)
                            for i in range(currentunknownum):
                                self.id_image2 = cv2.imread('./image_saved/{}/unknown/{}_{}.jpg'.format(currentDate, currentTime, self.unknownum))
                                self.display_id_image2(self.unknownum)

                        #print(time.strftime('%H:%M:%S', time.localtime(time.time())))
                    else:

                        web.write_result("未检测到人脸", 'red')
                        web.exec_()

            except:
                    pass
        else:
            pass