summaryrefslogtreecommitdiff
path: root/megapixels/commands/cv/face_landmark_2d_5.py
diff options
context:
space:
mode:
authoradamhrv <adam@ahprojects.com>2019-01-18 11:00:18 +0100
committeradamhrv <adam@ahprojects.com>2019-01-18 11:00:18 +0100
commite06af50389f849be0bfe4fa97d39f4519ef2c711 (patch)
tree49755b51e1b8b1f8031e5483333570a8e9951272 /megapixels/commands/cv/face_landmark_2d_5.py
parent03ad11fb2a3dcd425d50167b15d72d4e0ef536a2 (diff)
change to cli_proc
Diffstat (limited to 'megapixels/commands/cv/face_landmark_2d_5.py')
-rw-r--r--megapixels/commands/cv/face_landmark_2d_5.py146
1 files changed, 0 insertions, 146 deletions
diff --git a/megapixels/commands/cv/face_landmark_2d_5.py b/megapixels/commands/cv/face_landmark_2d_5.py
deleted file mode 100644
index 40ec6f41..00000000
--- a/megapixels/commands/cv/face_landmark_2d_5.py
+++ /dev/null
@@ -1,146 +0,0 @@
-"""
-
-"""
-
-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_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('-d', '--detector', 'opt_detector_type',
- type=cfg.FaceLandmark2D_5Var,
- default=click_utils.get_default(types.FaceLandmark2D_5.DLIB),
- help=click_utils.show_help(types.FaceLandmark2D_5))
-@click.option('--size', 'opt_size',
- type=(int, int), default=(300, 300),
- help='Output image size')
-@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_detector_type,
- opt_size, opt_slice, opt_force, opt_display):
- """Creates 2D 5-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_landmarks
- from app.models.data_store import DataStore
- from app.models.bbox import BBox
-
- # -------------------------------------------------
- # init here
-
- log = logger_utils.Logger.getLogger()
- # init filepaths
- data_store = DataStore(opt_data_store, opt_dataset)
- # set file output path
- metadata_type = types.Metadata.FACE_LANDMARK_2D_5
- 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
-
- # init face landmark processors
- if opt_detector_type == types.FaceLandmark2D_5.DLIB:
- # use dlib 68 point detector
- landmark_detector = face_landmarks.Dlib2D_5()
- elif opt_detector_type == types.FaceLandmark2D_5.MTCNN:
- # use dlib 5 point detector
- landmark_detector = face_landmarks.MTCNN2D_5()
- else:
- log.error('{} not yet implemented'.format(opt_detector_type.name))
- return
-
- log.info(f'Using landmark detector: {opt_detector_type.name}')
-
- # load filepath data
- fp_record = data_store.metadata(types.Metadata.FILE_RECORD)
- df_record = pd.read_csv(fp_record).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):
-
- # acces file record
- ds_record = df_record.iloc[record_index]
-
- # load image
- fp_im = data_store.face(ds_record.subdir, ds_record.fn, ds_record.ext)
- im = cv.imread(fp_im)
- im_resized = im_utils.resize(im, width=opt_size[0], height=opt_size[1])
-
- # iterate image group dataframe with roi index as key
- for roi_index, df_img in df_img_group.iterrows():
-
- # get bbox
- x, y, w, h = df_img.x, df_img.y, df_img.w, df_img.h
- dim = im_resized.shape[:2][::-1]
- bbox = BBox.from_xywh(x, y, w, h).to_dim(dim)
-
- # get landmark points
- points = landmark_detector.landmarks(im_resized, bbox)
- points_norm = landmark_detector.normalize(points, dim)
- points_flat = landmark_detector.flatten(points_norm)
-
- # display to screen if optioned
- if opt_display:
- draw_utils.draw_landmarks2D(im_resized, points)
- draw_utils.draw_bbox(im_resized, bbox)
- cv.imshow('', im_resized)
- display_utils.handle_keyboard()
-
- results.append(points_flat)
-
- # 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