summaryrefslogtreecommitdiff
path: root/megapixels/commands/demo/face_detection.py
diff options
context:
space:
mode:
Diffstat (limited to 'megapixels/commands/demo/face_detection.py')
-rw-r--r--megapixels/commands/demo/face_detection.py128
1 files changed, 128 insertions, 0 deletions
diff --git a/megapixels/commands/demo/face_detection.py b/megapixels/commands/demo/face_detection.py
new file mode 100644
index 00000000..fb23704b
--- /dev/null
+++ b/megapixels/commands/demo/face_detection.py
@@ -0,0 +1,128 @@
+"""
+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
+from app.settings import app_cfg as cfg
+
+
+@click.command()
+@click.option('-i', '--input', 'opt_fp_in', default=None, required=True,
+ help='Image filepath')
+@click.option('-o', '--output', 'opt_fp_out', default=None,
+ help='GIF output path')
+@click.option('--size', 'opt_size',
+ type=(int, int), default=(300, 300),
+ help='Output image size')
+@click.option('-g', '--gpu', 'opt_gpu', default=0,
+ 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,
+ help='Display detections to debug')
+@click.pass_context
+def cli(ctx, opt_fp_in, opt_fp_out, opt_gpu, opt_size, opt_force, opt_display):
+ """Face detector demo"""
+
+ import sys
+ import os
+ from os.path import join
+ from pathlib import Path
+ import time
+
+ from tqdm import tqdm
+ import numpy as np
+ import pandas as pd
+ import cv2 as cv
+ import dlib
+ from PIL import Image
+ import matplotlib.pyplot as plt
+
+ from app.utils import logger_utils, file_utils, im_utils, display_utils, draw_utils
+ from app.utils import plot_utils
+ from app.processors import face_detector, face_landmarks
+ from app.models.data_store import DataStore
+
+
+ log = logger_utils.Logger.getLogger()
+
+
+ # -------------------------------------------------
+ # load image
+
+ im = cv.imread(opt_fp_in)
+ im_resized = im_utils.resize(im, width=opt_size[0], height=opt_size[1])
+
+
+ # ----------------------------------------------------------------------------
+ # detect face
+
+ face_detector = face_detector.DetectorDLIBCNN(gpu=opt_gpu) # -1 for CPU
+ bboxes = face_detector.detect(im_resized, largest=True)
+ bbox = bboxes[0]
+ dim = im_resized.shape[:2][::-1]
+ bbox_dim = bbox.to_dim(dim)
+ if not bbox:
+ log.error('no face detected')
+ return
+
+
+ # ----------------------------------------------------------------------------
+ # generate 68 point landmarks using dlib
+
+ from app.processors import face_landmarks
+ landmark_detector_2d_68 = face_landmarks.Dlib2D_68()
+ points_2d_68 = landmark_detector_2d_68.landmarks(im_resized, bbox_dim)
+
+
+ # ----------------------------------------------------------------------------
+ # generate pose from 68 point 2D landmarks
+
+ from app.processors import face_pose
+ pose_detector = face_pose.FacePoseDLIB()
+ pose_data = pose_detector.pose(points_2d_68, dim)
+
+ # ----------------------------------------------------------------------------
+ # output
+
+ log.info(f'Face coords: {bbox_dim} face')
+ log.info(f'pitch: {pose_data["pitch"]}, roll: {pose_data["roll"]}, yaw: {pose_data["yaw"]}')
+
+
+ # ----------------------------------------------------------------------------
+ # draw
+
+ # 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
+ 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)
+
+
+ # ----------------------------------------------------------------------------
+ # save
+
+ if opt_fp_out:
+ # save pose only
+ cv.imwrite(opt_fp_out, im_pose)
+
+
+ # ----------------------------------------------------------------------------
+ # display
+
+ if opt_display:
+
+
+ # show all images here
+ cv.imshow('Original', im_resized)
+ cv.imshow('2D 68PT Landmarks', im_landmarks_2d_68)
+ cv.imshow('Pose', im_pose)
+ display_utils.handle_keyboard() \ No newline at end of file