def save_to_list_file(allfaces, lst_file, image_style_dir, annotation_dir, face_indexes, use_front, use_box):
  save_faces = []
  for index in face_indexes:
    face = allfaces[index]
    if use_front == False or face.check_front():
      save_faces.append( face )
  print ('Prepare to save {} face images into {}'.format(len(save_faces), lst_file))

  lst_file = open(lst_file, 'w')
  all_face_sizes = []
  for face in save_faces:
    image_path = face.image_path
    sub_dir, base_name = image_path.split('/')
    cannot_dir = osp.join(annotation_dir, sub_dir)
    cannot_path = osp.join(cannot_dir, base_name.split('.')[0] + '-{}.pts'.format(face.face_id))
    if not osp.isdir(cannot_dir): os.makedirs(cannot_dir)
    image_path = osp.join(image_style_dir, image_path)
    assert osp.isfile(image_path), 'The image [{}/{}] {} does not exsit'.format(index, len(save_faces), image_path)

    pts_str = datasets.PTSconvert2str( face.landmarks.T )
    pts_file = open(cannot_path, 'w')
    pts_file.write('{}'.format(pts_str))
    pts_file.close()

    box_str, face_size = face.get_face_size(use_box)

    lst_file.write('{} {} {} {}\n'.format(image_path, cannot_path, box_str, face_size))
    all_face_sizes.append( face_size )
  lst_file.close()

  all_faces = np.array( all_face_sizes )
  print ('all faces : mean={}, std={}'.format(all_faces.mean(), all_faces.std()))
Exemple #2
0
def main():
    #Open the sqlite database
    conn = sqlite3.connect(sqlit_path)
    c = conn.cursor()

    #Creating the query string for retriving: roll, pitch, yaw and faces position
    #Change it according to what you want to retrieve
    select_string = "faceimages.filepath, faces.face_id, facerect.x, facerect.y, facerect.w, facerect.h"
    from_string = "faceimages, faces, facerect"
    where_string = "faces.file_id = faceimages.file_id and faces.face_id = facerect.face_id"
    query_string = "SELECT " + select_string + " FROM " + from_string + " WHERE " + where_string

    tables_string = "SELECT name FROM sqlite_master WHERE type='table'"
    table_list = list(c.execute(tables_string))
    answers = list(c.execute(query_string))
    all_faces = []
    for query in c.execute(query_string):
        all_faces.append(AFLWFace(query))
    print('Finish load all faces : {}'.format(len(all_faces)))

    for face in all_faces:
        faceid = face.face_id
        select_string = "featurecoords.face_id, featurecoords.feature_id, featurecoords.x, featurecoords.y, featurecoords.annot_type_id"
        from_string = "featurecoords"
        where_string = "featurecoords.face_id = {}".format(faceid)
        query_string = "SELECT " + select_string + " FROM " + from_string + " WHERE " + where_string
        landmarks = list(c.execute(query_string))
        face.set_landmarks(landmarks)
    c.close()

    print('Finish load all facial landmarks : {}'.format(len(all_faces)))
    front_faces, other_faces = [], []
    for index, face in enumerate(all_faces):
        image_path = face.image_path
        image_path = osp.join(image_dir, image_path)
        face.from21to19()
        if osp.isfile(image_path):
            if face.check_front():
                front_faces.append(face)
            else:
                other_faces.append(face)
    print('Front faces : {}, Other faces : {}'.format(len(front_faces),
                                                      len(other_faces)))
    front_length = 1165
    train_length, test_length = 20000, 4396

    np.random.seed(10)
    random_indexes = np.random.permutation(len(front_faces))
    # Get the testing frontal faces
    indicators = np.zeros(len(front_faces), dtype='bool')
    indicators[random_indexes[:front_length]] = True
    test_fron_faces, others = [], []
    for index, face in enumerate(front_faces):
        if indicators[index]:
            test_fron_faces.append(face)
        else:
            others.append(face)
    new_test_length = len(others) + len(other_faces) - train_length
    np.random.seed(19)
    random_indexes = np.random.permutation(len(other_faces))
    indicators = np.zeros(len(other_faces), dtype='bool')
    indicators[random_indexes[:new_test_length]] = True

    train_faces, test_other_faces = [], []
    for index, face in enumerate(other_faces):
        if indicators[index] == False:
            train_faces.append(face)
        else:
            test_other_faces.append(face)
    train_faces = train_faces + others
    print('Train : {}, test front : {}, others : {}'.format(
        len(train_faces), len(test_fron_faces), len(test_other_faces)))
    np.random.seed(10)

    all_face_sizes = []

    train_save_list = open(osp.join(SAVE_DIR, 'train.' + SUFFIX), 'w')
    for face in train_faces:
        image_path = face.image_path
        sub_dir, base_name = image_path.split('/')
        cannot_dir = osp.join(annot_dir, sub_dir)
        cannot_path = osp.join(
            cannot_dir,
            base_name.split('.')[0] + '-{}.pts'.format(face.face_id))
        if not osp.isdir(cannot_dir): os.makedirs(cannot_dir)
        image_path = osp.join(image_dir, image_path)
        assert osp.isfile(
            image_path), 'The image [{}/{}] {} does not exsit'.format(
                index, len(all_faces), image_path)

        pts_str = datasets.PTSconvert2str(face.landmarks.T)
        pts_file = open(cannot_path, 'w')
        pts_file.write('{}'.format(pts_str))
        pts_file.close()

        box_str = return_box(cannot_path, face.face_rect)

        train_save_list.write('{} {} {} {}\n'.format(image_path, cannot_path,
                                                     box_str, face.face_size))
        all_face_sizes.append(face.face_size)
    train_save_list.close()

    test_save_list = open(osp.join(SAVE_DIR, 'test.front.' + SUFFIX), 'w')
    for face in test_fron_faces:
        image_path = face.image_path
        sub_dir, base_name = image_path.split('/')
        cannot_dir = osp.join(annot_dir, sub_dir)
        cannot_path = osp.join(
            cannot_dir,
            base_name.split('.')[0] + '-{}.pts'.format(face.face_id))
        if not osp.isdir(cannot_dir): os.makedirs(cannot_dir)
        image_path = osp.join(image_dir, image_path)
        assert osp.isfile(
            image_path), 'The image [{}/{}] {} does not exsit'.format(
                index, len(all_faces), image_path)

        pts_str = datasets.PTSconvert2str(face.landmarks.T)
        pts_file = open(cannot_path, 'w')
        pts_file.write('{}'.format(pts_str))
        pts_file.close()

        box_str = return_box(cannot_path, face.face_rect)

        test_save_list.write('{} {} {} {}\n'.format(image_path, cannot_path,
                                                    box_str, face.face_size))
        all_face_sizes.append(face.face_size)
    test_save_list.close()

    test_other_faces = test_other_faces + test_fron_faces
    test_save_list = open(osp.join(SAVE_DIR, 'test.' + SUFFIX), 'w')
    for face in test_other_faces:
        image_path = face.image_path
        sub_dir, base_name = image_path.split('/')
        cannot_dir = osp.join(annot_dir, sub_dir)
        cannot_path = osp.join(
            cannot_dir,
            base_name.split('.')[0] + '-{}.pts'.format(face.face_id))
        if not osp.isdir(cannot_dir): os.makedirs(cannot_dir)
        image_path = osp.join(image_dir, image_path)
        assert osp.isfile(
            image_path), 'The image [{}/{}] {} does not exsit'.format(
                index, len(all_faces), image_path)

        pts_str = datasets.PTSconvert2str(face.landmarks.T)
        pts_file = open(cannot_path, 'w')
        pts_file.write('{}'.format(pts_str))
        pts_file.close()

        box_str = return_box(cannot_path, face.face_rect)

        test_save_list.write('{} {} {} {}\n'.format(image_path, cannot_path,
                                                    box_str, face.face_size))
        all_face_sizes.append(face.face_size)
    test_save_list.close()

    all_faces = np.array(all_face_sizes)
    print('all faces : mean={}, std={}'.format(all_faces.mean(),
                                               all_faces.std()))