Commit
·
54b1993
0
Parent(s):
Duplicate from hysts/age-estimation-APPA-REAL
Browse filesCo-authored-by: hysts <[email protected]>
- .gitattributes +27 -0
- .pre-commit-config.yaml +35 -0
- .style.yapf +5 -0
- README.md +13 -0
- app.py +131 -0
- packages.txt +1 -0
- requirements.txt +6 -0
- sample_images/README.md +8 -0
- sample_images/pexels-alexey-makhinko-929436.jpg +0 -0
- sample_images/pexels-andrea-piacquadio-2672979.jpg +0 -0
- sample_images/pexels-andrea-piacquadio-774909.jpg +0 -0
- sample_images/pexels-andrea-piacquadio-834863.jpg +0 -0
- sample_images/pexels-linkedin-sales-navigator-2182970.jpg +0 -0
- sample_images/pexels-mentatdgt-937416.jpg +0 -0
- sample_images/pexels-sebastian-voortman-214576.jpg +0 -0
- style.css +3 -0
.gitattributes
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bin.* filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
20 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.pre-commit-config.yaml
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
repos:
|
2 |
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
3 |
+
rev: v4.2.0
|
4 |
+
hooks:
|
5 |
+
- id: check-executables-have-shebangs
|
6 |
+
- id: check-json
|
7 |
+
- id: check-merge-conflict
|
8 |
+
- id: check-shebang-scripts-are-executable
|
9 |
+
- id: check-toml
|
10 |
+
- id: check-yaml
|
11 |
+
- id: double-quote-string-fixer
|
12 |
+
- id: end-of-file-fixer
|
13 |
+
- id: mixed-line-ending
|
14 |
+
args: ['--fix=lf']
|
15 |
+
- id: requirements-txt-fixer
|
16 |
+
- id: trailing-whitespace
|
17 |
+
- repo: https://github.com/myint/docformatter
|
18 |
+
rev: v1.4
|
19 |
+
hooks:
|
20 |
+
- id: docformatter
|
21 |
+
args: ['--in-place']
|
22 |
+
- repo: https://github.com/pycqa/isort
|
23 |
+
rev: 5.12.0
|
24 |
+
hooks:
|
25 |
+
- id: isort
|
26 |
+
- repo: https://github.com/pre-commit/mirrors-mypy
|
27 |
+
rev: v0.991
|
28 |
+
hooks:
|
29 |
+
- id: mypy
|
30 |
+
args: ['--ignore-missing-imports']
|
31 |
+
- repo: https://github.com/google/yapf
|
32 |
+
rev: v0.32.0
|
33 |
+
hooks:
|
34 |
+
- id: yapf
|
35 |
+
args: ['--parallel', '--in-place']
|
.style.yapf
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[style]
|
2 |
+
based_on_style = pep8
|
3 |
+
blank_line_before_nested_class_or_def = false
|
4 |
+
spaces_before_comment = 2
|
5 |
+
split_before_logical_operator = true
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Age Estimation APPA REAL
|
3 |
+
emoji: 🦀
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: pink
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.36.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: hysts/age-estimation-APPA-REAL
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
|
app.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
|
3 |
+
import functools
|
4 |
+
import os
|
5 |
+
import pathlib
|
6 |
+
|
7 |
+
import cv2
|
8 |
+
import dlib
|
9 |
+
import gradio as gr
|
10 |
+
import huggingface_hub
|
11 |
+
import numpy as np
|
12 |
+
import pretrainedmodels
|
13 |
+
import torch
|
14 |
+
import torch.nn as nn
|
15 |
+
import torch.nn.functional as F
|
16 |
+
|
17 |
+
DESCRIPTION = '# [Age Estimation](https://github.com/yu4u/age-estimation-pytorch)'
|
18 |
+
|
19 |
+
|
20 |
+
def get_model(model_name='se_resnext50_32x4d',
|
21 |
+
num_classes=101,
|
22 |
+
pretrained='imagenet'):
|
23 |
+
model = pretrainedmodels.__dict__[model_name](pretrained=pretrained)
|
24 |
+
dim_feats = model.last_linear.in_features
|
25 |
+
model.last_linear = nn.Linear(dim_feats, num_classes)
|
26 |
+
model.avg_pool = nn.AdaptiveAvgPool2d(1)
|
27 |
+
return model
|
28 |
+
|
29 |
+
|
30 |
+
def load_model(device):
|
31 |
+
model = get_model(model_name='se_resnext50_32x4d', pretrained=None)
|
32 |
+
path = huggingface_hub.hf_hub_download(
|
33 |
+
'public-data/yu4u-age-estimation-pytorch', 'pretrained.pth')
|
34 |
+
model.load_state_dict(torch.load(path))
|
35 |
+
model = model.to(device)
|
36 |
+
model.eval()
|
37 |
+
return model
|
38 |
+
|
39 |
+
|
40 |
+
def load_image(path):
|
41 |
+
image = cv2.imread(path)
|
42 |
+
h_orig, w_orig = image.shape[:2]
|
43 |
+
size = max(h_orig, w_orig)
|
44 |
+
scale = 640 / size
|
45 |
+
w, h = int(w_orig * scale), int(h_orig * scale)
|
46 |
+
image = cv2.resize(image, (w, h))
|
47 |
+
return image
|
48 |
+
|
49 |
+
|
50 |
+
def draw_label(image,
|
51 |
+
point,
|
52 |
+
label,
|
53 |
+
font=cv2.FONT_HERSHEY_SIMPLEX,
|
54 |
+
font_scale=0.8,
|
55 |
+
thickness=1):
|
56 |
+
size = cv2.getTextSize(label, font, font_scale, thickness)[0]
|
57 |
+
x, y = point
|
58 |
+
cv2.rectangle(image, (x, y - size[1]), (x + size[0], y), (255, 0, 0),
|
59 |
+
cv2.FILLED)
|
60 |
+
cv2.putText(image,
|
61 |
+
label,
|
62 |
+
point,
|
63 |
+
font,
|
64 |
+
font_scale, (255, 255, 255),
|
65 |
+
thickness,
|
66 |
+
lineType=cv2.LINE_AA)
|
67 |
+
|
68 |
+
|
69 |
+
@torch.inference_mode()
|
70 |
+
def predict(image, model, face_detector, device, margin=0.4, input_size=224):
|
71 |
+
image = cv2.imread(image, cv2.IMREAD_COLOR)[:, :, ::-1].copy()
|
72 |
+
image_h, image_w = image.shape[:2]
|
73 |
+
|
74 |
+
# detect faces using dlib detector
|
75 |
+
detected = face_detector(image, 1)
|
76 |
+
faces = np.empty((len(detected), input_size, input_size, 3))
|
77 |
+
|
78 |
+
if len(detected) > 0:
|
79 |
+
for i, d in enumerate(detected):
|
80 |
+
x1, y1, x2, y2, w, h = d.left(), d.top(
|
81 |
+
), d.right() + 1, d.bottom() + 1, d.width(), d.height()
|
82 |
+
xw1 = max(int(x1 - margin * w), 0)
|
83 |
+
yw1 = max(int(y1 - margin * h), 0)
|
84 |
+
xw2 = min(int(x2 + margin * w), image_w - 1)
|
85 |
+
yw2 = min(int(y2 + margin * h), image_h - 1)
|
86 |
+
faces[i] = cv2.resize(image[yw1:yw2 + 1, xw1:xw2 + 1],
|
87 |
+
(input_size, input_size))
|
88 |
+
|
89 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (255, 255, 255), 2)
|
90 |
+
cv2.rectangle(image, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
|
91 |
+
|
92 |
+
# predict ages
|
93 |
+
inputs = torch.from_numpy(
|
94 |
+
np.transpose(faces.astype(np.float32), (0, 3, 1, 2))).to(device)
|
95 |
+
outputs = F.softmax(model(inputs), dim=-1).cpu().numpy()
|
96 |
+
ages = np.arange(0, 101)
|
97 |
+
predicted_ages = (outputs * ages).sum(axis=-1)
|
98 |
+
|
99 |
+
# draw results
|
100 |
+
for age, d in zip(predicted_ages, detected):
|
101 |
+
draw_label(image, (d.left(), d.top()), f'{int(age)}')
|
102 |
+
return image
|
103 |
+
|
104 |
+
|
105 |
+
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
106 |
+
model = load_model(device)
|
107 |
+
face_detector = dlib.get_frontal_face_detector()
|
108 |
+
fn = functools.partial(predict,
|
109 |
+
model=model,
|
110 |
+
face_detector=face_detector,
|
111 |
+
device=device)
|
112 |
+
|
113 |
+
image_dir = pathlib.Path('sample_images')
|
114 |
+
examples = [path.as_posix() for path in sorted(image_dir.glob('*.jpg'))]
|
115 |
+
|
116 |
+
with gr.Blocks(css='style.css') as demo:
|
117 |
+
gr.Markdown(DESCRIPTION)
|
118 |
+
with gr.Row():
|
119 |
+
with gr.Column():
|
120 |
+
image = gr.Image(label='Input', type='filepath')
|
121 |
+
run_button = gr.Button('Run')
|
122 |
+
with gr.Column():
|
123 |
+
result = gr.Image(label='Result')
|
124 |
+
|
125 |
+
gr.Examples(examples=examples,
|
126 |
+
inputs=image,
|
127 |
+
outputs=result,
|
128 |
+
fn=fn,
|
129 |
+
cache_examples=os.getenv('CACHE_EXAMPLES') == '1')
|
130 |
+
run_button.click(fn=fn, inputs=image, outputs=result, api_name='predict')
|
131 |
+
demo.queue(max_size=15).launch()
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
cmake
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
dlib>=19.23
|
2 |
+
numpy>=1.22.2
|
3 |
+
opencv-python-headless>=4.5.5.62
|
4 |
+
pretrainedmodels>=0.7.4
|
5 |
+
torch>=1.10.2
|
6 |
+
torchvision>=0.11.3
|
sample_images/README.md
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
These images are from the following public domain:
|
2 |
+
- https://www.pexels.com/photo/2-women-sitting-on-rock-during-daytime-214576/
|
3 |
+
- https://www.pexels.com/photo/boy-in-yellow-crew-neck-t-shirt-and-gray-bottoms-929436/
|
4 |
+
- https://www.pexels.com/photo/group-of-people-standing-beside-body-of-water-2672979/
|
5 |
+
- https://www.pexels.com/photo/man-sitting-on-chair-beside-table-834863/
|
6 |
+
- https://www.pexels.com/photo/man-wearing-white-dress-shirt-and-black-blazer-2182970/
|
7 |
+
- https://www.pexels.com/photo/shallow-focus-photography-of-woman-in-white-shirt-and-blue-denim-shorts-on-street-near-green-trees-937416/
|
8 |
+
- https://www.pexels.com/photo/woman-in-collared-shirt-774909/
|
sample_images/pexels-alexey-makhinko-929436.jpg
ADDED
![]() |
sample_images/pexels-andrea-piacquadio-2672979.jpg
ADDED
![]() |
sample_images/pexels-andrea-piacquadio-774909.jpg
ADDED
![]() |
sample_images/pexels-andrea-piacquadio-834863.jpg
ADDED
![]() |
sample_images/pexels-linkedin-sales-navigator-2182970.jpg
ADDED
![]() |
sample_images/pexels-mentatdgt-937416.jpg
ADDED
![]() |
sample_images/pexels-sebastian-voortman-214576.jpg
ADDED
![]() |
style.css
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
h1 {
|
2 |
+
text-align: center;
|
3 |
+
}
|