Exemple #1
0
def add_from_folder(folder_path='./', person_name='unknow'):

    print('only a face in each image and all image from the same person')
    dir_path = Path(folder_path)
    if not dir_path.is_dir():
        exit('dir does not exists !!')

    # create facebank folder if is not exists
    save_path = Path(os.path.dirname(os.path.realpath(__file__)),
                     'models/data/facebank')
    if not save_path.exists():
        save_path.mkdir()

    save_path = Path(f'{save_path}/{person_name}')
    if not save_path.exists():
        save_path.mkdir()
    print('loading ...')
    # init detector
    detector = FaceDetector()

    counter = 0
    for img_path in dir_path.iterdir():
        img = cv2.imread(str(img_path))
        if img is None:
            raise Exception('this image has a problem: ', img_path)
        face = detector.detect_align(img)[0].cpu().numpy()
        if len(face.shape) > 1:
            save_name = f'{save_path}/{dir_path.name}_{counter}.jpg'
            cv2.imwrite(save_name, face[0])
            counter += 1
        else:
            print(img_path, 'in this image did not detect any face')
    print('images saved in this path: ', save_path)
Exemple #2
0
def add_from_webcam(person_name='unknow', camera_index=0):
    print('loading ...')
    # create facebank folder if is not exists

    save_path = Path(os.path.dirname(os.path.realpath(__file__)),
                     'models/data/facebank')
    if not save_path.exists():
        save_path.mkdir()

    # create a new folder with (name) for new person
    save_path = save_path / person_name
    if not save_path.exists():
        save_path.mkdir()

    print('for exit: use q keyword')
    # init camera
    cap = cv2.VideoCapture(camera_index)
    cap.set(3, 1280)
    cap.set(4, 720)
    # init detector
    detector = FaceDetector()
    count = 4
    print('type q for exit')
    while cap.isOpened():
        ret, frame = cap.read()
        if ret == False:
            raise Exception(
                'the camera not recognized: change camera_index param to ' +
                str(0 if camera_index == 1 else 1))

        frame = cv2.putText(frame, f'Press t to take {count} pictures',
                            (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 100, 0),
                            3, cv2.LINE_AA)

        if cv2.waitKey(1) & 0xFF == ord('t'):
            count -= 1
            faces = detector.detect_align(frame)[0].cpu().numpy()
            if len(faces.shape) > 1:
                cv2.imwrite(f'{save_path}/{person_name}_{count}.jpg', faces[0])
                if count <= 0:
                    break
            else:
                print('there is not face in this frame')

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        cv2.imshow("add new person from camera", frame)

    cap.release()
    cv2.destroyAllWindows()
    print('images saved in this path: ', save_path)
Exemple #3
0
class WebcamAgeGenderEstimator:
    def __init__(
        self,
        device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")):
        print('loading ...')
        self.face_detector = FaceDetector(device=device)
        self.age_gender_detector = AgeGenderEstimator(device=device)

    def run(self, camera_index=0):
        cap = cv2.VideoCapture(camera_index)
        cap.set(3, 1280)
        cap.set(4, 720)
        print('type q for exit')
        while cap.isOpened():
            ret, frame = cap.read()
            if ret == False:
                raise Exception(
                    'the camera not recognized: change camera_index param to '
                    + str(0 if camera_index == 1 else 1))
            faces, boxes, scores, landmarks = self.face_detector.detect_align(
                frame)
            if len(faces.shape) > 1:
                genders, ages = self.age_gender_detector.detect(faces)
                for i, b in enumerate(boxes):
                    special_draw(frame,
                                 b,
                                 landmarks[i],
                                 name=genders[i] + ' ' + str(ages[i]))

            cv2.imshow('frame', frame)
            if cv2.waitKey(1) == ord('q'):
                break

        cv2.destroyAllWindows()
Exemple #4
0
    def __init__(
        self,
        update=True,
        tta=True,
        device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")):
        print('loading ...')
        self.tta = tta
        self.detector = FaceDetector(device=device)
        self.conf = get_config()
        self.conf.device = device
        recognizer = FaceRecognizer(self.conf)
        recognizer.model.eval()
        self.recognizer = recognizer

        if update:
            self.targets, self.names = update_facebank(self.conf,
                                                       recognizer.model,
                                                       self.detector,
                                                       tta=self.tta)
        else:
            self.targets, self.names = load_facebank(self.conf)
Exemple #5
0
 def __init__(
     self,
     device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")):
     print('loading ...')
     self.face_detector = FaceDetector(face_size=(224, 224), device=device)
     self.emotion_detector = EmotionDetector(device=device)
                        "--update",
                        default=True,
                        help="whether perform update the facebank")
    parser.add_argument("-tta",
                        "--tta",
                        default=True,
                        help="whether test time augmentation")
    parser.add_argument('-m',
                        '--mobilenet',
                        default=False,
                        help="use mobilenet for backbone")
    args = parser.parse_args()

    conf = get_config(training=False)
    detector = FaceDetector(name='mobilenet',
                            weight_path='../Retinaface/weights/mobilenet.pth',
                            device=conf.device)
    conf.use_mobilfacenet = args.mobilenet
    face_rec = FaceRecognizer(conf, inference=True)
    face_rec.threshold = args.threshold
    face_rec.model.eval()

    if args.update:
        targets, names = update_facebank(conf,
                                         face_rec.model,
                                         detector,
                                         tta=args.tta)
    else:
        targets, names = load_facebank(conf)

    # init camera
Exemple #7
0
from facelib import FaceDetector
from facelib import EmotionDetector
from facelib import special_draw
import cv2

face_detector = FaceDetector(name='mobilenet',
                             weight_path='../Retinaface/weights/mobilenet.pth',
                             device='cuda',
                             face_size=(224, 224))
emotion_detector = EmotionDetector(name='densnet121',
                                   weight_path='weights/densnet121.pth',
                                   device='cuda')

vid = cv2.VideoCapture(1)
vid.set(3, 1280)
vid.set(4, 720)
while True:
    ret, frame = vid.read()
    faces, boxes, scores, landmarks = face_detector.detect_align(frame)
    if len(faces.shape) > 1:
        emotions, emo_probs = emotion_detector.detect_emotion(faces)
        for i, b in enumerate(boxes):
            special_draw(frame,
                         b,
                         landmarks[i],
                         name=emotions[i],
                         score=emo_probs[i])

    cv2.imshow('frame', frame)
    if cv2.waitKey(1) == ord('q'):
        break
Exemple #8
0
 def __init__(
     self,
     device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")):
     print('loading ...')
     self.face_detector = FaceDetector(device=device)
     self.age_gender_detector = AgeGenderEstimator(device=device)
Exemple #9
0
class WebcamVerify:
    """
    WebcamVerify: face verfication with cv2
    
    if you add new person in to facebank
    pass update True
    """
    def __init__(
        self,
        update=True,
        tta=True,
        device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")):
        print('loading ...')
        self.tta = tta
        self.detector = FaceDetector(device=device)
        self.conf = get_config()
        self.conf.device = device
        recognizer = FaceRecognizer(self.conf)
        recognizer.model.eval()
        self.recognizer = recognizer

        if update:
            self.targets, self.names = update_facebank(self.conf,
                                                       recognizer.model,
                                                       self.detector,
                                                       tta=self.tta)
        else:
            self.targets, self.names = load_facebank(self.conf)

    def run(self, camera_index=0):
        if len(self.targets) < 1:
            raise Exception(
                "you don't have any person in facebank: add new person with 'add_from_webcam' or 'add_from_folder' function"
            )

        cap = cv2.VideoCapture(camera_index)
        cap.set(3, 1280)
        cap.set(4, 720)
        # frame rate 6 due to my laptop is quite slow...
        print('type q for exit')
        while cap.isOpened():
            ret, frame = cap.read()
            if ret == False:
                raise Exception(
                    'the camera not recognized: change camera_index param to '
                    + str(0 if camera_index == 1 else 1))
            faces, boxes, scores, landmarks = self.detector.detect_align(frame)
            if len(faces.shape) > 1:
                results, score = self.recognizer.infer(self.conf,
                                                       faces,
                                                       self.targets,
                                                       tta=self.tta)
                for idx, bbox in enumerate(boxes):
                    special_draw(frame, bbox, landmarks[idx],
                                 self.names[results[idx] + 1], score[idx])
            cv2.imshow('face Capture', frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        cap.release()
        cv2.destroyAllWindows()
print('only a face in each image and all image from the same person')

dir_path = Path(args.path)
if not dir_path.is_dir():
    exit('dir does not exists !!')

# create facebank folder if is not exists
save_path = Path('models/data/facebank')
if not save_path.exists():
    save_path.mkdir()

save_path = Path(f'models/data/facebank/{dir_path.name}')
if not save_path.exists():
    save_path.mkdir()

# init detector
detector = FaceDetector(name='mobilenet',
                        weight_path='../Retinaface/weights/mobilenet.pth',
                        device='cuda')

counter = 0
for img_path in dir_path.iterdir():
    img = cv2.imread(str(img_path))
    face = detector.detect_align(img)[0].cpu().numpy()
    if len(face.shape) > 1:
        save_name = f'{save_path}/{dir_path.name}_{counter}.jpg'
        cv2.imwrite(save_name, face[0])
        counter += 1
    else:
        print(img_path, 'in this image did not detect any face')