示例#1
0
def test_single(img):
    if type(img) == str:
        img = cv2.imread(img)

    face_detector = FaceDetector(
        name='mobilenet',
        weight_path='../Retinaface/weights/mobilenet.pth',
        device='cuda')
    age_gender_detector = AgeGender(name='full',
                                    weight_path='./weights/ShufflenetFull.pth',
                                    device='cuda')

    faces, boxes, scores, landmarks = face_detector.detect_align(img)

    genders = None
    ages = None
    if len(faces.shape) > 1:
        genders, ages = age_gender_detector.detect(faces)
        for i, b in enumerate(boxes):
            cv2.putText(img, f'{genders[i]},{ages[i]}',
                        (int(b[0]), int(b[1] - 10)), cv2.FONT_HERSHEY_SIMPLEX,
                        1.1, [0, 200, 0], 3)
            cv2.rectangle(img, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])),
                          (255, 0, 0), 3)

    return img, genders, ages
示例#2
0
                        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)
    conf.use_mobilfacenet = args.mobilenet
    face_rec = face_learner(conf, inference=True)
    face_rec.threshold = args.threshold
    face_rec.model.eval()

    face_detector = FaceDetector(
        name='mobilenet',
        weight_path='Retinaface/weights/mobilenet.pth',
        device=conf.device)
    age_gender_detector = AgeGender(
        name='full',
        weight_path='AgeGender/weights/ShufflenetFull.pth',
        device=conf.device)
    emotion_detector = EmotionDetector(
        name='densnet121',
        weight_path='FacialExpression/weights/densnet121.pth',
        device=conf.device)

    if args.update:
        targets, names = update_facebank(conf,
                                         face_rec.model,
                                         face_detector,
                                         tta=args.tta)
示例#3
0
parser = argparse.ArgumentParser(description='take a picture')
parser.add_argument('--path',
                    '-p',
                    default='unknown',
                    type=str,
                    help='path of dir of person images')
args = parser.parse_args()
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 !!')
save_path = Path(f'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')
示例#4
0
from Retinaface.Retinaface import FaceDetector
from FacialExpression.FaceExpression import EmotionDetector
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(0)
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 b in boxes:
            cv2.putText(frame, emotions[0], (int(b[0]), int(b[1]) - 5),
                        cv2.FONT_HERSHEY_SIMPLEX, 1.1, [0, 200, 0], 3)
            cv2.rectangle(frame, (int(b[0]), int(b[1])),
                          (int(b[2]), int(b[3])), (255, 0, 0), 3)

        for p in landmarks:
            for i in range(5):
                cv2.circle(frame, (p[i][0], p[i][1]), 3, (0, 255, 0), -1)
示例#5
0
                    default='unknown',
                    type=str,
                    help='input the name of the recording person')
args = parser.parse_args()

save_path = Path('data/facebank') / args.name
if not save_path.exists():
    save_path.mkdir()

# init camera
cap = cv2.VideoCapture(1)
cap.set(3, 1280)
cap.set(4, 720)
# init detector
detector = FaceDetector(name='resnet',
                        weight_path='Retinaface/weights/resnet50.pth',
                        device='cuda')
count = 4
while cap.isOpened():
    _, frame = cap.read()
    frame = cv2.putText(frame,
                        f'Press t to take {count} pictures, then finish...',
                        (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}/{args.name}_{count}.jpg', faces[0])
            if count <= 0: