Example #1
0
    def inference(self,conf,img):
        mtcnn = MTCNN()
        learner = face_learner(conf,True)
        learner.load_state(conf,'final.pth',True,True)
        learner.model.eval()
        targets, names = load_facebank(conf)
        
        image = Image.open(img)
        frame = cv2.imread(img,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)
            name = names[results[0]+1]
            frame = draw_box_name(bboxes[0], name, frame)
        except Exception as ex:
            name = "Can't detect face."
            h, w, c = frame.shape
            bbox = [int(h*0.5),int(w*0.5),int(h*0.5),int(w*0.5)]
            frame = draw_box_name(bbox, name, frame)
            
        return name, frame
Example #2
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
Example #3
0
def recognize(path):
    conf = get_config(False)
    mtcnn = MTCNN()
    print('mtcnn loaded')

    learner = face_learner(conf, True)
    learner.threshold = 1.35
    learner.load_state(conf, 'cpu_final.pth', True, True)
    learner.model.eval()
    print('learner loaded')
    targets, names = load_facebank(conf)
    print(names)
    print('facebank loaded')
    count = 0
    while True:
        frame = cv2.imread(path)
        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)

            for idx, bbox in enumerate(bboxes):
                frame = draw_box_name(
                    bbox, names[results[idx] + 1] +
                    '_{:.2f}'.format(100 - score[idx]), frame)
                result = {
                    "_result": "success",
                    "User Verified with": {
                        "confidence": '{:.2f}%'.format(100 - score[idx]),
                        "userid": names[results[idx] + 1],
                        "error": "Success"
                    }
                }
                accuracy.append('{:.2f}'.format(100 - score[idx]))
                user.append(names[results[idx] + 1])
                print(names[results[idx] + 1],
                      '{:.2f}'.format(100 - score[idx]))
            count = 1
        except:
            print('detect error')
        if count > 0:
            break
    return result
Example #4
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)
Example #5
0
 def __init__(self):
     self.stream_url = "https://demo.bahien.com/live/stream/playlist.m3u8"
     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.conf = get_config(False)
     self.mtcnn = MTCNN()
     self.learner = face_learner(self.conf, True)
     self.learner.threshold = THRESHOLD
     self.learner.load_state(self.conf, 'final.pth', True, True)
     self.learner.model.eval()
     print('learner loaded')
     self.targets, self.names = load_facebank(self.conf)
     print('facebank loaded')
     self.net, self.ln = yolo_detection.init_net()
     print('yolo model loaded')
     self.model = Darknet(config_file)
     self.model.load_weights(weight_file)
     self.model.cuda()
Example #6
0
    def __init__(self, register=False):
        self.conf = get_config()
        self.conf.device = torch.device(
            "cuda:0" if torch.cuda.is_available() else "cpu")
        self.mtcnn = My_Function_lib.MTCNN()
        print('mtcnn loaded')

        self.learner = face_learner(self.conf, True)
        if self.conf.device.type == 'cpu':
            self.learner.load_state(self.conf, 'best.pth', True)
        else:
            self.learner.load_state(self.conf, 'final.pth', True)
        self.learner.model.eval()
        print('learner loaded')
        if register == False:
            self.targets, self.names = load_facebank(self.conf)
        print('facebank updated')
        self.process_this_frame = True
        self.Img = None
Example #7
0
    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))

    if args.duration != 0:
        i = 0
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
 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))
             continue
         try:
             face = mtcnn.align(face)
         except:
             print('mtcnn failed for {}'.format(imgfn))
Example #10
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)}')