summaryrefslogtreecommitdiff
path: root/megapixels/commands/demo/face_gender.py
diff options
context:
space:
mode:
authoradamhrv <adam@ahprojects.com>2019-01-09 02:37:30 +0100
committeradamhrv <adam@ahprojects.com>2019-01-09 02:37:30 +0100
commit62decc28cdfeaa7d62e2fce3f47c82c982008180 (patch)
tree443bcdcce55e429336b7980cef1f919f4171bc61 /megapixels/commands/demo/face_gender.py
parent6586ae9e67d39f11bbd66356730aa126d3bf1269 (diff)
add 3d render
Diffstat (limited to 'megapixels/commands/demo/face_gender.py')
-rw-r--r--megapixels/commands/demo/face_gender.py127
1 files changed, 0 insertions, 127 deletions
diff --git a/megapixels/commands/demo/face_gender.py b/megapixels/commands/demo/face_gender.py
deleted file mode 100644
index ea083fcb..00000000
--- a/megapixels/commands/demo/face_gender.py
+++ /dev/null
@@ -1,127 +0,0 @@
-import click
-
-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 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_age
- 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
- else:
- log.info(f'face detected: {bbox_dim.to_xyxy()}')
-
-
- # ----------------------------------------------------------------------------
- # age
-
- age_apparent_predictor = face_age.FaceAgeApparent()
- age_real_predictor = face_age.FaceAgeReal()
-
- st = time.time()
- age_real = age_real_predictor.age(im_resized, bbox_dim)
- log.info(f'age real took: {(time.time()-st)/1000:.5f}s')
- st = time.time()
- age_apparent = age_apparent_predictor.age(im_resized, bbox_dim)
- log.info(f'age apparent took: {(time.time()-st)/1000:.5f}s')
-
-
- # ----------------------------------------------------------------------------
- # output
-
- log.info(f'Face coords: {bbox_dim} face')
- log.info(f'Age (real): {(age_real):.2f}')
- log.info(f'Age (apparent): {(age_apparent):.2f}')
-
-
- # ----------------------------------------------------------------------------
- # draw
-
- # draw real age
- im_age_real = im_resized.copy()
- draw_utils.draw_bbox(im_age_real, bbox_dim)
- txt = f'{(age_real):.2f}'
- draw_utils.draw_text(im_age_real, bbox_dim.pt_tl, txt)
-
- # apparent
- im_age_apparent = im_resized.copy()
- draw_utils.draw_bbox(im_age_apparent, bbox_dim)
- txt = f'{(age_apparent):.2f}'
- draw_utils.draw_text(im_age_apparent, bbox_dim.pt_tl, txt)
-
-
- # ----------------------------------------------------------------------------
- # save
-
- if opt_fp_out:
- # save pose only
- fpp_out = Path(opt_fp_out)
-
- fp_out = join(fpp_out.parent, f'{fpp_out.stem}_real{fpp_out.suffix}')
- cv.imwrite(fp_out, im_age_real)
-
- fp_out = join(fpp_out.parent, f'{fpp_out.stem}_apparent{fpp_out.suffix}')
- cv.imwrite(fp_out, im_age_apparent)
-
-
- # ----------------------------------------------------------------------------
- # display
-
- if opt_display:
- # show all images here
- cv.imshow('real', im_age_real)
- cv.imshow('apparent', im_age_apparent)
- display_utils.handle_keyboard() \ No newline at end of file