diff options
Diffstat (limited to 'megapixels/commands/cv')
| -rw-r--r-- | megapixels/commands/cv/face_attributes.py | 139 | ||||
| -rw-r--r-- | megapixels/commands/cv/face_landmark_2d_68.py | 4 | ||||
| -rw-r--r-- | megapixels/commands/cv/face_pose.py | 11 | ||||
| -rw-r--r-- | megapixels/commands/cv/face_roi.py | 14 | ||||
| -rw-r--r-- | megapixels/commands/cv/face_vector.py | 13 |
5 files changed, 160 insertions, 21 deletions
diff --git a/megapixels/commands/cv/face_attributes.py b/megapixels/commands/cv/face_attributes.py new file mode 100644 index 00000000..bb7978f7 --- /dev/null +++ b/megapixels/commands/cv/face_attributes.py @@ -0,0 +1,139 @@ +""" + +""" + +import click + +from app.settings import types +from app.utils import click_utils +from app.settings import app_cfg as cfg + + +@click.command() +@click.option('-i', '--input', 'opt_fp_in', default=None, + help='Override enum input filename CSV') +@click.option('-o', '--output', 'opt_fp_out', default=None, + help='Override enum output filename CSV') +@click.option('-m', '--media', 'opt_dir_media', default=None, + help='Override enum media directory') +@click.option('--store', 'opt_data_store', + type=cfg.DataStoreVar, + default=click_utils.get_default(types.DataStore.HDD), + show_default=True, + help=click_utils.show_help(types.Dataset)) +@click.option('--dataset', 'opt_dataset', + type=cfg.DatasetVar, + required=True, + show_default=True, + help=click_utils.show_help(types.Dataset)) +@click.option('--size', 'opt_size', + type=(int, int), default=cfg.DEFAULT_SIZE_FACE_DETECT, + help='Processing size for detection') +@click.option('--slice', 'opt_slice', type=(int, int), default=(None, None), + help='Slice list of files') +@click.option('-f', '--force', 'opt_force', is_flag=True, + help='Force overwrite file') +@click.option('-d', '--display', 'opt_display', is_flag=True, + help='Display image for debugging') +@click.pass_context +def cli(ctx, opt_fp_in, opt_fp_out, opt_dir_media, opt_data_store, opt_dataset, + opt_size, opt_slice, opt_force, opt_display): + """Creates 2D 68-point 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 cv2 as cv + import pandas as pd + + from app.utils import logger_utils, file_utils, im_utils, display_utils, draw_utils + from app.processors import face_age_gender + from app.models.data_store import DataStore + from app.models.bbox import BBox + + # ------------------------------------------------------------------------- + # init here + + log = logger_utils.Logger.getLogger() + # init face processors + age_estimator_apnt = face_age_gender.FaceAgeApparent() + age_estimator_real = face_age_gender.FaceAgeReal() + gender_estimator = face_age_gender.FaceGender() + + # init filepaths + data_store = DataStore(opt_data_store, opt_dataset) + # set file output path + metadata_type = types.Metadata.FACE_ATTRIBUTES + fp_out = data_store.metadata(metadata_type) if opt_fp_out is None else opt_fp_out + if not opt_force and Path(fp_out).exists(): + log.error('File exists. Use "-f / --force" to overwite') + return + + # ------------------------------------------------------------------------- + # load filepath data + fp_record = data_store.metadata(types.Metadata.FILE_RECORD) + df_record = pd.read_csv(fp_record, dtype={'fn':str}).set_index('index') + # load ROI data + fp_roi = data_store.metadata(types.Metadata.FACE_ROI) + df_roi = pd.read_csv(fp_roi).set_index('index') + # slice if you want + if opt_slice: + df_roi = df_roi[opt_slice[0]:opt_slice[1]] + # group by image index (speedup if multiple faces per image) + df_img_groups = df_roi.groupby('record_index') + log.debug('processing {:,} groups'.format(len(df_img_groups))) + + # store landmarks in list + results = [] + + # ------------------------------------------------------------------------- + # iterate groups with file/record index as key + + for record_index, df_img_group in tqdm(df_img_groups): + + # access file_record DataSeries + file_record = df_record.iloc[record_index] + + # load image + fp_im = data_store.face(file_record.subdir, file_record.fn, file_record.ext) + im = cv.imread(fp_im) + im_resized = im_utils.resize(im, width=opt_size[0], height=opt_size[1]) + dim = im_resized.shape[:2][::-1] + + # iterate ROIs in this image + for roi_index, df_img in df_img_group.iterrows(): + + # find landmarks + bbox_norm = BBox.from_xywh(df_img.x, df_img.y, df_img.w, df_img.h) + bbox_dim = bbox_norm.to_dim(dim) + + #age_apnt = age_estimator_apnt.predict(im_resized, bbox_norm) + #age_real = age_estimator_real.predict(im_resized, bbox_norm) + #gender = gender_estimator.predict(im_resized, bbox_norm) + + # attr_obj = { + # 'age_real':float(f'{age_real:.2f}'), + # 'age_apparent': float(f'{age_apnt:.2f}'), + # 'm': float(f'{gender["m"]:.4f}'), + # 'f': float(f'{gender["f"]:.4f}'), + # 'roi_index': roi_index + # } + attr_obj = { + 'roi_index': roi_index + } + results.append(attr_obj) + + + # create DataFrame and save to CSV + file_utils.mkdirs(fp_out) + df = pd.DataFrame.from_dict(results) + df.index.name = 'index' + df.to_csv(fp_out) + + # save script + file_utils.write_text(' '.join(sys.argv), '{}.sh'.format(fp_out))
\ No newline at end of file diff --git a/megapixels/commands/cv/face_landmark_2d_68.py b/megapixels/commands/cv/face_landmark_2d_68.py index e24d4b60..c6978a40 100644 --- a/megapixels/commands/cv/face_landmark_2d_68.py +++ b/megapixels/commands/cv/face_landmark_2d_68.py @@ -126,7 +126,7 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_dir_media, opt_data_store, opt_dataset, bbox = BBox.from_xywh(x, y, w, h).to_dim(dim) points = landmark_detector.landmarks(im_resized, bbox) points_norm = landmark_detector.normalize(points, dim) - points_flat = landmark_detector.flatten(points_norm) + points_str = landmark_detector.to_str(points_norm) # display if optioned if opt_display: @@ -137,7 +137,7 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_dir_media, opt_data_store, opt_dataset, display_utils.handle_keyboard() # add to results for CSV - results.append(points_flat) + results.append({'vec': points_str, 'roi_index':roi_index}) # create DataFrame and save to CSV diff --git a/megapixels/commands/cv/face_pose.py b/megapixels/commands/cv/face_pose.py index 70ea1f30..75db603b 100644 --- a/megapixels/commands/cv/face_pose.py +++ b/megapixels/commands/cv/face_pose.py @@ -92,7 +92,7 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_dir_media, opt_data_store, opt_dataset, # load data fp_record = data_store.metadata(types.Metadata.FILE_RECORD) - df_record = pd.read_csv(fp_record).set_index('index') + df_record = pd.read_csv(fp_record, dtype={'fn':str}).set_index('index') # load ROI data fp_roi = data_store.metadata(types.Metadata.FACE_ROI) df_roi = pd.read_csv(fp_roi).set_index('index') @@ -125,10 +125,11 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_dir_media, opt_data_store, opt_dataset, x, y, w, h = df_img.x, df_img.y, df_img.w, df_img.h #dim = (file_record.width, file_record.height) dim = im_resized.shape[:2][::-1] - bbox = BBox.from_xywh(x, y, w, h).to_dim(dim) + bbox_norm = BBox.from_xywh(x, y, w, h) + bbox_dim = bbox_norm.to_dim(dim) # get pose - landmarks = face_landmarks.landmarks(im_resized, bbox) + landmarks = face_landmarks.landmarks(im_resized, bbox_norm) pose_data = face_pose.pose(landmarks, dim) #pose_degrees = pose_data['degrees'] # only keep the degrees data #pose_degrees['points_nose'] = pose_data @@ -143,8 +144,8 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_dir_media, opt_data_store, opt_dataset, # add image index and append to result CSV data pose_data['roi_index'] = roi_index for k, v in pose_data['points'].items(): - pose_data[f'point_{k}_x'] = v[0][0] / dim[0] - pose_data[f'point_{k}_y'] = v[0][1] / dim[1] + pose_data[f'point_{k}_x'] = v[0] / dim[0] + pose_data[f'point_{k}_y'] = v[1] / dim[1] # rearrange data structure for DataFrame pose_data.pop('points') diff --git a/megapixels/commands/cv/face_roi.py b/megapixels/commands/cv/face_roi.py index 70fff401..950936cf 100644 --- a/megapixels/commands/cv/face_roi.py +++ b/megapixels/commands/cv/face_roi.py @@ -33,7 +33,7 @@ color_filters = {'color': 1, 'gray': 2, 'all': 3} help='Output image size') @click.option('-d', '--detector', 'opt_detector_type', type=cfg.FaceDetectNetVar, - default=click_utils.get_default(types.FaceDetectNet.DLIB_CNN), + default=click_utils.get_default(types.FaceDetectNet.CVDNN), help=click_utils.show_help(types.FaceDetectNet)) @click.option('-g', '--gpu', 'opt_gpu', default=0, help='GPU index') @@ -97,8 +97,8 @@ def cli(ctx, opt_fp_in, opt_dir_media, opt_fp_out, opt_data_store, opt_dataset, detector = face_detector.DetectorDLIBCNN(gpu=opt_gpu) elif opt_detector_type == types.FaceDetectNet.DLIB_HOG: detector = face_detector.DetectorDLIBHOG() - elif opt_detector_type == types.FaceDetectNet.MTCNN: - detector = face_detector.DetectorMTCNN(gpu=opt_gpu) + elif opt_detector_type == types.FaceDetectNet.MTCNN_TF: + detector = face_detector.DetectorMTCNN_TF(gpu=opt_gpu) elif opt_detector_type == types.FaceDetectNet.HAAR: log.error('{} not yet implemented'.format(opt_detector_type.name)) return @@ -106,7 +106,7 @@ def cli(ctx, opt_fp_in, opt_dir_media, opt_fp_out, opt_data_store, opt_dataset, # get list of files to process fp_in = data_store.metadata(types.Metadata.FILE_RECORD) if opt_fp_in is None else opt_fp_in - df_records = pd.read_csv(fp_in).set_index('index') + df_records = pd.read_csv(fp_in, dtype={'fn':str}).set_index('index') if opt_slice: df_records = df_records[opt_slice[0]:opt_slice[1]] log.debug('processing {:,} files'.format(len(df_records))) @@ -144,9 +144,9 @@ def cli(ctx, opt_fp_in, opt_dir_media, opt_fp_out, opt_data_store, opt_dataset, for bbox in bboxes: roi = { 'record_index': int(df_record.Index), - 'x': bbox.x, - 'y': bbox.y, - 'w': bbox.w, + 'x': bbox.x, + 'y': bbox.y, + 'w': bbox.w, 'h': bbox.h } data.append(roi) diff --git a/megapixels/commands/cv/face_vector.py b/megapixels/commands/cv/face_vector.py index 9e9f6396..9a527bc3 100644 --- a/megapixels/commands/cv/face_vector.py +++ b/megapixels/commands/cv/face_vector.py @@ -27,10 +27,10 @@ from app.settings import app_cfg as cfg show_default=True, help=click_utils.show_help(types.Dataset)) @click.option('--size', 'opt_size', - type=(int, int), default=(300, 300), + type=(int, int), default=cfg.DEFAULT_SIZE_FACE_DETECT, help='Output image size') @click.option('-e', '--extractor', 'opt_extractor', - default=types.FaceExtractor.VGG, + default=click_utils.get_default(types.FaceExtractor.VGG), type=cfg.FaceExtractorVar, help='Type of extractor framework/network to use') @click.option('-j', '--jitters', 'opt_jitters', default=cfg.DLIB_FACEREC_JITTERS, @@ -88,7 +88,7 @@ def cli(ctx, opt_fp_out, opt_dir_media, opt_data_store, opt_dataset, opt_size, # load data fp_record = data_store.metadata(types.Metadata.FILE_RECORD) - df_record = pd.read_csv(fp_record).set_index('index') + df_record = pd.read_csv(fp_record, dtype={'fn':str}).set_index('index') fp_roi = data_store.metadata(types.Metadata.FACE_ROI) df_roi = pd.read_csv(fp_roi).set_index('index') @@ -115,10 +115,9 @@ def cli(ctx, opt_fp_out, opt_dir_media, opt_data_store, opt_dataset, opt_size, bbox = BBox.from_xywh(x, y, w, h) # norm # compute vec vec = extractor.extract(im, bbox) # use normalized BBox - vec_flat = extractor.flatten(vec) - vec_flat['roi_index'] = roi_index - vec_flat['record_index'] = record_index - vecs.append(vec_flat) + vec_str = extractor.to_str(vec) + vec_obj = {'vec':vec_str, 'roi_index': roi_index, 'record_index':record_index} + vecs.append(vec_obj) # ------------------------------------------------- # save data |
