Ejemplo n.º 1
0
def save_aligned(old_fn, new_fn):
    old_fn = os.path.join(extracted_folder, old_fn)
    _, _, landmarks = get_central_face_attributes(old_fn)

    unaligned_img = read_image_from_file(old_fn)
    img = align_face(unaligned_img, landmarks)  # BGR

    # img = align_face(old_fn, landmarks)
    new_fn = os.path.join('images', new_fn)
    cv.imwrite(new_fn, img)
Ejemplo n.º 2
0
def crop_one_image(filepath, oldkey, newkey):
    new_fn = filepath.replace(oldkey, newkey)
    tardir = os.path.dirname(new_fn)
    if not os.path.isdir(tardir):
        os.makedirs(tardir)

    if not os.path.exists(new_fn):
        is_valid, bounding_boxes, landmarks = get_central_face_attributes(filepath)
        if is_valid:
            img = align_face(filepath, landmarks)
            cv.imwrite(new_fn, img)
Ejemplo n.º 3
0
def detect_face(data):
    from utils import get_central_face_attributes, align_face
    src_path = data['src_path']
    dst_path = data['dst_path']
    with torch.no_grad():
        has_face, bboxes, landmarks = get_central_face_attributes(src_path)
        if has_face:
            img = align_face(src_path, landmarks)
            cv.imwrite(dst_path, img)

    return True
Ejemplo n.º 4
0
def process():
    subjects = [
        d for d in os.listdir('data/lfw_funneled')
        if os.path.isdir(os.path.join('data/lfw_funneled', d))
    ]
    assert (len(subjects) == 5749), "Number of subjects is: {}!".format(
        len(subjects))

    file_names = []
    for i in tqdm(range(len(subjects))):
        sub = subjects[i]
        folder = os.path.join('data/lfw_funneled', sub)
        files = [
            f for f in os.listdir(folder)
            if os.path.isfile(os.path.join(folder, f))
            and f.lower().endswith('.jpg')
        ]
        for file in files:
            filename = os.path.join(folder, file)
            file_names.append({
                'filename': filename,
                'class_id': i,
                'subject': sub
            })

    assert (len(file_names) == 13233), "Number of files is: {}!".format(
        len(file_names))

    samples = []
    for item in tqdm(file_names):
        filename = item['filename']
        class_id = item['class_id']
        sub = item['subject']

        try:
            bboxes, landmarks = get_central_face_attributes(filename)

            samples.append({
                'class_id': class_id,
                'subject': sub,
                'full_path': filename,
                'bounding_boxes': bboxes,
                'landmarks': landmarks
            })
        except KeyboardInterrupt:
            raise
        except Exception as err:
            print(err)

    with open(lfw_pickle, 'wb') as file:
        save = {'samples': samples}
        pickle.dump(save, file, pickle.HIGHEST_PROTOCOL)
Ejemplo n.º 5
0
def process():
    #전체 img폴더 불러오기
    subjects = [
        d for d in os.listdir('data/lfw_funneled')
        if os.path.isdir(os.path.join('data/lfw_funneled', d))
    ]
    #개수 안맞으면 오류
    assert (len(subjects) == 5749), "Number of subjects is: {}!".format(
        len(subjects))

    file_names = []
    for i in range(len(subjects)):
        sub = subjects[i]  #각 이미지 폴더 넣기
        folder = os.path.join('data/lfw_funneled', sub)
        #각 폴더별 jpg 파일 files에 넣기
        files = [
            f for f in os.listdir(folder)
            if os.path.isfile(os.path.join(folder, f))
            and f.lower().endswith('.jpg')
        ]
        #jpg 마다 파일경로 레이블 폴더이름 넣기
        for file in files:
            filename = os.path.join(folder, file)
            file_names.append({
                'filename': filename,
                'class_id': i,
                'subject': sub
            })
    #file_names에는 전체 폴더의 jpg파일들 다 들어감
    assert (len(file_names) == 13233), "Number of files is: {}!".format(
        len(file_names))

    #전체 jpg파일 항목 별로 나누기
    samples = []
    for item in file_names:
        filename = item['filename']
        class_id = item['class_id']
        sub = item['subject']

        #mtcnn으로 crop
        is_valid, bounding_boxes, landmarks = get_central_face_attributes(
            filename)

        #crop된 이미지로 다시 file 레이블 만들기
        if is_valid:
            samples.append(#file_names ->samples
                {'class_id': class_id, 'subject': sub, 'full_path': filename, 'bounding_boxes': bounding_boxes,
                 'landmarks': landmarks})
    #pickle파일로 재저장
    with open(lfw_pickle, 'wb') as file:
        save = {'samples': samples}
        pickle.dump(save, file, pickle.HIGHEST_PROTOCOL)
Ejemplo n.º 6
0
    def crop(self, folder):
        if os.path.isdir(folder):
            files = os.listdir(folder)
            if not os.path.exists(folder + '_crop'):
                os.makedirs(folder + '_crop')
        else:
            raise ValueError("Folder is not exist")

        for file in files:
            filepath = os.path.join(folder, file)
            new_fn = os.path.join(folder + '_crop', file)
            bounding_boxes, landmarks = get_central_face_attributes(filepath)
            img = align_face(filepath, landmarks)
            cv2.imwrite(new_fn, img)
Ejemplo n.º 7
0
def make_pickle():
    data = []
    model_load()
    classes = [
        ide for ide in os.listdir(kndir)
        if os.path.isdir(os.path.join(kndir, ide))
    ]  #[family,friends]
    for ide in classes:
        files = [kndir + ide + '/' + img
                 for img in os.listdir(kndir + ide)]  #[fullpaths]
        for file in files:
            is_valid, bounding_boxes, landmarks = get_central_face_attributes(
                file)
            if is_valid:
                img = get_image(file, landmarks, transformer)
                data.append({'fullpath': file, 'identity': ide, 'img': img})
    with open(knpkl, 'wb') as f:
        pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
Ejemplo n.º 8
0
def save_aligned(old_fn, new_fn):
    old_fn = os.path.join('data/lfw_funneled', old_fn)
    _, landmarks = get_central_face_attributes(old_fn)
    img = align_face(old_fn, landmarks)
    new_fn = os.path.join('images', new_fn)
    cv.imwrite(new_fn, img)
import cv2 as cv

from utils import image_aug, get_central_face_attributes, align_face

if __name__ == "__main__":
    filename = 'data/lfw_funneled/Aaron_Eckhart/Aaron_Eckhart_0001.jpg'
    print(filename)
    img = cv.imread(filename)  # BGR
    cv.imwrite('1.jpg', img)

    is_valid, bounding_boxes, landmarks = get_central_face_attributes(filename)
    img = align_face(filename, landmarks)
    cv.imwrite('2.jpg', img)

    img = image_aug(img)  # RGB
    cv.imwrite('3.jpg', img)

    img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    cv.imwrite('4.jpg', img)