diff options
| author | adamhrv <adam@ahprojects.com> | 2019-01-08 01:13:22 +0100 |
|---|---|---|
| committer | adamhrv <adam@ahprojects.com> | 2019-01-08 01:13:22 +0100 |
| commit | 6586ae9e67d39f11bbd66356730aa126d3bf1269 (patch) | |
| tree | 09b81ec0db1ec144e81d5d7c4d1846499b254a0d /megapixels/commands | |
| parent | 807503666f06c5b22b752e32362a0b4ceacc1a5e (diff) | |
add age (real, apparent), gender predictor
Diffstat (limited to 'megapixels/commands')
| -rw-r--r-- | megapixels/commands/demo/face_age_gender.py (renamed from megapixels/commands/demo/face_age.py) | 61 | ||||
| -rw-r--r-- | megapixels/commands/demo/face_gender.py | 53 |
2 files changed, 87 insertions, 27 deletions
diff --git a/megapixels/commands/demo/face_age.py b/megapixels/commands/demo/face_age_gender.py index 45ae5190..477404a5 100644 --- a/megapixels/commands/demo/face_age.py +++ b/megapixels/commands/demo/face_age_gender.py @@ -37,7 +37,7 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_gpu, opt_size, opt_force, opt_display): 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.processors import face_detector, face_age_gender from app.models.data_store import DataStore @@ -68,25 +68,53 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_gpu, opt_size, opt_force, opt_display): # ---------------------------------------------------------------------------- # age - age_predictor = face_age.FaceAge(gpu=opt_gpu) - age_score = age_predictor.age(im_resized, bbox_dim) + # real + age_real_predictor = face_age_gender.FaceAgeReal() + st = time.time() + age_real = age_real_predictor.predict(im_resized, bbox_dim) + 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_dim) + 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_dim) + log.info(f'gender took: {(time.time()-st)/1000:.5f}s') # ---------------------------------------------------------------------------- # output log.info(f'Face coords: {bbox_dim} face') - log.info(f'age score: {(100*age_score):.2f}') + log.info(f'Age (real): {(age_real):.2f}') + log.info(f'Age (apparent): {(age_apparent):.2f}') + log.info(f'gender: {gender}') # ---------------------------------------------------------------------------- # draw - # draw 2d landmarks - im_age = im_resized.copy() - draw_utils.draw_bbox(im_age, bbox_dim) - txt = f'age score: {(100*age_score):.2f}' - draw_utils.draw_text(im_age, bbox_dim.pt_tl, txt) + # 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 age + 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) + + # gender + im_gender = im_resized.copy() + draw_utils.draw_bbox(im_age_apparent, bbox_dim) + txt = f"M: {gender['m']:.2f}, F: {gender['f']:.2f}" + draw_utils.draw_text(im_gender, (10, dim[1]-20), txt) # ---------------------------------------------------------------------------- @@ -94,7 +122,16 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_gpu, opt_size, opt_force, opt_display): if opt_fp_out: # save pose only - cv.imwrite(opt_fp_out, im_age) + 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) # ---------------------------------------------------------------------------- @@ -102,5 +139,7 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_gpu, opt_size, opt_force, opt_display): if opt_display: # show all images here - cv.imshow('age', im_age) + cv.imshow('real', im_age_real) + cv.imshow('apparent', im_age_apparent) + cv.imshow('gender', im_gender) display_utils.handle_keyboard()
\ No newline at end of file diff --git a/megapixels/commands/demo/face_gender.py b/megapixels/commands/demo/face_gender.py index 8e8c86f3..ea083fcb 100644 --- a/megapixels/commands/demo/face_gender.py +++ b/megapixels/commands/demo/face_gender.py @@ -7,12 +7,12 @@ from app.settings import app_cfg as cfg @click.command() @click.option('-i', '--input', 'opt_fp_in', default=None, required=True, - help='Imgender filepath') + 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 imgender size') + help='Output image size') @click.option('-g', '--gpu', 'opt_gpu', default=0, help='GPU index') @click.option('-f', '--force', 'opt_force', is_flag=True, @@ -37,7 +37,7 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_gpu, opt_size, opt_force, opt_display): 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_gender + from app.processors import face_detector, face_age from app.models.data_store import DataStore @@ -45,7 +45,7 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_gpu, opt_size, opt_force, opt_display): # ------------------------------------------------- - # load imgender + # load image im = cv.imread(opt_fp_in) im_resized = im_utils.resize(im, width=opt_size[0], height=opt_size[1]) @@ -66,27 +66,41 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_gpu, opt_size, opt_force, opt_display): # ---------------------------------------------------------------------------- - # gender + # age - gender_predictor = face_gender.FaceGender(gpu=opt_gpu) - gender_score = gender_predictor.gender(im_resized, bbox_dim) + 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'gender score: {(100*gender_score):.2f}') + log.info(f'Age (real): {(age_real):.2f}') + log.info(f'Age (apparent): {(age_apparent):.2f}') # ---------------------------------------------------------------------------- # draw - # draw 2d landmarks - im_gender = im_resized.copy() - draw_utils.draw_bbox(im_gender, bbox_dim) - txt = f'gender score: {(100*gender_score):.2f}' - draw_utils.draw_text(im_gender, bbox_dim.pt_tl, txt) + # 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) # ---------------------------------------------------------------------------- @@ -94,13 +108,20 @@ def cli(ctx, opt_fp_in, opt_fp_out, opt_gpu, opt_size, opt_force, opt_display): if opt_fp_out: # save pose only - cv.imwrite(opt_fp_out, im_gender) + 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 imgenders here - cv.imshow('gender', im_gender) + # 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 |
