def generate_frames_max_bbox(frames_path, frames_format, pts_paths, pts_formats, pts_names, save_path, proportion, figure_size, overwrite, save_original, render_options, only_ln=False, verbose=True): # find crop offset print('Computing max bounding box:') bounds_x = [] bounds_y = [] try: if len(os.listdir(pts_paths[0])) == 0: raise IndexError() except IndexError: if len(pts_paths) > 0: ms = 'The directory of landmarks {} is empty, returning.' print(ms.format(pts_paths[0])) return for s in mio.import_landmark_files(pts_paths[0] + '*' + pts_formats[0], verbose=verbose): min_b, max_b = s.lms.bounds() bounds_x.append(max_b[0] - min_b[0]) bounds_y.append(max_b[1] - min_b[1]) off1 = round(max(bounds_x) * (1. + proportion) / 2) off2 = round(max(bounds_y) * (1. + proportion) / 2) print('\nLoad images, crop and save:') try: from joblib import Parallel, delayed Parallel(n_jobs=-1, verbose=4)(delayed(_aux)(im, pts_paths, pts_names, pts_formats, save_path, save_original, off1, off2, figure_size, overwrite, render_options, only_ln=only_ln) for im in mio.import_images(frames_path + '*' + frames_format, verbose=False)); except: print('Sequential execution') for im in mio.import_images(frames_path + '*' + frames_format, verbose=verbose): _aux(im, pts_paths, pts_names, pts_formats, save_path, save_original, off1, off2, figure_size, overwrite, render_options, only_ln=only_ln);
def generate_frames_max_bbox(frames_path, frames_format, pts_paths, pts_formats, pts_names, save_path, proportion, figure_size, overwrite, save_original, render_options, only_ln=False, verbose=True): # find crop offset print('Computing max bounding box:') bounds_x = [] bounds_y = [] try: if len(os.listdir(pts_paths[0])) == 0: raise IndexError() except IndexError: if len(pts_paths) > 0: print('The directory of landmarks (%s) is empty, returning' % pts_paths[0]) return for s in mio.import_landmark_files(pts_paths[0] + '*.pts', verbose=verbose): min_b, max_b = s.lms.bounds() bounds_x.append(max_b[0] - min_b[0]) bounds_y.append(max_b[1] - min_b[1]) off1 = round(max(bounds_x) * (1. + proportion) / 2) off2 = round(max(bounds_y) * (1. + proportion) / 2) print('\nLoad images, crop and save:') try: from joblib import Parallel, delayed Parallel(n_jobs=-1, verbose=4)(delayed(_aux)(im, pts_paths, pts_names, pts_formats, save_path, save_original, off1, off2, figure_size, overwrite, render_options, only_ln=only_ln) for im in mio.import_images(frames_path + '*' + frames_format, verbose=False)); except: print('Sequential execution') for im in mio.import_images(frames_path + '*' + frames_format, verbose=verbose): _aux(im, pts_paths, pts_names, pts_formats, save_path, save_original, off1, off2, figure_size, overwrite, render_options, only_ln=only_ln);
def load_menpo_image_list( img_dir, train_crop_dir, img_dir_ns, mode, bb_dictionary=None, image_size=256, margin=0.25, bb_type='gt', test_data='full', augment_basic=True, augment_texture=False, p_texture=0, augment_geom=False, p_geom=0, verbose=False, return_transform=False): """load images from image dir to create menpo-type image list""" def crop_to_face_image_gt(img): return crop_to_face_image(img, bb_dictionary, gt=True, margin=margin, image_size=image_size, return_transform=return_transform) def crop_to_face_image_init(img): return crop_to_face_image(img, bb_dictionary, gt=False, margin=margin, image_size=image_size, return_transform=return_transform) def crop_to_face_image_test(img): return crop_to_face_image(img, bb_dictionary=None, margin=margin, image_size=image_size, return_transform=return_transform) def augment_menpo_img_ns_rand(img): return augment_menpo_img_ns(img, img_dir_ns, p_ns=1. * (np.random.rand() < p_texture)[0]) def augment_menpo_img_geom_rand(img): return augment_menpo_img_geom(img, p_geom=1. * (np.random.rand() < p_geom)[0]) if mode is 'TRAIN': if train_crop_dir is None: img_set_dir = os.path.join(img_dir, 'training') out_image_list = mio.import_images(img_set_dir, verbose=verbose, normalize=False) if bb_type is 'gt': out_image_list = out_image_list.map(crop_to_face_image_gt) elif bb_type is 'init': out_image_list = out_image_list.map(crop_to_face_image_init) else: img_set_dir = os.path.join(img_dir, train_crop_dir) out_image_list = mio.import_images(img_set_dir, verbose=verbose) # perform image augmentation if augment_texture and p_texture > 0: out_image_list = out_image_list.map(augment_menpo_img_ns_rand) if augment_geom and p_geom > 0: out_image_list = out_image_list.map(augment_menpo_img_geom_rand) if augment_basic: out_image_list = out_image_list.map(augment_face_image) else: # if mode is 'TEST', load test data if test_data in ['full', 'challenging', 'common', 'training', 'test']: img_set_dir = os.path.join(img_dir, test_data) out_image_list = mio.import_images(img_set_dir, verbose=verbose, normalize=False) if bb_type is 'gt': out_image_list = out_image_list.map(crop_to_face_image_gt) elif bb_type is 'init': out_image_list = out_image_list.map(crop_to_face_image_init) else: img_set_dir = os.path.join(img_dir, test_data+'*') out_image_list = mio.import_images(img_set_dir, verbose=verbose, normalize=False) out_image_list = out_image_list.map(crop_to_face_image_test) return out_image_list
def load_menpo_image_list_artistic_aug(img_dir, train_crop_dir, img_dir_ns, mode, bb_dictionary=None, image_size=256, margin=0.25, bb_type='gt', test_data='full', augment_basic=True, augment_texture=False, p_texture=0, augment_geom=False, p_geom=0): def crop_to_face_image_gt(img): return crop_to_face_image(img, bb_dictionary, gt=True, margin=margin, image_size=image_size) def crop_to_face_image_init(img): return crop_to_face_image(img, bb_dictionary, gt=False, margin=margin, image_size=image_size) def augment_menpo_img_ns_rand(img): return augment_menpo_img_ns(img, img_dir_ns, p_ns=1. * (np.random.rand() <= p_texture)) def augment_menpo_img_geom_rand(img): return augment_menpo_img_geom(img, p_geom=1. * (np.random.rand() <= p_geom)) if mode is 'TRAIN': img_set_dir = os.path.join(img_dir, train_crop_dir) out_image_list = mio.import_images(img_set_dir, verbose=True) if augment_texture: out_image_list = out_image_list.map(augment_menpo_img_ns_rand) if augment_geom: out_image_list = out_image_list.map(augment_menpo_img_geom_rand) if augment_basic: out_image_list = out_image_list.map(augment_face_image) else: img_set_dir = os.path.join(img_dir, test_data + '_set') out_image_list = mio.import_images(img_set_dir, verbose=True) if test_data in ['full', 'challenging', 'common', 'training', 'test']: if bb_type is 'gt': out_image_list = out_image_list.map(crop_to_face_image_gt) elif bb_type is 'init': out_image_list = out_image_list.map(crop_to_face_image_init) return out_image_list
def test_import_images_are_ordered_and_unduplicated(): # we know that import_images returns images in path order imgs = list(mio.import_images(mio.data_dir_path())) imgs_filenames = [i.path.stem for i in imgs] print(imgs_filenames) exp_imgs_filenames = ['breakingbad', 'einstein', 'lenna', 'menpo_thumbnail', 'takeo', 'tongue'] assert exp_imgs_filenames == imgs_filenames
def load_frgc(session_id, recreate_meshes=False, output_base_path='/vol/atlas/homes/pts08/', input_base_path='/vol/atlas/databases/frgc', max_images=None): previously_pickled_path = os.path.join( output_base_path, 'frgc_{0}_68_cleaned.pkl'.format(session_id)) abs_files_path = os.path.join(input_base_path, session_id, '*.abs') if not recreate_meshes and os.path.exists(previously_pickled_path): with open(previously_pickled_path) as f: images = cPickle.load(f) else: all_images = list(mio.import_images(abs_files_path, max_images=max_images)) images = [im for im in all_images if im.n_landmark_groups == 1] print '{0}% of the images had landmarks'.format( len(images) / float(len(all_images)) * 100) for i, im in enumerate(images): preprocess_image(im) print_replace_line( 'Image {0} of {1} cleaned'.format(i + 1, len(images))) # Only dump the saved images if we loaded all of them! if max_images is None: cPickle.dump(images, open(previously_pickled_path, 'wb'), protocol=2) return images
def load_n_create_generator(pattern, detector_name, group=None, overwrite=False): import menpo.io as mio from menpo.landmark import LandmarkGroup from menpo.model import PCAModel try: detector = _DETECTORS[detector_name]() except KeyError: detector_list = ', '.join(list(_DETECTORS.keys())) raise ValueError('Valid detector types are: {}'.format(detector_list)) print('Running {} detector on {}'.format(detector_name, pattern)) bboxes = [(img, detect_and_check(img, detector, group=group)) for img in mio.import_images(pattern, normalise=False, verbose=True)] # find all the detections that did not fail detections = filter(lambda x: x[1] is not None, bboxes) print('Creating a model out of {} detections.'.format(len(detections))) # normalize these to size [1, 1], centred on origin normed_detections = [ normalize(im.landmarks[group].lms.bounding_box()).apply(det) for im, det in detections ] # build a PCA model from good detections pca = PCAModel(normed_detections) mio.export_pickle(pca, '{}_gen.pkl'.format(detector_name), overwrite=overwrite)
def train(): path_to_images = 'lfpw/ceph_trainset/' training_images = [] for img in mio.import_images(path_to_images, verbose=True): # if img.n_channels == 3: # img = img.as_greyscale() img = img.crop_to_landmarks_proportion(0.2) d = img.diagonal() if d > 400: img = img.rescale(400.0 / d) training_images.append(img) # patch_aam = PatchAAM(training_images, group='PTS', patch_shape=[(15, 15), (23, 23)], # diagonal=200, scales=(0.5, 1.0), holistic_features=fast_dsift, # max_shape_components=60, max_appearance_components=200, # verbose=True) patch_aam = PatchAAM(training_images, group='PTS', patch_shape=[(16, 19), (19, 16)], diagonal=200, scales=(0.5, 1), holistic_features=fast_dsift, max_shape_components=74, max_appearance_components=175, verbose=True) fitter = LucasKanadeAAMFitter(patch_aam, lk_algorithm_cls=WibergInverseCompositional, n_shape=[10, 30], n_appearance=[40, 160]) mio.export_pickle(fitter, '26_img_35_pnt.pkl', overwrite=True) return fitter
def test_import_lazy_list(): from menpo.base import LazyList data_path = mio.data_dir_path() ll = mio.import_images(data_path) assert isinstance(ll, LazyList) ll = mio.import_landmark_files(data_path) assert isinstance(ll, LazyList)
def train_aic_rlms(trainset, output, n_train_imgs=None): training_images = [] # load landmarked images for i in mio.import_images(Path(trainset) / '*', verbose=True, max_images=n_train_imgs): # crop image i = i.crop_to_landmarks_proportion(0.5) labeller(i, 'PTS', face_ibug_68_to_face_ibug_66_trimesh) # convert it to greyscale if needed if i.n_channels == 3: i = i.as_greyscale(mode='average') # append it to the list training_images.append(i) offsets = np.meshgrid(range(-0, 1, 1), range(-0, 1, 1)) offsets = np.asarray([offsets[0].flatten(), offsets[1].flatten()]).T np.seterr(divide ='ignore') np.seterr(invalid ='ignore') unified = UnifiedAAMCLM(training_images, parts_shape=(17, 17), offsets=offsets, group = test_group, holistic_features=fast_dsift, diagonal=100, scales=(1, .5), max_appearance_components = min(50,int(n_train_imgs/2)), verbose=True) n_appearance=[min(25,int(n_train_imgs/2)), min(50,int(n_train_imgs/2))] fitter = UnifiedAAMCLMFitter(unified, algorithm_cls=AICRLMS, n_shape=[3, 12], n_appearance=n_appearance) return fitter
def generate_dataset(): with managed_dataset('lfpw-test') as p: for img in mio.import_images(p / '*.png', max_images=20, normalise=False, shuffle=True, landmark_resolver=_resolver): img.landmarks['gt'] = ibug_face_68(img.landmarks['gt'])[1] yield img.path.stem, img
def read_public_images(path_to_db, max_images=100, training_images=None, crop_reading=0.3, pix_thres=330, feat=None): """ Read images from public databases. The landmarks are expected to be in the same folder. :param path_to_db: Path to the folder of images. The landmark files are expected to be in the same folder. :param max_images: Max images that will be loaded from this database. Menpo will try to load as many as requested (if they exist). :param training_images: (optional) List of images to append the new ones. :param crop_reading: (optional) Amount of cropping the image around the landmarks. :param pix_thres: (optional) If the cropped image has a dimension bigger than this, it gets cropped to this diagonal dimension. :param feat: (optional) Features to be applied to the images before inserting them to the list. :return: List of menpo images. """ import menpo.io as mio if not (os.path.isdir(path_to_db)): raise RuntimeError( 'The path to the public DB images does not exist. Try with a valid path.' ) if feat is None: feat = no_op if training_images is None: training_images = [] for i in mio.import_images(path_to_db + '*', verbose=True, max_images=max_images): if not i.has_landmarks: continue i = crop_rescale_img(i, crop_reading=crop_reading, pix_thres=pix_thres) training_images.append(feat(i)) # append it to the list return training_images
def aam_features_rgb(fitter, img_dir='', isShown=False, save_path=''): image_paths = list(imutils.paths.list_images(img_dir)) image_paths.sort() images = mio.import_images(img_dir) result = [] for image, image_path in tqdm(zip(images, image_paths), total=len(image_paths)): aam_feat = {'shape': {}, 'texture': {}} # image.view() for channel, color in enumerate(['R', 'G', 'B']): # print(channel) grey_image = image.as_greyscale(mode='channel', channel=channel) # mage.view() fitting_result = fitting_img(grey_image, fitter) if isShown == True: fitting_result.view() plt.show() shape_param = {color: list(fitting_result.shape_parameters[-1])} # print(shape_param) aam_feat['shape'].update(shape_param) appearance_param = {color: list( fitting_result.appearance_parameters[-1])} aam_feat['texture'].update(appearance_param) aam_feat['shape'].update(dict.fromkeys(['R','G', 'B'], list( np.mean( list(aam_feat['shape'].values()), axis=0 ) ) )) d = [{"imagePath": image_path, "encoding": flattening_aam_feat(aam_feat), "shape": aam_feat['shape'], "texture": aam_feat['texture'] }] result.extend(d) if save_path != '': print("[INFO] serializing encodings...") f = open(save_path, "wb") f.write(pickle.dumps(result)) f.close()
def test_import_as_generator(): import types gen = mio.import_images(mio.data_dir_path(), as_generator=True) assert isinstance(gen, types.GeneratorType) gen = mio.import_landmark_files(mio.data_dir_path(), as_generator=True) assert isinstance(gen, types.GeneratorType)
def crop_img(img): detect_landmark(img) out_image_list = mio.import_images( '/home/KLTN_TheFaceOfArtFaceParsing/Updates/face_warp/input/', verbose='true') image = crop_img_facial(out_image_list[0]) save_image(image)
def test_import_lazy_list(): from menpo.base import LazyList data_path = mio.data_dir_path() ll = mio.import_images(data_path) assert isinstance(ll, LazyList) ll = mio.import_landmark_files(data_path) assert isinstance(ll, LazyList)
def test_import_images(): imgs = list(mio.import_images(mio.data_dir_path())) imgs_filenames = set(i.path.stem for i in imgs) exp_imgs_filenames = { 'einstein', 'takeo', 'breakingbad', 'lenna', 'menpo_thumbnail' } assert (len(exp_imgs_filenames - imgs_filenames) == 0)
def trainAAMObject(self): try : from menpo.feature import fast_dsift except : pass #detector = load_dlib_frontal_face_detector() def load_image(i): i = i.crop_to_landmarks_proportion(0.5) if i.n_channels == 3: i = i.as_greyscale() # This step is actually quite important! If we are using # an AAM and a PiecewiseAffine transform then we need # to ensure that our triangulation is sensible so that # we don't end up with ugly skinny triangles. Luckily, # we provide a decent triangulation in the landmarks # package. labeller(i, 'PTS', ibug_face_68_trimesh) return i training_images_path = Path(pathToTrainset) training_images = [load_image(i) for i in mio.import_images(training_images_path, verbose=True)] aam = HolisticAAM( training_images, group='ibug_face_68_trimesh', scales=(0.5, 1.0), diagonal=150, max_appearance_components=200, max_shape_components=20, verbose=True ) pickle.dump(aam, open(AAMFile, "wb"))
def img_pre(path_to_images, save_dir=None, propotion=0.2, scale=400.0, greyscale=False): import menpo.io as mio from menpo.visualize import print_progress if save_dir is not None: mk_dir(save_dir, 0) for image_path in path_to_images: for img in print_progress(mio.import_images(image_path, verbose=True)): if greyscale: # convert to greyscale if img.n_channels == 3: img = img.as_greyscale() # crop to landmarks bounding box with an extra 20% padding re_img = img.crop_to_landmarks_proportion(propotion) # # rescale image if its diagonal is bigger than 400 pixels # d = img.diagonal() # if d > scale: # img = img.rescale(scale / d) # save enhanced image with lable img_suffix = img.path.suffix lb_suffix = '.pts' new_image_name = '%s' % img.path.name.split('.')[0] img_path = os.path.join(save_dir, new_image_name + img_suffix) lb_path = os.path.join(save_dir, new_image_name + lb_suffix) mio.export_image(re_img, img_path, overwrite=True) mio.export_landmark_file(re_img.landmarks['PTS'], lb_path, overwrite=True)
def load_frgc(session_id, recreate_meshes=False, output_base_path=Path('/vol/atlas/homes/pts08/'), input_base_path=Path('/vol/atlas/databases/frgc'), max_images=None): previously_pickled_path = output_base_path / 'frgc_{0}_68_cleaned.pkl'.format(session_id) abs_files_path = input_base_path / session_id / '*.abs' if not recreate_meshes and previously_pickled_path.exists(): with open(str(previously_pickled_path)) as f: images = cPickle.load(f) else: # Add the custom ABS importer from menpo.io.input.extensions import image_types image_types['.abs'] = ABSImporter images = [] for i, im in enumerate(mio.import_images(abs_files_path, max_images=max_images, verbose=True)): if im.n_landmark_groups > 0: preprocess_image(im) images.append(im) # Only dump the saved images if we loaded all of them! if max_images is None: with open(str(previously_pickled_path), 'wb') as f: cPickle.dump(images, f, protocol=2) return images
def load_frgc(session_id, recreate_meshes=False, output_base_path='/vol/atlas/homes/pts08/', input_base_path='/vol/atlas/databases/frgc', max_images=None): previously_pickled_path = os.path.join( output_base_path, 'frgc_{0}_68_cleaned.pkl'.format(session_id)) abs_files_path = os.path.join(input_base_path, session_id, '*.abs') if not recreate_meshes and os.path.exists(previously_pickled_path): with open(previously_pickled_path) as f: images = cPickle.load(f) else: all_images = list( mio.import_images(abs_files_path, max_images=max_images)) images = [im for im in all_images if im.n_landmark_groups == 1] print '{0}% of the images had landmarks'.format( len(images) / float(len(all_images)) * 100) for i, im in enumerate(images): preprocess_image(im) print_replace_line('Image {0} of {1} cleaned'.format( i + 1, len(images))) # Only dump the saved images if we loaded all of them! if max_images is None: cPickle.dump(images, open(previously_pickled_path, 'wb'), protocol=2) return images
def load_n_create_generator(pattern, detector_name, group=None, overwrite=False): # from menpo.landmark import LandmarkGroup from menpo.model import PCAModel try: cur_detector = _DETECTORS[detector_name]() except KeyError: detector_list = ', '.join(list(_DETECTORS.keys())) raise ValueError('Valid detector types are: {}'.format(detector_list)) print('Running {} detector on {}'.format(detector_name, pattern)) bboxes = [ (img, detect_and_check(img, cur_detector, group=group)) for img in mio.import_images(pattern, normalise=False, verbose=True) ] # find all the detections that did not fail detections = list(filter(lambda x: x[1] is not None, bboxes)) print('Creating a model out of {} detections.'.format(len(detections))) # normalize these to size [1, 1], centred on origin normed_detections = [ normalize(im.landmarks[group].bounding_box()).apply(det) for im, det in detections ] # build a PCA model from good detections pca = PCAModel(normed_detections) mio.export_pickle(pca, '{}_gen.pkl'.format(detector_name), overwrite=overwrite)
def densereg_face_iterator(istraining, db='helen'): database_path = Path( '/vol/atlas/homes/yz4009/databases/DenseReg/FaceRegDataset') / db landmarks_path = Path('/vol/atlas/databases') / db image_folder = 'Images_Resized' uv_folder = 'Labels_Resized' z_folder = 'Labels_Resized' if istraining == 1: database_path = database_path / 'trainset' landmarks_path = landmarks_path / 'trainset' elif istraining == 0: database_path = database_path / 'testset' landmarks_path = landmarks_path / 'testset' for pimg in print_progress( mio.import_images(database_path / image_folder / image_folder)): image_name = pimg.path.stem # load iuv data labels = sio.loadmat( str(database_path / uv_folder / uv_folder / ('%s.mat' % image_name))) iuv = Image( np.stack([(labels['LabelsH_resized'] >= 0).astype(np.float32), labels['LabelsH_resized'], labels['LabelsV_resized']]).clip(0, 1)) # load lms data try: orig_image = mio.import_image(landmarks_path / ('%s.jpg' % image_name)) except: orig_image = mio.import_image(landmarks_path / ('%s.png' % image_name)) orig_image = orig_image.resize(pimg.shape) pimg.landmarks['JOINT'] = orig_image.landmarks['PTS'] pimg_data = utils.crop_image_bounding_box( pimg, pimg.landmarks['JOINT'].bounding_box(), [384, 384], base=256) pimg = pimg_data[0] iuv_data = utils.crop_image_bounding_box( iuv, pimg.landmarks['JOINT'].bounding_box(), [384, 384], base=256) iuv = iuv_data[0] yield { 'image': pimg, 'iuv': iuv, 'visible_pts': np.array(list(range(68))), 'marked_index': np.array(list(range(68))) }
def crop_face(): filePathSimple = NAME OF THE DIRECTOERY files = os.listdir(filePathSimple) filePath = THE PATH OF VIDEOs vids = import_videos(filePathSimple) detector = load_ffld2_frontal_face_detector() for utterance in print_progress(vids): # vids is a list with all videos fileName = files.pop(0) whole_path_of_video = filePath + "/" + fileName path_where_you_want_the_uncropped_images_to_be_stored = fileName + "_" + "uncropped" same_as_before_with_cropped_images = fileName + "_" + "cropped" # cropped_image_location = fileName + "_" + "cropped2" cropped_image_location = same_as_before_with_cropped_images print(fileName + " Cropping") fps_check = subprocess.check_output('ffprobe '+whole_path_of_video+' 2>&1 | grep fps',shell=True) fps_check = str(fps_check, 'utf-8') # fps = float(fps_check.split(' fps')[0].split(',')[-1][1:]) ## in our case we know fps=30 so you can just put this fps = 30 if not os.path.exists(path_where_you_want_the_uncropped_images_to_be_stored): os.makedirs(path_where_you_want_the_uncropped_images_to_be_stored) if not os.path.exists(same_as_before_with_cropped_images): os.makedirs(same_as_before_with_cropped_images) subprocess.call('ffmpeg -loglevel panic -i '+whole_path_of_video+' -vf fps='+ str(fps)+' '+path_where_you_want_the_uncropped_images_to_be_stored+'/%05d.jpg',shell=True) vv = mio.import_images(path_where_you_want_the_uncropped_images_to_be_stored+'/*.jpg') for cnt, im in enumerate(vv): name = '{0:05d}'.format(cnt+1) # i select to start the images names from 1 and not 0 lns = detector(im) if im.landmarks.n_groups == 0: # there are no detections continue if im.landmarks.n_groups == 1: im.constrain_landmarks_to_bounds() mio.export_image(im.crop_to_landmarks(), cropped_image_location+'/'+name+'.jpg', extension=None, overwrite=True) elif im.landmarks.n_groups > 1: for i in range(im.landmarks.n_groups): im.constrain_landmarks_to_bounds() mio.export_image(im.crop_to_landmarks(group='ffld2_'+str(i)), cropped_image_location+'/'+name+'_'+str(i)+'.jpg', extension=None, overwrite=True) subprocess.call("mv " + filePathSimple + "/" + fileName + " " + filePathSimple + "/Finished", shell=True) print(fileName + " Finished") print("All Done")
def generate_dataset(): with managed_dataset('lfpw-train') as p: for img in mio.import_images(p / '*.png', normalise=False, shuffle=True, landmark_resolver=_resolver): img.landmarks['gt'] = ibug_face_68(img.landmarks['gt'])[1] yield img.path.stem, img
def LoadDataset(self): trainset = os.path.join(self.dataset, 'trainset', '*') training_images = [ self.LoadImage(img) for img in menpoio.import_images(trainset, verbose=True) ] return training_images
def load_database(self, path_to_images, max_images=None): images = [] for i in mio.import_images(path_to_images, max_images=max_images, verbose=True): if i.n_channels == 3: i = i.as_greyscale(mode='luminosity') images.append(i) return images
def load_test_data(testset, n_test_imgs=None): test_images = [] for i in mio.import_images(Path(testset), verbose=True, max_images=n_test_imgs): i = i.crop_to_landmarks_proportion(0.5) labeller(i, 'PTS', face_ibug_68_to_face_ibug_66_trimesh) if i.n_channels == 3: i = i.as_greyscale(mode='average') test_images.append(i) return test_images
def test_import_images_are_ordered_and_unduplicated(): # we know that import_images returns images in path order imgs = list(mio.import_images(mio.data_dir_path())) imgs_filenames = [i.path.stem for i in imgs] print(imgs_filenames) exp_imgs_filenames = [ 'breakingbad', 'einstein', 'lenna', 'menpo_thumbnail', 'takeo', 'tongue' ] assert exp_imgs_filenames == imgs_filenames
def extract_features(img_dir, lock): print 'load images from ', img_dir p2pErrs = [] fitting_results = [] indexCount = 0 imageList = mio.import_images(img_dir, verbose=True) indexAll = len(imageList) for i in imageList: # input images with size of 256x256 if i.shape[0] != i.shape[1] or i.shape[0] != 256: zoomImg = scipy.ndimage.zoom(i.pixels, zoom=[1, 256 / float(i.shape[1]), 256 / float(i.shape[1])]) i.pixels = zoomImg # check whether the ground-truth is provided or not try: i.landmarks['PTS'] except: i.landmarks['PTS'] = fit_model.reference_shape # Estimation step, get response maps from FCN net.blobs['data'].data[...] = transformer.preprocess('data', np.rollaxis(i.pixels, 0, 3)) i.rspmap_data = np.array(net.forward()['upsample']) # zoom response maps # i.rspmap_data = scipy.ndimage.zoom(i.rspmap_data, zoom=[1, 1, float(i.height) / i.rspmap_data.shape[-2], # float(i.width) / i.rspmap_data.shape[-1]], order=1) # mode = 'nearest' gt_s = i.landmarks['PTS'].lms s = rspimage.initial_shape_fromMap(i) # fit image fr = fitter.fit_from_shape(i, s, gt_shape=gt_s) fitting_results.append(fr) # calculate point-to-point Normalized Mean Error Err = euclidean_distance_normalised_error(fr.shapes[-1], fr.gt_shape, distance_norm_f=inner_pupil) p2pErrs.append(Err) text_file = open(img_dir + '/' + i.path.stem + '.68pt', "w") np.savetxt(text_file, fr.shapes[-1].points, fmt='%d', newline='\n') print img_dir + '/' + i.path.stem + '.5pt' five_pt_text_file = open(img_dir + '/' + i.path.stem + '.5pt', "w") five_pt_array = gen_5pt(fr.shapes[-1].points) np.savetxt(five_pt_text_file, five_pt_array, fmt='%d', newline='\n') text_file.close() five_pt_text_file.close() indexCount = indexCount + 1 # sys.stdout.write('{} done;'.format(i.path.name)) sys.stdout.write('\r') sys.stdout.write('{}/{} Done; '.format(indexCount,indexAll)) sys.stdout.flush() p2pErrs = np.array(p2pErrs) print('NormalizedMeanError: {:.4f}'.format(average(p2pErrs)))
def read_images(img_glob, normalise): # Read the training set into memory. images = [] for img_orig in mio.import_images(img_glob, verbose=True, normalise=normalise): if not img_orig.has_landmarks: continue # Convert to greyscale and crop to landmarks. img = img_orig.as_greyscale(mode='average').crop_to_landmarks_proportion(0.5) #img = img.resize((MAX_FACE_WIDTH, img.shape[1]*(MAX_FACE_WIDTH/img.shape[0]))) images.append(img) return np.array(images)
def read_images(img_glob, normalise): # Read the training set into memory. images = [] for img_orig in mio.import_images(img_glob, verbose=True, normalise=normalise): if not img_orig.has_landmarks: continue # Convert to greyscale and crop to landmarks. img = img_orig.as_greyscale(mode='average').crop_to_landmarks_proportion_inplace(0.5) #img = img.resize((MAX_FACE_WIDTH, img.shape[1]*(MAX_FACE_WIDTH/img.shape[0]))) images.append(img) return np.array(images)
def load_images_test(paths, reference_shape, group=None, verbose=True, PLOT=False): """Loads and rescales input knn_2D to the diagonal of the reference shape. Args: paths: a list of strings containing the data directories. reference_shape (meanshape): a numpy array [num_landmarks, 2] group: landmark group containing the grounth truth landmarks. verbose: boolean, print debugging info. Returns: knn_2D: a list of numpy arrays containing knn_2D. shapes: a list of the ground truth landmarks. reference_shape (meanshape): a numpy array [num_landmarks, 2]. shape_gen: PCAModel, a shape generator. """ images = [] shapes = [] scales = [] # compute mean shape reference_shape = PointCloud(reference_shape) nameList = [] bbox = [] data = dict() for path in paths: if verbose: print('Importing data from {}'.format(path)) for im in mio.import_images(path, verbose=verbose, as_generator=True): # group = group or im.landmarks[group]._group_label group = group or im.landmarks.keys()[0] bb_root = im.path.parent.relative_to(im.path.parent.parent.parent) if 'set' not in str(bb_root): bb_root = im.path.parent.relative_to(im.path.parent.parent) im.landmarks['bb'] = mio.import_landmark_file( str( Path('bbs') / bb_root / (im.path.stem.replace(' ', '') + '.pts'))) nameList.append(str(im.path)) lms = im.landmarks['bb'].lms.points bbox.append([lms[0, 1], lms[2, 1], lms[0, 0], lms[1, 0]]) # bbox = np.array(bbox) # data['nameList'] = nameList # data['bbox'] = bbox # sio.savemat('ibug_data.mat', {'nameList':data['nameList'], 'bbox':data['bbox']}) # exit(0) im = im.crop_to_landmarks_proportion(0.3, group='bb') images.append(im) return images
def test_import_images(): imgs = list(mio.import_images(mio.data_dir_path())) imgs_filenames = set(i.path.stem for i in imgs) exp_imgs_filenames = { "einstein", "takeo", "tongue", "breakingbad", "lenna", "menpo_thumbnail", } assert exp_imgs_filenames == imgs_filenames
def load_images(img_path): imgs = [] print("Importing Images") for i in mio.import_images(str(img_path), verbose=True): # crop image i = i.crop_to_landmarks_proportion(0.1) # convert it to greyscale if needed if i.n_channels == 3: i = i.as_greyscale(mode="luminosity") imgs.append(i) return imgs
def AAMModel(path, max_shape=None, max_appearance=None): training_images = [] for img in print_progress(mio.import_images(path, verbose=True)): labeller(img, 'PTS', face_ibug_68_to_face_ibug_68_trimesh) training_images.append(img) aam_model = HolisticAAM(training_images, group='face_ibug_68_trimesh', scales=(0.5, 1.0), holistic_features=fast_dsift, verbose=True, max_shape_components=max_shape, max_appearance_components=max_appearance) return aam_model, training_images
def get_images_from_image_folder(top_dir, ext='.jpg', restrict_to_landmarked=False): images = [] folders = glob.glob(os.path.join(top_dir, '*')) for label, folder in enumerate(tqdm(folders)): for img in print_progress( mio.import_images(os.path.join(folder, '*' + ext), verbose=False)): images.append(img) if restrict_to_landmarked: images = [image for image in images if 'PTS' in image.landmarks.keys()] return images
def test_import_images_are_ordered_and_unduplicated(): # we know that import_images returns images in path order imgs = list(mio.import_images(mio.data_dir_path())) imgs_filenames = [i.path.stem for i in imgs] print(imgs_filenames) exp_imgs_filenames = [ "breakingbad", "einstein", "lenna", "menpo_thumbnail", "takeo", "tongue", ] assert exp_imgs_filenames == imgs_filenames
def load_database(path_to_images, crop_percentage, max_images=None): images = [] # load landmarked images for i in mio.import_images(path_to_images, max_images=max_images, verbose=True): # crop image i = i.crop_to_landmarks_proportion(crop_percentage) # convert it to grayscale if needed if i.n_channels == 3: i = i.as_greyscale(mode='luminosity') # append it to the list images.append(i) return images
def write_pkl(file_path, pkl_path, crop_scale=0.1, to_gray_scale=True, resize=False, resize_shape=(128, 128), resize_order=1, to_CNN=False): if os.path.exists(file_path): my_images = [] print('Digging into %s' % file_path) else: raise IOError('File path %s not found. Please have it checked' % file_path) exit() for i, image in enumerate(mio.import_images(file_path, verbose=True)): if image.has_landmarks and 'PTS' in image.landmarks.group_labels: new_my_image = my_image(image, crop_scale=crop_scale, to_gray_scale=to_gray_scale, resize=resize, resize_shape=resize_shape, resize_order=resize_order, to_CNN=to_CNN) my_images.append(new_my_image) # # Show images and landmarks # image_shown = utils.add_landmarks(new_my_image.image, new_my_image.landmarks) # cv2.imshow('image',cv2.cvtColor(image_shown, cv2.COLOR_RGB2BGR)) # cv2.waitKey(0) & 0xFF print('%d / %d in dataset' % (i, len(mio.import_images(file_path)))) else: print("Landmarks not found!!!") print('Finished loading %s' % file_path) print('Writing to %s, this may take quite a long time.' % pkl_path) mio.export_pickle(my_images, pkl_path) print('Finished writing to %s.' % pkl_path)
def test_trained_aam_default_dataset(self, i_image_count = 800): dataset = os.path.join( GetDirectory(__file__) , 'lfpw') Model = AAM(dataset, i_debug = True) testset = os.path.join(dataset, 'testset', '*') forward_backward_errors = [Model.FitAnnotatedImage(Model.LoadImage(img)) for img in menpoio.import_images(testset, max_images=800, verbose = True) ] # Ensure mean error < 0.1 - Experimentally derived err = 0 for error in forward_backward_errors: err = err + error.final_error() self.assertTrue( err / len(forward_backward_errors) < 0.1 )
def save_bounding_boxes(pattern, detector_type, group=None, sythesize_problematic=False, overwrite=False): import menpo.io as mio from menpo.landmark import LandmarkGroup from menpo.model import PCAModel try: detector = _DETECTORS[detector_type]() except KeyError: detector_list = ', '.join(list(_DETECTORS.keys())) raise ValueError('Valid detector types are: {}'.format(detector_list)) print('Running {} detector on {}'.format(detector_type, pattern)) bboxes = {img.path: detect_and_check(img, detector, group=group) for img in mio.import_images(pattern, normalise=False, verbose=True)} # find all the detections that failed problematic = filter(lambda x: x[1]['d'] is None, bboxes.items()) print('Failed to detect {} objects'.format(len(problematic))) if len(problematic) > 0 and sythesize_problematic: print('Learning detector traits and sythesizing fits for {} ' 'images'.format(len(problematic))) # get the good detections detections = filter(lambda x: x['d'] is not None, bboxes.values()) # normalize these to size [1, 1], centred on origin normed_detections = [normalize(r['gt']).apply(r['d']) for r in detections] # build a PCA model from good detections pca = PCAModel(normed_detections) for p, r in problematic: # generate a new bbox offset in the normalized space by using # our learnt PCA basis d = random_instance(pca) # apply an inverse transform to place it on the image bboxes[p]['d'] = normalize(r['gt']).pseudoinverse().apply(d) to_save = len(bboxes) if not sythesize_problematic: to_save = to_save - len(problematic) print('Saving out {} {} detections'.format(to_save, detector_type)) # All done, save out results for p, r in bboxes.items(): if r['d'] is not None: lg = LandmarkGroup.init_with_all_label(r['d']) mio.export_landmark_file(lg, p.parent / (p.stem + '_{}.ljson'.format(detector_type)), overwrite=overwrite)
def main_for_ps_detector(path_clips, in_bb_fol, out_bb_fol, out_model_fol, out_landmarks_fol, overwrite=False): # define a dictionary for the paths paths = {} paths['clips'] = path_clips paths['in_bb'] = path_clips + in_bb_fol # existing bbox of detection paths['out_bb'] = path_clips + out_bb_fol # save bbox of detection paths['out_lns'] = path_clips + out_landmarks_fol paths['out_model'] = mkdir_p(path_clips + out_model_fol) # path that trained models will be saved. # Log file output. log = mkdir_p(path_clips + 'logs' + sep) + datetime.now().strftime("%Y.%m.%d.%H.%M.%S") + '_2_ffld.log' sys.stdout = Logger(log) print_fancy('Training person specific model with FFLD') list_clips = sorted(os.listdir(path_clips + frames)) img_type = check_img_type(list_clips, path_clips + frames) negative_images = [i.as_greyscale(mode='channel', channel=1) for i in mio.import_images('/vol/atlas/homes/pts08/non_person_images', normalise=False, max_images=300)] [process_clip(clip_name, paths, img_type, negative_images, overwrite=overwrite) for clip_name in list_clips];
def load_database(path_to_images, save_path, db_name, crop_percentage, fast, group, verbose=False): # create filename if group is not None: filename = (db_name + '_' + group.__name__ + '_crop' + str(int(crop_percentage * 100))) else: filename = db_name + 'PTS' + '_crop' + str(int(crop_percentage * 100)) if fast: filename += '_menpofast.pickle' else: filename += '_menpo.pickle' save_path = os.path.join(save_path, filename) # check if file exists if file_exists(save_path): if verbose: print_dynamic('Loading images...') images = pickle_load(save_path) if verbose: print_dynamic('Images Loaded.') else: # load images images = [] for i in mio.import_images(path_to_images, verbose=verbose): if fast: i = convert_from_menpo(i) i.crop_to_landmarks_proportion_inplace(crop_percentage, group='PTS') if group is not None: labeller(i, 'PTS', group) if i.n_channels == 3: i = i.as_greyscale(mode='average') images.append(i) # save images pickle_dump(images, save_path) # return images return images
def read_public_images(path_to_db, max_images=100, training_images=None, crop_reading=0.3, pix_thres=330, feat=None): """ Read images from public databases. The landmarks are expected to be in the same folder. :param path_to_db: Path to the folder of images. The landmark files are expected to be in the same folder. :param max_images: Max images that will be loaded from this database. Menpo will try to load as many as requested (if they exist). :param training_images: (optional) List of images to append the new ones. :param crop_reading: (optional) Amount of cropping the image around the landmarks. :param pix_thres: (optional) If the cropped image has a dimension bigger than this, it gets cropped to this diagonal dimension. :param feat: (optional) Features to be applied to the images before inserting them to the list. :return: List of menpo images. """ import menpo.io as mio if not(os.path.isdir(path_to_db)): raise RuntimeError('The path to the public DB images does not exist. Try with a valid path.') if feat is None: feat = no_op if training_images is None: training_images = [] for i in mio.import_images(path_to_db + '*', verbose=True, max_images=max_images): if not i.has_landmarks: continue i = crop_rescale_img(i, crop_reading=crop_reading, pix_thres=pix_thres) training_images.append(feat(i)) # append it to the list return training_images
def test_import_images(): imgs = list(mio.import_images(mio.data_dir_path())) imgs_filenames = set(i.path.stem for i in imgs) exp_imgs_filenames = {'einstein', 'takeo', 'breakingbad', 'lenna'} assert(len(exp_imgs_filenames - imgs_filenames) == 0)
def test_import_as_generator(): import types gen = mio.import_images(mio.data_dir_path(), as_generator=True) assert isinstance(gen, types.GeneratorType) gen = mio.import_landmark_files(mio.data_dir_path(), as_generator=True) assert isinstance(gen, types.GeneratorType)
def test_shuffle_kwarg_true_calls_shuffle(mock): list(mio.import_images(mio.data_dir_path(), shuffle=True)) assert mock.called
def test_import_images_wrong_path_raises_value_error(): list(mio.import_images('asldfjalkgjlaknglkajlekjaltknlaekstjlakj'))
def test_import_images(): imgs = list(mio.import_images(mio.data_dir_path())) imgs_filenames = set(i.path.stem for i in imgs) exp_imgs_filenames = {'einstein', 'takeo', 'tongue', 'breakingbad', 'lenna', 'menpo_thumbnail'} assert exp_imgs_filenames == imgs_filenames
def test_import_image(): img_path = os.path.join(mio.data_dir_path(), 'einstein.jpg') mio.import_images(img_path)
def LoadDataset(self): trainset = os.path.join(self.dataset,'trainset','*') training_images = [self.LoadImage(img) for img in menpoio.import_images(trainset, verbose = True) ] return training_images
def test_import_images(): imgs_glob = os.path.join(pio.data_dir_path(), '*') imgs = list(pio.import_images(imgs_glob)) imgs_filenames = set(i.ioinfo.filename for i in imgs) exp_imgs_filenames = {'einstein', 'takeo', 'breakingbad', 'lenna'} assert(len(exp_imgs_filenames - imgs_filenames) == 0)
def load_database(database_path, bounding_boxes=None, db_loading_options=None, verbose=False): r""" Loads the database images, crops them and converts them. Parameters ---------- database_path: str The path of the database images. db_loading_options: dictionary, optional A dictionary with options related to image loading. If None, the default options will be used. This is an example of the dictionary with the default options: training_options = {'crop_proportion': 0.1, 'convert_to_grey': True, } crop_proportion (float) defines the additional padding to be added all around the landmarks bounds when the images are cropped. It is defined as a proportion of the landmarks' range. convert_to_grey (boolean)defines whether the images will be converted to greyscale. Default: None verbose: boolean, optional If True, it prints a progress percentage bar. Default: False Returns ------- images: list of :class:MaskedImage objects A list of the loaded images. Raises ------ ValueError Invalid path given ValueError No {files_extension} files in given path """ # check input options if db_loading_options is None: db_loading_options = {} # check given path database_path = os.path.abspath(os.path.expanduser(database_path)) if os.path.isdir(database_path) is not True: raise ValueError('Invalid path given') # create final path final_path = os.path.join(database_path, '*') # get options crop_proportion = db_loading_options.pop('crop_proportion', 0.5) convert_to_grey = db_loading_options.pop('convert_to_grey', True) # load images images = [] for i in mio.import_images(final_path, verbose=verbose): # If we have bounding boxes then we need to make sure we crop to them! # If we don't crop to the bounding box then we might crop out part of # the image the bounding box belongs to. landmark_group_label = None if bounding_boxes is not None: fname = i.ioinfo.filename + i.ioinfo.extension landmark_group_label = 'bbox' i.landmarks[landmark_group_label] = bounding_boxes[fname].detector # crop image i.crop_to_landmarks_proportion_inplace(crop_proportion, group=landmark_group_label) # convert it to greyscale if needed if convert_to_grey and i.n_channels == 3: i = i.as_greyscale(mode='luminosity') # append it to the list images.append(i) if verbose: print("\nAssets loaded.") return images
def test_import_images_negative_max_images(): list(mio.import_images(mio.data_dir_path(), max_images=-2))
def load_images(paths, group=None, verbose=True): """Loads and rescales input images to the diagonal of the reference shape. Args: paths: a list of strings containing the data directories. reference_shape: a numpy array [num_landmarks, 2] group: landmark group containing the grounth truth landmarks. verbose: boolean, print debugging info. Returns: images: a list of numpy arrays containing images. shapes: a list of the ground truth landmarks. reference_shape: a numpy array [num_landmarks, 2]. shape_gen: PCAModel, a shape generator. """ images = [] shapes = [] bbs = [] reference_shape = PointCloud(build_reference_shape(paths)) for path in paths: if verbose: print('Importing data from {}'.format(path)) for im in mio.import_images(path, verbose=verbose, as_generator=True): group = group or im.landmarks[group]._group_label bb_root = im.path.parent.relative_to(im.path.parent.parent.parent) if 'set' not in str(bb_root): bb_root = im.path.parent.relative_to(im.path.parent.parent) im.landmarks['bb'] = mio.import_landmark_file(str(Path( 'bbs') / bb_root / (im.path.stem + '.pts'))) im = im.crop_to_landmarks_proportion(0.3, group='bb') im = im.rescale_to_pointcloud(reference_shape, group=group) im = grey_to_rgb(im) images.append(im.pixels.transpose(1, 2, 0)) shapes.append(im.landmarks[group].lms) bbs.append(im.landmarks['bb'].lms) train_dir = Path(FLAGS.train_dir) mio.export_pickle(reference_shape.points, train_dir / 'reference_shape.pkl', overwrite=True) print('created reference_shape.pkl using the {} group'.format(group)) pca_model = detect.create_generator(shapes, bbs) # Pad images to max length max_shape = np.max([im.shape for im in images], axis=0) max_shape = [len(images)] + list(max_shape) padded_images = np.random.rand(*max_shape).astype(np.float32) print(padded_images.shape) for i, im in enumerate(images): height, width = im.shape[:2] dy = max(int((max_shape[1] - height - 1) / 2), 0) dx = max(int((max_shape[2] - width - 1) / 2), 0) lms = shapes[i] pts = lms.points pts[:, 0] += dy pts[:, 1] += dx lms = lms.from_vector(pts) padded_images[i, dy:(height+dy), dx:(width+dx)] = im return padded_images, shapes, reference_shape.points, pca_model
def test_import_images_zero_max_images(): # different since the conditional 'if max_assets' is skipped, # thus all images might be imported. list(mio.import_images(mio.data_dir_path(), max_images=0))
def test_import_mesh(): obj_path = os.path.join(mio.data_dir_path(), 'bunny.obj') mio.import_images(obj_path)