summaryrefslogtreecommitdiff
path: root/megapixels/commands
diff options
context:
space:
mode:
Diffstat (limited to 'megapixels/commands')
-rw-r--r--megapixels/commands/cv/rois_to_pose.py (renamed from megapixels/commands/cv/face_pose_to_csv.py)60
-rw-r--r--megapixels/commands/cv/rois_to_vecs.py (renamed from megapixels/commands/cv/face_vec_to_csv.py)3
-rw-r--r--megapixels/commands/datasets/filter_poses.py76
-rw-r--r--megapixels/commands/datasets/lookup.py44
4 files changed, 162 insertions, 21 deletions
diff --git a/megapixels/commands/cv/face_pose_to_csv.py b/megapixels/commands/cv/rois_to_pose.py
index ca7489de..3877cecf 100644
--- a/megapixels/commands/cv/face_pose_to_csv.py
+++ b/megapixels/commands/cv/rois_to_pose.py
@@ -1,18 +1,15 @@
"""
-Crop images to prepare for training
+Converts ROIs to pose: yaw, roll, pitch
"""
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('-f', '--files', 'opt_fp_files', required=True,
+@click.option('-i', '--input', 'opt_fp_files', required=True,
help='Input ROI CSV')
@click.option('-r', '--rois', 'opt_fp_rois', required=True,
help='Input ROI CSV')
@@ -27,9 +24,11 @@ color_filters = {'color': 1, 'gray': 2, 'all': 3}
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_files, opt_fp_rois, opt_dir_media, opt_fp_out, opt_size,
- opt_slice, opt_force):
+ opt_slice, opt_force, opt_display):
"""Converts ROIs to pose: roll, yaw, pitch"""
import sys
@@ -58,6 +57,7 @@ def cli(ctx, opt_fp_files, opt_fp_rois, opt_dir_media, opt_fp_out, opt_size,
face_pose = FacePoseDLIB()
face_landmarks = LandmarksDLIB()
+ # load datra
df_files = pd.read_csv(opt_fp_files)
df_rois = pd.read_csv(opt_fp_rois)
@@ -70,32 +70,54 @@ def cli(ctx, opt_fp_files, opt_fp_rois, opt_dir_media, opt_fp_out, opt_size,
# -------------------------------------------------
# process here
-
- df_roi_groups = df_rois.groupby('index')
- log.debug('processing {:,} groups'.format(len(df_roi_groups)))
+ df_img_groups = df_rois.groupby('image_index')
+ log.debug('processing {:,} groups'.format(len(df_img_groups)))
poses = []
- #for df_roi_group in tqdm(df_roi_groups.itertuples(), total=len(df_roi_groups)):
- for df_roi_group_idx, df_roi_group in tqdm(df_roi_groups):
+ # iterate
+ #for df_roi_group_idx, df_roi_group in tqdm(df_roi_groups):
+ for image_index, df_img_group in tqdm(df_img_groups):
# make fp
- image_index = df_roi_group.image_index.values[0]
+ #image_index = df_roi_group.image_index.values[0]
pds_file = df_files.iloc[image_index]
fp_im = join(opt_dir_media, pds_file.subdir, '{}.{}'.format(pds_file.fn, pds_file.ext))
im = cv.imread(fp_im)
# get bbox
- x = df_roi_group.x.values[0]
- y = df_roi_group.y.values[0]
- w = df_roi_group.w.values[0]
- h = df_roi_group.h.values[0]
+ x = df_img_group.x.values[0]
+ y = df_img_group.y.values[0]
+ w = df_img_group.w.values[0]
+ h = df_img_group.h.values[0]
dim = im.shape[:2][::-1]
bbox = BBox.from_xywh(x, y, w, h).to_dim(dim)
# get pose
landmarks = face_landmarks.landmarks(im, bbox)
- pose = face_pose.pose(landmarks, dim)
- pose['image_index'] = image_index
- poses.append(pose)
+ pose_data = face_pose.pose(landmarks, dim, project_points=opt_display)
+ pose_degrees = pose_data['degrees'] # only keep the degrees data
+
+ # use the project point data if display flag set
+ if opt_display:
+ pts_im = pose_data['points_image']
+ pts_model = pose_data['points_model']
+ pt_nose = pose_data['point_nose']
+ dst = im.copy()
+ face_pose.draw_pose(dst, pts_im, pts_model, pt_nose)
+ face_pose.draw_degrees(dst, pose_degrees)
+ # display to cv window
+ cv.imshow('', dst)
+ while True:
+ k = cv.waitKey(1) & 0xFF
+ if k == 27 or k == ord('q'): # ESC
+ cv.destroyAllWindows()
+ sys.exit()
+ elif k != 255:
+ # any key to continue
+ break
+
+ # add image index and append to result CSV data
+ pose_degrees['image_index'] = image_index
+ poses.append(pose_degrees)
# save date
diff --git a/megapixels/commands/cv/face_vec_to_csv.py b/megapixels/commands/cv/rois_to_vecs.py
index 6c9fad09..525f4404 100644
--- a/megapixels/commands/cv/face_vec_to_csv.py
+++ b/megapixels/commands/cv/rois_to_vecs.py
@@ -10,7 +10,7 @@ from app.settings import app_cfg as cfg
@click.command()
@click.option('-i', '--input', 'opt_fp_files', required=True,
- help='Input ROI CSV')
+ help='Input file meta CSV')
@click.option('-r', '--rois', 'opt_fp_rois', required=True,
help='Input ROI CSV')
@click.option('-m', '--media', 'opt_dir_media', required=True,
@@ -73,7 +73,6 @@ def cli(ctx, opt_fp_files, opt_fp_rois, opt_dir_media, opt_fp_out, opt_size,
# -------------------------------------------------
# process here
-
df_img_groups = df_rois.groupby('image_index')
log.debug('processing {:,} groups'.format(len(df_img_groups)))
diff --git a/megapixels/commands/datasets/filter_poses.py b/megapixels/commands/datasets/filter_poses.py
new file mode 100644
index 00000000..304eeff2
--- /dev/null
+++ b/megapixels/commands/datasets/filter_poses.py
@@ -0,0 +1,76 @@
+import click
+
+from app.settings import types
+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 directory')
+@click.option('-o', '--output', 'opt_fp_out', required=True,
+ help='Output directory')
+@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('--yaw', 'opt_yaw', type=(float, float), default=(-25,25),
+ help='Yaw (min, max)')
+@click.option('--roll', 'opt_roll', type=(float, float), default=(-15,15),
+ help='Roll (min, max)')
+@click.option('--pitch', 'opt_pitch', type=(float, float), default=(-10,10),
+ help='Pitch (min, max)')
+@click.option('--drop', 'opt_drop', type=click.Choice(['valid', 'invalid']), default='invalid',
+ help='Drop valid or invalid poses')
+@click.pass_context
+def cli(ctx, opt_fp_in, opt_fp_out, opt_slice, opt_yaw, opt_roll, opt_pitch,
+ opt_drop, opt_force):
+ """Filter out exaggerated poses"""
+
+ from glob import glob
+ from os.path import join
+ from pathlib import Path
+ import time
+ from multiprocessing.dummy import Pool as ThreadPool
+ import random
+
+ import pandas as pd
+ from tqdm import tqdm
+ from glob import glob
+
+ from app.utils import file_utils, im_utils
+
+
+ if not opt_force and Path(opt_fp_out).exists():
+ log.error('File exists. Use "-f / --force" to overwite')
+ return
+
+ df_poses = pd.read_csv(opt_fp_in).set_index('index')
+
+ if opt_slice:
+ df_poses = df_poses[opt_slice[0]:opt_slice[1]]
+
+ log.info('Processing {:,} rows'.format(len(df_poses)))
+
+ # extend a new temporary column
+ df_poses['valid'] = [0] * len(df_poses)
+
+ # filter out extreme poses
+ for ds_pose in tqdm(df_poses.itertuples(), total=len(df_poses)):
+ if ds_pose.yaw > opt_yaw[0] and ds_pose.yaw < opt_yaw[1] \
+ and ds_pose.roll > opt_roll[0] and ds_pose.roll < opt_roll[1] \
+ and ds_pose.pitch > opt_pitch[0] and ds_pose.pitch < opt_pitch[1]:
+ df_poses.at[ds_pose.Index, 'valid'] = 1
+
+ # filter out valid/invalid
+ drop_val = 0 if opt_drop == 'valid' else 0 # drop 0's if drop == valid, else drop 1's
+ df_poses_filtered = df_poses.drop(df_poses[df_poses.valid == int()].index, axis=0)
+
+ # drop temp column
+ df_poses_filtered = df_poses_filtered.drop('valid', axis=1)
+
+ # save filtered poses
+ df_poses_filtered.to_csv(opt_fp_out)
+ log.info('Saved {:,} rows'.format(len(df_poses_filtered))) \ No newline at end of file
diff --git a/megapixels/commands/datasets/lookup.py b/megapixels/commands/datasets/lookup.py
new file mode 100644
index 00000000..11f54957
--- /dev/null
+++ b/megapixels/commands/datasets/lookup.py
@@ -0,0 +1,44 @@
+import click
+
+from app.settings import types
+from app.utils import click_utils
+from app.settings import app_cfg as cfg
+from app.utils.logger_utils import Logger
+
+log = Logger.getLogger()
+
+lookup_types = ['image', 'identity']
+
+@click.command()
+@click.option('-i', '--input', 'opt_fp_in', required=True,
+ help='Input CSV file')
+@click.option('-t', '--type', 'opt_type', default='image',
+ type=click.Choice(lookup_types),
+ help='Type of lookup')
+@click.option('-d', '--dataset', 'opt_dataset', required=True,
+ type=cfg.DatasetVar,
+ default=click_utils.get_default(types.Dataset.LFW),
+ show_default=True,
+ help=click_utils.show_help(types.Dataset))
+@click.option('--index', 'opt_index', required=True,
+ help='Index to lookup')
+@click.pass_context
+def cli(ctx, opt_fp_in, opt_fp_out, opt_index):
+ """Display image info"""
+
+ from glob import glob
+ from os.path import join
+ from pathlib import Path
+ import time
+
+ import pandas as pd
+ from tqdm import tqdm
+
+ from app.utils import file_utils, im_utils
+
+ # lookup and index and display all information
+ df = pd.read_csv(opt_fp_in).set_index('index')
+
+
+
+ \ No newline at end of file