summaryrefslogtreecommitdiff
path: root/megapixels/commands/demo/face_age_gender.py
blob: c4f09c13f0cdb1f3a1ebb71d13b57279a19d8daf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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=True,
  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_gender
  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.DetectorCVDNN()
  bboxes = face_detector.detect(im_resized, largest=True)
  bbox_norm = bboxes[0]
  dim = im_resized.shape[:2][::-1]
  bbox_dim = bbox_norm.to_dim(dim)
  if not bbox_norm:
    log.error('no face detected')
    return
  else:
    log.info(f'face detected: {bbox_dim.to_xyxy()}')


  # ----------------------------------------------------------------------------
  # age

  # real
  age_real_predictor = face_age_gender.FaceAgeReal()
  st = time.time()
  age_real = age_real_predictor.predict(im_resized, bbox_norm)
  log.info(f'age real took: {(time.time()-st)/1000:.5f}s')
  
  # apparent
  age_apparent_predictor = face_age_gender.FaceAgeApparent()
  st = time.time()
  age_apparent = age_apparent_predictor.predict(im_resized, bbox_norm)
  log.info(f'age apparent took: {(time.time()-st)/1000:.5f}s')

  # gender
  gender_predictor = face_age_gender.FaceGender()
  st = time.time()
  gender = gender_predictor.predict(im_resized, bbox_norm)
  log.info(f'gender took: {(time.time()-st)/1000:.5f}s')

  # ethnicity 
  # TODO
  
  # ----------------------------------------------------------------------------
  # 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}')
  log.info(f'gender: {gender}')


  # ----------------------------------------------------------------------------
  # draw

  # draw real age
  im_age_real = im_resized.copy()
  im_age_real = draw_utils.draw_bbox(im_age_real, bbox_norm)
  txt = f'{(age_real):.2f}'
  im_age_real = draw_utils.draw_text(im_age_real, bbox_norm.pt_tl, txt)

  # apparent age
  im_age_apparent = im_resized.copy()
  im_age_apparent = draw_utils.draw_bbox(im_age_apparent, bbox_norm)
  txt = f'{(age_apparent):.2f}'
  im_age_apparent = draw_utils.draw_text(im_age_apparent, bbox_norm.pt_tl, txt)

  # gender
  im_gender = im_resized.copy()
  im_gender = draw_utils.draw_bbox(im_gender, bbox_norm)
  txt = f"M: {gender['m']:.2f}, F: {gender['f']:.2f}"
  im_gender = draw_utils.draw_text(im_gender, (.1, .9), 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)

    fp_out = join(fpp_out.parent, f'{fpp_out.stem}_gender{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)
    cv.imshow('gender', im_gender)
    display_utils.handle_keyboard()