diff options
| author | adamhrv <adam@ahprojects.com> | 2019-01-16 13:30:16 +0100 |
|---|---|---|
| committer | adamhrv <adam@ahprojects.com> | 2019-01-16 13:30:16 +0100 |
| commit | 65cb506ca182272e2701136097fd00c55dc6bd69 (patch) | |
| tree | cc5be8e61a8d5173745be1d331b210e967e146b5 /megapixels/commands/demo/face_landmarks_3d.py | |
| parent | fceeb3b7adbc8d522e9fe1c40e12e9a529199068 (diff) | |
change bbox to norm, refine face extractor
Diffstat (limited to 'megapixels/commands/demo/face_landmarks_3d.py')
| -rw-r--r-- | megapixels/commands/demo/face_landmarks_3d.py | 82 |
1 files changed, 15 insertions, 67 deletions
diff --git a/megapixels/commands/demo/face_landmarks_3d.py b/megapixels/commands/demo/face_landmarks_3d.py index 22e09297..ed5a00d5 100644 --- a/megapixels/commands/demo/face_landmarks_3d.py +++ b/megapixels/commands/demo/face_landmarks_3d.py @@ -3,7 +3,6 @@ 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 @@ -27,7 +26,7 @@ from app.settings import app_cfg as cfg help='GPU index') @click.option('-f', '--force', 'opt_force', is_flag=True, help='Force overwrite file') -@click.option('--display/--no-display', 'opt_display', is_flag=True, default=False, +@click.option('--display/--no-display', 'opt_display', is_flag=True, default=True, help='Display detections to debug') @click.pass_context def cli(ctx, opt_fp_in, opt_fp_out, opt_gpu, opt_gif_frames, @@ -52,12 +51,6 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_gpu, opt_gif_frames, from app.utils import plot_utils from app.processors import face_detector, face_landmarks from app.models.data_store import DataStore - - # TOOD add selective testing - opt_run_pose = True - opt_run_2d_68 = True - opt_run_3d_68 = True - opt_run_3d_68 = True # ------------------------------------------------- @@ -74,14 +67,14 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_gpu, opt_gif_frames, # ---------------------------------------------------------------------------- # detect face - face_detector = face_detector.DetectorDLIBCNN(gpu=opt_gpu) # -1 for CPU + face_detector = face_detector.DetectorCVDNN() log.info('detecting face...') st = time.time() bboxes = face_detector.detect(im_resized, largest=True) - bbox = bboxes[0] + bbox_norm = bboxes[0] dim = im_resized.shape[:2][::-1] - bbox_dim = bbox.to_dim(dim) - if not bbox: + bbox_dim = bbox_norm.to_dim(dim) + if not bbox_norm: log.error('no face detected') return else: @@ -96,7 +89,7 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_gpu, opt_gif_frames, landmark_detector_3d_68 = face_landmarks.FaceAlignment3D_68(gpu=opt_gpu) # -1 for CPU log.info('generating 3D landmarks...') st = time.time() - points_3d_68 = landmark_detector_3d_68.landmarks(im_resized, bbox_dim.to_xyxy()) + points_3d_68 = landmark_detector_3d_68.landmarks(im_resized, bbox_norm) log.info(f'generated 3D landmarks in {(time.time() - st):.2f}s') log.info('') @@ -119,19 +112,6 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_gpu, opt_gif_frames, # ---------------------------------------------------------------------------- - # generate face vectors, only to test if feature extraction works - - log.info('initialize face recognition model...') - from app.processors import face_recognition - face_rec = face_recognition.RecognitionDLIB() - st = time.time() - log.info('generating face vector...') - vec = face_rec.vec(im_resized, bbox_dim) - log.info(f'generated face vector in {(time.time() - st):.2f}s') - log.info('') - - - # ---------------------------------------------------------------------------- # generate 68 point landmarks using dlib log.info('initializing face landmarks 68 dlib...') @@ -139,54 +119,25 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_gpu, opt_gif_frames, landmark_detector_2d_68 = face_landmarks.Dlib2D_68() log.info('generating 2D 68PT landmarks...') st = time.time() - points_2d_68 = landmark_detector_2d_68.landmarks(im_resized, bbox_dim) + points_2d_68 = landmark_detector_2d_68.landmarks(im_resized, bbox_norm) log.info(f'generated 2D 68PT face landmarks in {(time.time() - st):.2f}s') log.info('') - # ---------------------------------------------------------------------------- - # generate pose from 68 point 2D landmarks - - if opt_run_pose: - log.info('initialize pose...') - from app.processors import face_pose - pose_detector = face_pose.FacePoseDLIB() - log.info('generating pose...') - st = time.time() - pose_data = pose_detector.pose(points_2d_68, dim) - log.info(f'generated pose {(time.time() - st):.2f}s') - log.info('') - - - # x - - - # display if opt_display: - # draw bbox - - # draw 3d landmarks - im_landmarks_3d_68 = im_resized.copy() - draw_utils.draw_landmarks3D(im_landmarks_3d_68, points_3d_68) - draw_utils.draw_bbox(im_landmarks_3d_68, bbox_dim) - - # draw 2d landmarks - im_landmarks_2d_68 = im_resized.copy() - draw_utils.draw_landmarks2D(im_landmarks_2d_68, points_2d_68) - draw_utils.draw_bbox(im_landmarks_2d_68, bbox_dim) - - # draw pose - if opt_run_pose: - im_pose = im_resized.copy() - draw_utils.draw_pose(im_pose, pose_data['point_nose'], pose_data['points']) - draw_utils.draw_degrees(im_pose, pose_data) + # draw landmarks + im_lmarks = im_resized.copy() + im_lmarks = draw_utils.draw_bbox(im_lmarks, bbox_norm) + im_lmarks = draw_utils.draw_landmarks2D(im_lmarks, points_2d_68, radius=1, color=(0,0,255)) + im_lmarks = draw_utils.draw_landmarks3D(im_lmarks, points_3d_68, radius=3, color=(0,255,0)) # draw animated GIF - im = Image.open(opt_fp_out) + im = Image.open(fp_out) im_frames = [] duration = im.info['duration'] + try: while True: im.seek(len(im_frames)) @@ -204,10 +155,7 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_gpu, opt_gif_frames, while True: # show all images here - cv.imshow('Original', im_resized) - cv.imshow('2D 68PT Landmarks', im_landmarks_2d_68) - cv.imshow('3D 68PT Landmarks', im_landmarks_3d_68) - cv.imshow('Pose', im_pose) + cv.imshow('2D/3D 68PT Landmarks', im_lmarks) cv.imshow('3D 68pt GIF', im_frames[frame_number]) frame_number = (frame_number + 1) % n_frames k = cv.waitKey(duration) & 0xFF |
