From 994d74feae29f2577bc04e10dd4bafbfb3dc8e83 Mon Sep 17 00:00:00 2001 From: adamhrv Date: Tue, 18 Dec 2018 01:15:36 +0100 Subject: set jitter 25 --- megapixels/commands/cv/face_landmarks_3d.py | 96 +++++++++++++++++++++++++++++ megapixels/commands/cv/face_pose.py | 2 +- megapixels/commands/cv/face_vector.py | 2 +- megapixels/commands/cv/faces_to_3dlm.py | 96 ----------------------------- megapixels/commands/datasets/lookup.py | 4 +- megapixels/commands/datasets/s3_sync.py | 6 +- megapixels/commands/demo/face_search.py | 26 ++++---- 7 files changed, 120 insertions(+), 112 deletions(-) create mode 100644 megapixels/commands/cv/face_landmarks_3d.py delete mode 100644 megapixels/commands/cv/faces_to_3dlm.py (limited to 'megapixels/commands') diff --git a/megapixels/commands/cv/face_landmarks_3d.py b/megapixels/commands/cv/face_landmarks_3d.py new file mode 100644 index 00000000..03ef8fc2 --- /dev/null +++ b/megapixels/commands/cv/face_landmarks_3d.py @@ -0,0 +1,96 @@ +""" + +""" + +import click + +from app.settings import types +from app.utils import click_utils +from app.settings import app_cfg as cfg + +color_filters = {'color': 1, 'gray': 2, 'all': 3} + +@click.command() +@click.option('-i', '--input', 'opt_dirs_in', required=True, multiple=True, + help='Input directory') +@click.option('-o', '--output', 'opt_fp_out', required=True, + help='Output CSV') +@click.option('-e', '--ext', 'opt_ext', + default='jpg', type=click.Choice(['jpg', 'png']), + help='File glob ext') +@click.option('--size', 'opt_size', + type=(int, int), default=(300, 300), + help='Output image size') +@click.option('-g', '--gpu', 'opt_gpu', default=0, + help='GPU index') +@click.option('--slice', 'opt_slice', type=(int, int), default=(None, None), + help='Slice list of files') +@click.option('--recursive/--no-recursive', 'opt_recursive', is_flag=True, default=False, + help='Use glob recursion (slower)') +@click.option('-f', '--force', 'opt_force', is_flag=True, + help='Force overwrite file') +@click.pass_context +def cli(ctx, opt_dirs_in, opt_fp_out, opt_ext, opt_size, opt_gpu, opt_slice, + opt_recursive, opt_force): + """Converts face imges to 3D landmarks""" + + import sys + import os + from os.path import join + from pathlib import Path + from glob import glob + + from tqdm import tqdm + import numpy as np + import dlib # must keep a local reference for dlib + import cv2 as cv + import pandas as pd + from face_alignment import FaceAlignment, LandmarksType + from skimage import io + + from app.utils import logger_utils, file_utils + from app.processors import face_detector + + # ------------------------------------------------- + # init here + + + log = logger_utils.Logger.getLogger() + + if not opt_force and Path(opt_fp_out).exists(): + log.error('File exists. Use "-f / --force" to overwite') + return + + device = 'cuda' if opt_gpu > -1 else 'cpu' + fa = FaceAlignment(LandmarksType._3D, flip_input=False, device=device) + + # get list of files to process + fp_ims = [] + for opt_dir_in in opt_dirs_in: + if opt_recursive: + fp_glob = join(opt_dir_in, '**/*.{}'.format(opt_ext)) + fp_ims += glob(fp_glob, recursive=True) + else: + fp_glob = join(opt_dir_in, '*.{}'.format(opt_ext)) + fp_ims += glob(fp_glob) + log.debug(fp_glob) + + + if opt_slice: + fp_ims = fp_ims[opt_slice[0]:opt_slice[1]] + log.debug('processing {:,} files'.format(len(fp_ims))) + + + data = {} + + for fp_im in tqdm(fp_ims): + fpp_im = Path(fp_im) + im = io.imread(fp_im) + preds = fa.get_landmarks(im) + if preds and len(preds) > 0: + data[fpp_im.name] = preds[0].tolist() + + # save date + file_utils.mkdirs(opt_fp_out) + + file_utils.write_json(data, opt_fp_out, verbose=True) \ No newline at end of file diff --git a/megapixels/commands/cv/face_pose.py b/megapixels/commands/cv/face_pose.py index e7ffb7ac..c37d006f 100644 --- a/megapixels/commands/cv/face_pose.py +++ b/megapixels/commands/cv/face_pose.py @@ -95,7 +95,7 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_dir_media, opt_data_store, opt_dataset, for record_index, df_img_group in tqdm(df_img_groups): # make fp ds_record = df_record.iloc[record_index] - fp_im = data_store.face_image(ds_record.subdir, ds_record.fn, ds_record.ext) + fp_im = data_store.face(ds_record.subdir, ds_record.fn, ds_record.ext) im = cv.imread(fp_im) # get bbox x = df_img_group.x.values[0] diff --git a/megapixels/commands/cv/face_vector.py b/megapixels/commands/cv/face_vector.py index 203f73eb..cd816f9f 100644 --- a/megapixels/commands/cv/face_vector.py +++ b/megapixels/commands/cv/face_vector.py @@ -98,7 +98,7 @@ def cli(ctx, opt_fp_out, opt_dir_media, opt_data_store, opt_dataset, opt_size, ds_file = df_record.loc[roi_index] # locate image meta #ds_file = df_record.loc['index', image_index] # locate image meta - fp_im = data_store.face_image(str(ds_file.subdir), str(ds_file.fn), str(ds_file.ext)) + fp_im = data_store.face(str(ds_file.subdir), str(ds_file.fn), str(ds_file.ext)) im = cv.imread(fp_im) # get bbox x = df_img_group.x.values[0] diff --git a/megapixels/commands/cv/faces_to_3dlm.py b/megapixels/commands/cv/faces_to_3dlm.py deleted file mode 100644 index 658d4484..00000000 --- a/megapixels/commands/cv/faces_to_3dlm.py +++ /dev/null @@ -1,96 +0,0 @@ -""" -Crop images to prepare for training -""" - -import click -# from PIL import Image, ImageOps, ImageFilter, ImageDraw - -from app.settings import types -from app.utils import click_utils -from app.settings import app_cfg as cfg - -color_filters = {'color': 1, 'gray': 2, 'all': 3} - -@click.command() -@click.option('-i', '--input', 'opt_dirs_in', required=True, multiple=True, - help='Input directory') -@click.option('-o', '--output', 'opt_fp_out', required=True, - help='Output CSV') -@click.option('-e', '--ext', 'opt_ext', - default='jpg', type=click.Choice(['jpg', 'png']), - help='File glob ext') -@click.option('--size', 'opt_size', - type=(int, int), default=(300, 300), - help='Output image size') -@click.option('-g', '--gpu', 'opt_gpu', default=0, - help='GPU index') -@click.option('--slice', 'opt_slice', type=(int, int), default=(None, None), - help='Slice list of files') -@click.option('--recursive/--no-recursive', 'opt_recursive', is_flag=True, default=False, - help='Use glob recursion (slower)') -@click.option('-f', '--force', 'opt_force', is_flag=True, - help='Force overwrite file') -@click.pass_context -def cli(ctx, opt_dirs_in, opt_fp_out, opt_ext, opt_size, opt_gpu, opt_slice, - opt_recursive, opt_force): - """Converts face imges to 3D landmarks""" - - import sys - import os - from os.path import join - from pathlib import Path - from glob import glob - - from tqdm import tqdm - import numpy as np - import dlib # must keep a local reference for dlib - import cv2 as cv - import pandas as pd - from face_alignment import FaceAlignment, LandmarksType - from skimage import io - - from app.utils import logger_utils, file_utils - from app.processors import face_detector - - # ------------------------------------------------- - # init here - - log = logger_utils.Logger.getLogger() - - if not opt_force and Path(opt_fp_out).exists(): - log.error('File exists. Use "-f / --force" to overwite') - return - - device = 'cuda' if opt_gpu > -1 else 'cpu' - fa = FaceAlignment(LandmarksType._3D, flip_input=False, device=device) - - # get list of files to process - fp_ims = [] - for opt_dir_in in opt_dirs_in: - if opt_recursive: - fp_glob = join(opt_dir_in, '**/*.{}'.format(opt_ext)) - fp_ims += glob(fp_glob, recursive=True) - else: - fp_glob = join(opt_dir_in, '*.{}'.format(opt_ext)) - fp_ims += glob(fp_glob) - log.debug(fp_glob) - - - if opt_slice: - fp_ims = fp_ims[opt_slice[0]:opt_slice[1]] - log.debug('processing {:,} files'.format(len(fp_ims))) - - - data = {} - - for fp_im in tqdm(fp_ims): - fpp_im = Path(fp_im) - im = io.imread(fp_im) - preds = fa.get_landmarks(im) - if preds and len(preds) > 0: - data[fpp_im.name] = preds[0].tolist() - - # save date - file_utils.mkdirs(opt_fp_out) - - file_utils.write_json(data, opt_fp_out, verbose=True) \ No newline at end of file diff --git a/megapixels/commands/datasets/lookup.py b/megapixels/commands/datasets/lookup.py index c1c66c19..5ae4c3f5 100644 --- a/megapixels/commands/datasets/lookup.py +++ b/megapixels/commands/datasets/lookup.py @@ -10,7 +10,7 @@ log = Logger.getLogger() @click.command() @click.option('--index', 'opt_index', type=int, required=True, - help='Vector index to lookup') + help='File index to lookup') @click.option('--data_store', 'opt_data_store', type=cfg.DataStoreVar, default=click_utils.get_default(types.DataStore.SSD), @@ -45,7 +45,7 @@ def cli(ctx, opt_index, opt_data_store, opt_dataset): dataset.load_records() dataset.load_identities() # set data store and load files - # find image records + # get image record from file index image_record = dataset.index_to_record(opt_index) image_record.summarize() # load image diff --git a/megapixels/commands/datasets/s3_sync.py b/megapixels/commands/datasets/s3_sync.py index 3098d9be..17940c6d 100644 --- a/megapixels/commands/datasets/s3_sync.py +++ b/megapixels/commands/datasets/s3_sync.py @@ -54,4 +54,8 @@ def cli(ctx, opt_data_store, opt_dataset, opt_type, opt_dryrun): if not opt_dryrun: subprocess.call(cmd) - \ No newline at end of file + +''' +upload: '/data_store_ssd/datasets/people/vgg_face2/media/uuid/00418e0e-48e9-44f9-b6a0-b2ffd773802e.jpg' -> 's3://megapixels/v1/media/vgg_face2/00418e0e-48e9-44f9-b6a0-b2ffd773802e.jpg' [3202 of 3187313] +[2953 of 3187313] +''' \ No newline at end of file diff --git a/megapixels/commands/demo/face_search.py b/megapixels/commands/demo/face_search.py index 0452cc9d..34a25762 100644 --- a/megapixels/commands/demo/face_search.py +++ b/megapixels/commands/demo/face_search.py @@ -6,9 +6,11 @@ from app.utils import click_utils from app.settings import app_cfg as cfg from app.utils.logger_utils import Logger +log = Logger.getLogger() + @click.command() @click.option('-i', '--input', 'opt_fp_in', required=True, - help='Input face image') + help='File to lookup') @click.option('--data_store', 'opt_data_store', type=cfg.DataStoreVar, default=click_utils.get_default(types.DataStore.SSD), @@ -19,8 +21,8 @@ from app.utils.logger_utils import Logger required=True, show_default=True, help=click_utils.show_help(types.Dataset)) -@click.option('--gpu', 'opt_gpu', default=0, - help='GPU index (use -1 for CPU)') +@click.option('--gpu', 'opt_gpu', default=0, + help='GPU index (use -1 for CPU') @click.pass_context def cli(ctx, opt_fp_in, opt_data_store, opt_dataset, opt_gpu): """Display image info""" @@ -31,18 +33,22 @@ def cli(ctx, opt_fp_in, opt_data_store, opt_dataset, opt_gpu): from pathlib import Path import time - import imutils import pandas as pd import cv2 as cv - import dlib from tqdm import tqdm - + import imutils + from app.utils import file_utils, im_utils - from app.models.data_store import DataStore, DataStoreS3 + from app.models.data_store import DataStore from app.processors import face_detector from app.processors import face_recognition log = Logger.getLogger() + # init dataset + dataset = Dataset(opt_data_store, opt_dataset) + dataset.load_face_vectors() + dataset.load_records() + dataset.load_identities() # init face detection detector = face_detector.DetectorDLIBHOG() @@ -52,6 +58,7 @@ def cli(ctx, opt_fp_in, opt_data_store, opt_dataset, opt_gpu): # load query image im_query = cv.imread(opt_fp_in) + # get detection as BBox object bboxes = detector.detect(im_query, largest=True) bbox = bboxes[0] @@ -64,9 +71,6 @@ def cli(ctx, opt_fp_in, opt_data_store, opt_dataset, opt_gpu): # extract the face vectors vec_query = recognition.vec(im_query, bbox) - - # load dataset CSVs - dataset = Dataset(opt_data_store, opt_dataset) # find matches image_records = dataset.find_matches(vec_query, n_results=5) @@ -91,4 +95,4 @@ def cli(ctx, opt_fp_in, opt_data_store, opt_dataset, opt_gpu): sys.exit() elif k != 255: # any key to continue - break \ No newline at end of file + break -- cgit v1.2.3-70-g09d2