Beispiel #1
0
def init_facebank(conf, learner, mtcnn):
    if UPDATE:
        targets, names = prepare_facebank(conf, learner.model, mtcnn, tta=TTA)
        print('facebank updated')
    else:
        targets, names = load_facebank(conf)
        print('facebank loaded')

    return targets, names
Beispiel #2
0
 def update_old_facebank(self, stream_url):
     self.stream_url = stream_url
     self.cap = cv2.VideoCapture(self.stream_url)
     self.cap.set(cv2.CAP_PROP_POS_MSEC, random.randint(3, 1000))
     self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 2)
     self.targets, self.names = prepare_facebank(self.conf,
                                                 self.learner.model,
                                                 self.mtcnn,
                                                 tta=True)
     print('facebank updated')
Beispiel #3
0
def train():
    conf = get_config(False)
    mtcnn = MTCNN()
    #print('mtcnn loaded')
    learner = face_learner(conf, True)
    learner.load_state(conf, 'cpu_final.pth', True, True)
    learner.model.eval()
    print('learner loaded')
    targets, names = prepare_facebank(conf, learner.model, mtcnn)
    return {'_result': 'success', '_message': 'Model Is Updated'}
Beispiel #4
0
 def OnUpdateRegister(self):
     self.initDatabase(update=1)
     self.targets, self.names = prepare_facebank(conf, learner.model, mtcnn,
                                                 True)
     for i in range(len(self.targets)):
         id_, unit, name, gender = self.names[i + 1].split('_')
         self.insertRow([id_, unit, name, gender], 1)
     print('人脸数据更新成功')
     self.infoText.AppendText('[%s]' % getDateAndTime(hms=1) +
                              '人脸数据库已更新\r\n')
     self.loadDataBase(1)
Beispiel #5
0
def fn_face_verify_module():
    mtcnn = MTCNN()
    print('mtcnn loaded')
    learner = face_learner(conf, True)
    learner.threshold = args.threshold
    if conf.device.type == 'cpu':
        learner.load_state(conf, 'cpu_final.pth', True, True)
    else:
        learner.load_state(conf, 'final.pth', True, True)
    learner.model.eval()
    print('learner loaded')

    if args.update:
        targets, names = prepare_facebank(conf,
                                          learner.model,
                                          mtcnn,
                                          tta=args.tta)
        print('facebank updated')
    else:
        targets, names = load_facebank(conf)
        print('facebank loaded')

    isSuccess, frame = cap.read()
    if isSuccess:
        try:
            image = Image.fromarray(frame)
            bboxes, faces = mtcnn.align_multi(image, conf.face_limit,
                                              conf.min_face_size)
            bboxes = bboxes[:, :
                            -1]  # shape:[10,4],only keep 10 highest possibiity faces
            bboxes = bboxes.astype(int)
            bboxes = bboxes + [-1, -1, 1, 1]  # personal choice
            results, score = learner.infer(conf, faces, targets, args.tta)
            for idx, bbox in enumerate(bboxes):
                if args.score:
                    frame = draw_box_name(
                        bbox,
                        names[results[idx] + 1] + '_{:.2f}'.format(score[idx]),
                        frame)
                else:
                    frame = draw_box_name(bbox, names[results[idx] + 1], frame)
        except:
            print('detect error')

        cv2.imshow('face Capture', frame)

    if args.save:
        video_writer.write(frame)
Beispiel #6
0
def register(user_id):
    data_path = Path('data')
    save_path = data_path / 'facebank' / user_id
    fetch_path = data_path / 'dataset' / user_id
    images = load_images_from_folder(fetch_path)
    print(images)
    if not save_path.exists():
        save_path.mkdir()

    mtcnn = MTCNN()
    count = 0
    face_id = user_id
    count = 0
    for img in images:
        frame = img
        p = Image.fromarray(frame[..., ::-1])
        try:
            warped_face = np.array(mtcnn.align(p))[..., ::-1]
            cv2.imwrite(
                "data/facebank/" + str(face_id) + '/' + str(face_id) + '_' +
                str(count) + ".jpg", warped_face)
            count += 1
            #cv2.imwrite(str(save_path/'{}.jpg'.format(str(datetime.now())[:-7].replace(":","-").replace(" ","-"))), warped_face)
        except:
            result = {
                "_result": "Error",
                "_message": "Unable to detect the face"
            }
    if count == len(images):
        result = {
            "_result": "success",
            "_message": "User Registered Successfully"
        }

    conf = get_config(False)
    learner = face_learner(conf, True)
    learner.load_state(conf, 'cpu_final.pth', True, True)
    learner.model.eval()
    #print('learner loaded')
    targets, names = prepare_facebank(conf, learner.model, mtcnn, user_id)
    #print('facebank updated')
    return result
Beispiel #7
0
 async def update_facebank(self, files, stream_url):
     self.stream_url = stream_url
     self.cap = cv2.VideoCapture(self.stream_url)
     self.cap.set(cv2.CAP_PROP_POS_MSEC, random.randint(3, 1000))
     self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 2)
     shutil.rmtree(FACEBANK_DIR, ignore_errors=False, onerror=None)
     os.makedirs(FACEBANK_DIR)
     for file in files:
         content = await file.read()
         nparr = np.fromstring(content, np.uint8)
         img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
         boxes, faces, face_ids, np_faces = self.detect_yolo_gpu(img)
         for idx, face in enumerate(faces):
             with open(
                     os.path.join(FACEBANK_DIR,
                                  str(idx) + "_" + file.filename),
                     "wb") as buffer:
                 shutil.copyfileobj(face, buffer)
     self.targets, self.names = prepare_facebank(self.conf,
                                                 self.learner.model,
                                                 self.mtcnn,
                                                 tta=True)
     print('facebank updated')
def predict():

    #img=img_path#注意修改

    parser = argparse.ArgumentParser(description='for face verification')
    parser.add_argument("-s",
                        "--save",
                        help="whether save",
                        action="store_true")
    parser.add_argument('-th',
                        '--threshold',
                        help='threshold to decide identical faces',
                        default=1.54,
                        type=float)
    parser.add_argument("-u",
                        "--update",
                        default=True,
                        help="whether perform update the facebank",
                        action="store_true")
    parser.add_argument("-tta",
                        "--tta",
                        default=False,
                        help="whether testCode time augmentation",
                        action="store_true")
    parser.add_argument("-c",
                        "--score",
                        default=True,
                        help="whether show the confidence score",
                        action="store_true")
    parser.add_argument('--img_path',
                        '-p',
                        default='1.jpg',
                        type=str,
                        help='input the name of the recording person')
    args = parser.parse_args()
    mtcnn = MTCNN()
    conf = get_config(False)
    learner = face_learner(conf, True)
    learner.threshold = args.threshold
    image = Image.open(
        'D:/code/Python/InsightFace-刘悦/InsightFace_Pytorch-master/1.jpg')

    if conf.device.type == 'cpu':
        learner.load_state(conf, 'ir_se50.pth', True, True)
    else:
        learner.load_state(conf, 'ir_se50.pth', True, True)
    learner.model.eval()
    print('learner loaded')

    if args.update:
        targets, names = prepare_facebank(conf,
                                          learner.model,
                                          mtcnn,
                                          tta=args.tta)
        print('facebank updated')
    else:
        targets, names = load_facebank(conf)
        print('facebank loaded')

    try:
        # image = Image.fromarray(img)
        # 利用mtcnn网络,对齐
        bboxes, faces = mtcnn.align_multi(image, conf.face_limit,
                                          conf.min_face_size)
        bboxes = bboxes[:, :
                        -1]  # shape:[10,4],only keep 10 highest possibiity faces
        bboxes = bboxes.astype(int)
        bboxes = bboxes + [-1, -1, 1, 1]  # personal choice
        results, score = learner.infer(conf, faces, targets, args.tta)
        print(results, score)
        for idx, bbox in enumerate(bboxes):
            print(names[results[idx] + 1])
            res = "name:" + names[results[idx] + 1]
    except:
        print('detect')
        res = "unknow"
    return res
        receptive_field_list=cfg.param_receptive_field_list,
        receptive_field_stride=cfg.param_receptive_field_stride,
        bbox_small_list=cfg.param_bbox_small_list,
        bbox_large_list=cfg.param_bbox_large_list,
        receptive_field_center_start=cfg.param_receptive_field_center_start,
        num_output_scales=cfg.param_num_output_scales)
    return face_detector


if __name__ == '__main__':
    args = args_parser()
    model = face_model.FaceModel(args)
    conf = get_config(training=False)

    targets, names = prepare_facebank(conf=conf,
                                      model=model,
                                      args=args,
                                      tta=True)
    if not os.path.exists(conf.demo):
        os.mkdir(conf.demo)

    cap = cv2.VideoCapture(str(args.file_name))

    cap.set(cv2.CAP_PROP_POS_MSEC, args.begin * 1000)

    fps = cap.get(cv2.CAP_PROP_FPS)
    video_writer = cv2.VideoWriter(
        str(conf.facebank_path / '{}.avi'.format(args.save_name)),
        cv2.VideoWriter_fourcc(*'XVID'), int(fps), (1280, 720))
    # for file in glob.glob(args.folder + "/*.{}".format(args.extension)):
    while cap.isOpened():
        print('READING----------------------------')
Beispiel #10
0
    parser.add_argument("-f",
                        "--facebank",
                        type=str,
                        help="dir of facebank picture",
                        default='facebank')

    args = parser.parse_args()

    conf = get_config(False)
    mtcnn = load_MTCNN()
    learner = load_Learner(conf, args)
    conf.facebank_path = conf.data_path / args.facebank

    start = time.time()
    if args.update:
        targets, names = prepare_facebank(conf, learner.model, mtcnn, False)
        print('facebank updated')
    else:
        targets, names = load_facebank(conf)
        print('facebank loaded')
    print(f'WorkingTime[Load facebank]: {time.time()-start}')

    print("Names")

    # list image directory
    img_list = glob(args.image_dir + '**/*.jpg')
    acc = 0
    detect_err = 0
    fails = []
    print(f"{'Found':^15}{'Name':^20}{'Result':^15}{'Score':^15}")
         conf,
         '2019-03-19-20-18_accuracy:0.9307142857142857_step:300234_None.pth',
         False, True)
 learner.model.eval()
 print('learner loaded')
 '''vgg2_fp, vgg2_fp_issame = get_val_pair(conf.emore_folder, 'vgg2_fp')
 accuracy, best_threshold, roc_curve_tensor = learner.evaluate(conf, vgg2_fp, vgg2_fp_issame, nrof_folds=10, tta=True)
 print('vgg2_fp - accuray:{}, threshold:{}'.format(accuracy, best_threshold))
 exit(0)'''
 df = pd.read_csv(args.csv)
 imlst = df.groupby('classnm')['imgfile'].apply(
     lambda x: x.tolist()).to_dict()
 if args.update:
     targets, ftoid, idinfo = prepare_facebank(conf,
                                               imlst,
                                               learner.model,
                                               mtcnn,
                                               tta=args.tta,
                                               save=True)
     print('facebank updated')
 else:
     targets, ftoid, idinfo = load_facebank(conf)
     print('facebank loaded')
 faces = []
 predfns = []
 with open(args.file) as f:
     imgfiles = list(map(str.strip, f.readlines()))
     for imgfn in imgfiles:
         try:
             face = Image.open(imgfn)
         except:
             print('cannot open query image file {}'.format(imgfn))
Beispiel #12
0
def face_detection(video_path, result_csv_path):

    conf = get_config(False)
    mtcnn = MTCNN(select_largest=False, keep_all=True)
    print('mtcnn loaded')
    
    learner = face_learner(conf, True)
    learner.threshold = 0.5
    if conf.device.type == 'cpu':
        learner.load_state(conf, 'cpu_final.pth', True, True)
    else:
        learner.load_state(conf, 'final.pth', True, True)
    learner.model.eval()
    print('learner loaded')
    
    targets, names = prepare_facebank(conf, learner.model, mtcnn, tta = True)
    print('facebank updated')
        
    cap = cv2.VideoCapture(video_path)
    cap.set(cv2.CAP_PROP_POS_MSEC, 0)
    
    fps = cap.get(cv2.CAP_PROP_FPS)
    #video writer
    #video_writer = cv2.VideoWriter(str(conf.facebank_path/'{}.avi'.format(args.save_name)),
#                                    cv2.VideoWriter_fourcc(*'XVID'), int(fps), (int(cap.get(3)),int(cap.get(4))))
    #video_writer = cv2.VideoWriter(str(conf.facebank_path/'{}.avi'.format(args.save_name)),
    #                               cv2.CAP_OPENCV_MJPEG, int(fps), (int(cap.get(3)),int(cap.get(4))))
    

    #### csv
    df = pd.DataFrame(columns=['frame_number', 'ID', 'LT', 'RB', 'score'])

    
    framecounter = 0
    while cap.isOpened():
        isSuccess,frame = cap.read()
        if isSuccess:            
#             image = Image.fromarray(frame[...,::-1]) #bgr to rgb
            image = Image.fromarray(frame)
            try:
                bboxes, faces = mtcnn.align_multi(image, conf.face_limit, 16)
                # mtcnn 에서 검출된 얼굴 BB 와 5point landmark
            except:
                bboxes = []
                faces = []
            if len(bboxes) == 0:
                print('no face')
                # continue
            else:
                bboxes = bboxes[:,:-1] #shape:[10,4],only keep 10 highest possibiity faces
                bboxes = bboxes.astype(int)
                bboxes = bboxes + [-1,-1,1,1] # personal choice
                # 사람 identification   
                results, score = learner.infer(conf, faces, targets, True)
                for idx,bbox in enumerate(bboxes):
                    #frame = draw_box_name(bbox, names[results[idx] + 1] + '_{:.2f}'.format(score[idx]), frame)
                    df = df.append({'frame_number':framecounter, 'ID':names[results[idx] + 1], 'LT':(bbox[0],bbox[1]), 'RB':(bbox[2],bbox[3]), 'score':'{:.2f}'.format(score[idx])}, ignore_index=True)

            #video_writer.write(frame)
            #print('{0:.2f}' .format(framecounter/duration*100))
            framecounter +=1
        else:
            break
      
    cap.release()
    #video_writer.release()
    df.to_csv(result_csv_path, index=False)
Beispiel #13
0
    def test(self,conf,img_dir,update=False,view_score=False,view_error=False):
        #Load models
        mtcnn = MTCNN()
        learner = face_learner(conf, True)
        if conf.device.type == 'cpu':
            learner.load_state(conf,'cpu_final.pth',True,True)
        else:
            learner.load_state(conf,'final.pth',True,True)
        learner.model.eval()

        #Load Facebank
        if update:
            targets, names = prepare_facebank(conf, learner.model, mtcnn, False)
            print('facebank updated')
        else:
            targets, names = load_facebank(conf)
            print('facebank loaded')

        #Load Image list
        img_list = glob(img_dir + '**/*.jpg')
        acc = 0
        detect_err=0
        fails = []
        print(f"{'Found':^15}{'Name':^20}{'Result':^15}{'Score':^15}")
        pbar = enumerate(img_list)
        pbar = tqdm(pbar, total = len(img_list))
        for i, x in pbar:
            preds = []
            label = str(os.path.dirname(x))
            label = os.path.basename(label)
            image = Image.open(x)
            frame = cv2.imread(x,cv2.IMREAD_COLOR)
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

            try:
                bboxes, faces = mtcnn.align_multi(image, conf.face_limit, conf.min_face_size)
                bboxes = bboxes[:,:-1] #shape:[10,4],only keep 10 highest possibiity faces
                bboxes = bboxes.astype(int)
                bboxes = bboxes + [-1,-1,1,1] # personal choice    
                results, score = learner.infer(conf, faces, targets, False)
                for idx,bbox in enumerate(bboxes):
                    print(f'{Label}: {score[idx]}')
                    if view_score:
                        frame = draw_box_name(bbox, names[results[idx] + 1] + '_{:.2f}'.format(score[idx]), frame)
                    else:
                        frame = draw_box_name(bbox, names[results[idx] + 1], frame)
                    preds.append(names[results[idx]+1])

                if label in preds:
                    acc += 1
                else:
                    fails.append([label,preds])
                    # Image.fromarray(frame,'RGB').show()
            except Exception as ex:
                fails.append([label,ex])
                detect_err += 1

            f = len(bboxes)
            tf = str(True if label in preds else False)
            t = f'{f:^15}{label:^20}{tf:^15}{acc/(i+1):^15.4}'
            pbar.set_description(t)
        
        if detect_err>0:
            print(f'Detect Error: {detect_err}')
            if view_error:
                pp(fails)
            else:
                print(f'If you want to see details, make veiw_error True.')

        print(f'Accuracy: {acc/len(img_list)}')
    if conf.device.type == 'cpu':
        learner.load_state(conf, 'cpu_final.pth', True, True)
    else:
        learner.load_state(conf, 'final.pth', True, True)
    learner.model.eval()
    print('learner loaded')

    # todo, modify this
    filepath = './input.txt'
    # read files
    train_dict, test_dict, output_dir = get_inputs(filepath)

    if args.update:
        targets, names = prepare_facebank(conf,
                                          learner.model,
                                          mtcnn,
                                          tta=args.tta,
                                          load_from_custom_dir=True,
                                          dir_name_dict=train_dict)
        print('facebank updated')
    else:
        targets, names = load_facebank(conf)
        print('facebank loaded')

    id_results = []
    random_output_idx = 0
    for tc_id in test_dict:
        tc_path = test_dict[tc_id]
        result = str(tc_id)
        image = cv2.imread(tc_path, cv2.IMREAD_COLOR)  # queryImage
        try:
            #           image = Image.fromarray(frame[...,::-1]) #bgr to rgb
Beispiel #15
0
    conf = get_config(False)

    update = False

    learner = face_learner(conf, True)

    if conf.device.type == 'cpu':
        learner.load_state(conf, '2019-11-11-08-11.pth', True, True)
    else:
        learner.load_state(conf, 'model_2019-11-11-08-11.pth', True, True)
    learner.model.eval()
    print('learner loaded')

    if update:
        targets, names = prepare_facebank(conf, learner.model)
        print('facebank updated')
    else:
        targets, names = load_facebank(conf)
        print('facebank loaded')
    imgs = []
    names = []

    testlist = []
    learner.threshold = 0.25
    with open(conf.testlist_path, 'r') as f:
        testlist = f.readlines()
        f.close()
    # print(testlist)
    acc = 0
    total = 0
Beispiel #16
0
def updateDatabase():
    return prepare_facebank(conf, learner.model, mtcnn, tta="store_true")
Beispiel #17
0
    mtcnn = MTCNN()
    print('mtcnn loaded')

    learner = face_learner(conf, True)
    learner.threshold = args.threshold
    if conf.device.type == 'cpu':
        learner.load_state(conf, 'cpu_final.pth', True, True)
    else:
        learner.load_state(conf, 'final.pth', True, True)
    learner.model.eval()
    print('learner loaded')

    if args.update:
        targets, names = prepare_facebank(conf,
                                          learner.model,
                                          mtcnn,
                                          tta=args.tta)
        print('facebank updated')
    else:
        targets, names = load_facebank(conf)
        print('facebank loaded')

    #cap = cv2.VideoCapture(str(conf.facebank_path/args.file_name))
    cap = cv2.VideoCapture("1.mp4")

    cap.set(cv2.CAP_PROP_POS_MSEC, args.begin * 1000)

    fps = cap.get(cv2.CAP_PROP_FPS)
    video_writer = cv2.VideoWriter(
        str(conf.facebank_path / '{}.avi'.format(args.save_name)),
        cv2.VideoWriter_fourcc(*'XVID'), int(fps), (1280, 720))
Beispiel #18
0
 def update_facebank(self,conf):
     mtcnn = MTCNN()
     learner = face_learner(conf,True)
     learner.load_state(conf,'final.pth',True,True)
     learner.model.eval()
     _, _ = prepare_facebank(conf,learner.model,mtcnn,False)