summaryrefslogtreecommitdiff
path: root/megapixels/commands/cv
diff options
context:
space:
mode:
Diffstat (limited to 'megapixels/commands/cv')
-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
2 files changed, 42 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)))