Spaces:
Runtime error
Runtime error
Update
Browse files- .gitattributes +1 -0
- .gitignore +0 -1
- .pre-commit-config.yaml +60 -0
- .vscode/settings.json +30 -0
- README.md +2 -1
- app.py +43 -105
- images/README.md +8 -0
- images/pexels-alexey-makhinko-929436.jpg +3 -0
- images/pexels-andrea-piacquadio-2672979.jpg +3 -0
- images/pexels-andrea-piacquadio-774909.jpg +3 -0
- images/pexels-andrea-piacquadio-834863.jpg +3 -0
- images/pexels-linkedin-sales-navigator-2182970.jpg +3 -0
- images/pexels-mentatdgt-937416.jpg +3 -0
- images/pexels-sebastian-voortman-214576.jpg +3 -0
- requirements.txt +5 -5
- style.css +11 -0
.gitattributes
CHANGED
@@ -26,3 +26,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
26 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
27 |
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
28 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
26 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
27 |
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
28 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
.gitignore
DELETED
@@ -1 +0,0 @@
|
|
1 |
-
images
|
|
|
|
.pre-commit-config.yaml
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
repos:
|
2 |
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
3 |
+
rev: v4.5.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: end-of-file-fixer
|
12 |
+
- id: mixed-line-ending
|
13 |
+
args: ["--fix=lf"]
|
14 |
+
- id: requirements-txt-fixer
|
15 |
+
- id: trailing-whitespace
|
16 |
+
- repo: https://github.com/myint/docformatter
|
17 |
+
rev: v1.7.5
|
18 |
+
hooks:
|
19 |
+
- id: docformatter
|
20 |
+
args: ["--in-place"]
|
21 |
+
- repo: https://github.com/pycqa/isort
|
22 |
+
rev: 5.13.2
|
23 |
+
hooks:
|
24 |
+
- id: isort
|
25 |
+
args: ["--profile", "black"]
|
26 |
+
- repo: https://github.com/pre-commit/mirrors-mypy
|
27 |
+
rev: v1.8.0
|
28 |
+
hooks:
|
29 |
+
- id: mypy
|
30 |
+
args: ["--ignore-missing-imports"]
|
31 |
+
additional_dependencies:
|
32 |
+
[
|
33 |
+
"types-python-slugify",
|
34 |
+
"types-requests",
|
35 |
+
"types-PyYAML",
|
36 |
+
"types-pytz",
|
37 |
+
]
|
38 |
+
- repo: https://github.com/psf/black
|
39 |
+
rev: 24.2.0
|
40 |
+
hooks:
|
41 |
+
- id: black
|
42 |
+
language_version: python3.10
|
43 |
+
args: ["--line-length", "119"]
|
44 |
+
- repo: https://github.com/kynan/nbstripout
|
45 |
+
rev: 0.7.1
|
46 |
+
hooks:
|
47 |
+
- id: nbstripout
|
48 |
+
args:
|
49 |
+
[
|
50 |
+
"--extra-keys",
|
51 |
+
"metadata.interpreter metadata.kernelspec cell.metadata.pycharm",
|
52 |
+
]
|
53 |
+
- repo: https://github.com/nbQA-dev/nbQA
|
54 |
+
rev: 1.7.1
|
55 |
+
hooks:
|
56 |
+
- id: nbqa-black
|
57 |
+
- id: nbqa-pyupgrade
|
58 |
+
args: ["--py37-plus"]
|
59 |
+
- id: nbqa-isort
|
60 |
+
args: ["--float-to-top"]
|
.vscode/settings.json
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"editor.formatOnSave": true,
|
3 |
+
"files.insertFinalNewline": false,
|
4 |
+
"[python]": {
|
5 |
+
"editor.defaultFormatter": "ms-python.black-formatter",
|
6 |
+
"editor.formatOnType": true,
|
7 |
+
"editor.codeActionsOnSave": {
|
8 |
+
"source.organizeImports": "explicit"
|
9 |
+
}
|
10 |
+
},
|
11 |
+
"[jupyter]": {
|
12 |
+
"files.insertFinalNewline": false
|
13 |
+
},
|
14 |
+
"black-formatter.args": [
|
15 |
+
"--line-length=119"
|
16 |
+
],
|
17 |
+
"isort.args": ["--profile", "black"],
|
18 |
+
"flake8.args": [
|
19 |
+
"--max-line-length=119"
|
20 |
+
],
|
21 |
+
"ruff.lint.args": [
|
22 |
+
"--line-length=119"
|
23 |
+
],
|
24 |
+
"notebook.output.scrolling": true,
|
25 |
+
"notebook.formatOnCellExecution": true,
|
26 |
+
"notebook.formatOnSave.enabled": true,
|
27 |
+
"notebook.codeActionsOnSave": {
|
28 |
+
"source.organizeImports": "explicit"
|
29 |
+
}
|
30 |
+
}
|
README.md
CHANGED
@@ -4,9 +4,10 @@ emoji: 🏢
|
|
4 |
colorFrom: gray
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
|
|
|
4 |
colorFrom: gray
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.19.2
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
short_description: age estimation
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
|
app.py
CHANGED
@@ -2,12 +2,9 @@
|
|
2 |
|
3 |
from __future__ import annotations
|
4 |
|
5 |
-
import argparse
|
6 |
-
import functools
|
7 |
import os
|
8 |
import pathlib
|
9 |
import sys
|
10 |
-
import tarfile
|
11 |
|
12 |
import cv2
|
13 |
import gradio as gr
|
@@ -15,78 +12,31 @@ import huggingface_hub
|
|
15 |
import numpy as np
|
16 |
import torch
|
17 |
|
18 |
-
sys.path.insert(0,
|
19 |
-
sys.path.insert(0,
|
20 |
-
sys.path.insert(0,
|
21 |
-
sys.path.insert(0,
|
22 |
|
23 |
from ibug.age_estimation import AgeEstimator
|
24 |
from ibug.face_detection import RetinaFacePredictor
|
25 |
from ibug.face_parsing.utils import label_colormap
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
parser.add_argument('--port', type=int)
|
41 |
-
parser.add_argument('--disable-queue',
|
42 |
-
dest='enable_queue',
|
43 |
-
action='store_false')
|
44 |
-
parser.add_argument('--allow-flagging', type=str, default='never')
|
45 |
-
return parser.parse_args()
|
46 |
-
|
47 |
-
|
48 |
-
def load_sample_images() -> list[pathlib.Path]:
|
49 |
-
image_dir = pathlib.Path('images')
|
50 |
-
if not image_dir.exists():
|
51 |
-
image_dir.mkdir()
|
52 |
-
dataset_repo = 'hysts/input-images'
|
53 |
-
filenames = ['003.tar']
|
54 |
-
for name in filenames:
|
55 |
-
path = huggingface_hub.hf_hub_download(dataset_repo,
|
56 |
-
name,
|
57 |
-
repo_type='dataset',
|
58 |
-
use_auth_token=TOKEN)
|
59 |
-
with tarfile.open(path) as f:
|
60 |
-
f.extractall(image_dir.as_posix())
|
61 |
-
return sorted(image_dir.rglob('*.jpg'))
|
62 |
-
|
63 |
-
|
64 |
-
def load_detector(device: torch.device) -> RetinaFacePredictor:
|
65 |
-
model = RetinaFacePredictor(
|
66 |
-
threshold=0.8,
|
67 |
-
device=device,
|
68 |
-
model=RetinaFacePredictor.get_model('mobilenet0.25'))
|
69 |
-
return model
|
70 |
-
|
71 |
-
|
72 |
-
def load_model(device: torch.device) -> AgeEstimator:
|
73 |
-
ckpt_path = huggingface_hub.hf_hub_download(
|
74 |
-
'hysts/ibug',
|
75 |
-
'fpage/models/fpage-resnet50-fcn-14-97.torch',
|
76 |
-
use_auth_token=TOKEN)
|
77 |
-
model = AgeEstimator(
|
78 |
-
device=device,
|
79 |
-
ckpt=ckpt_path,
|
80 |
-
encoder='resnet50',
|
81 |
-
decoder='fcn',
|
82 |
-
age_classes=97,
|
83 |
-
face_classes=14,
|
84 |
-
)
|
85 |
-
return model
|
86 |
|
87 |
|
88 |
-
def predict(image: np.ndarray, max_num_faces: int
|
89 |
-
detector: RetinaFacePredictor, model: AgeEstimator) -> np.ndarray:
|
90 |
colormap = label_colormap(14)
|
91 |
|
92 |
# RGB -> BGR
|
@@ -94,7 +44,7 @@ def predict(image: np.ndarray, max_num_faces: int,
|
|
94 |
|
95 |
faces = detector(image, rgb=False)
|
96 |
if len(faces) == 0:
|
97 |
-
raise RuntimeError(
|
98 |
faces = sorted(list(faces), key=lambda x: -x[4])[:max_num_faces][::-1]
|
99 |
ages, masks = model.predict_img(image, faces, rgb=False)
|
100 |
|
@@ -116,7 +66,7 @@ def predict(image: np.ndarray, max_num_faces: int,
|
|
116 |
thickness=2,
|
117 |
)
|
118 |
|
119 |
-
text_content = f
|
120 |
cv2.putText(
|
121 |
res,
|
122 |
text_content,
|
@@ -130,40 +80,28 @@ def predict(image: np.ndarray, max_num_faces: int,
|
|
130 |
return res[:, :, ::-1]
|
131 |
|
132 |
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
examples=examples,
|
155 |
-
title=TITLE,
|
156 |
-
description=DESCRIPTION,
|
157 |
-
article=ARTICLE,
|
158 |
-
theme=args.theme,
|
159 |
-
allow_flagging=args.allow_flagging,
|
160 |
-
live=args.live,
|
161 |
-
).launch(
|
162 |
-
enable_queue=args.enable_queue,
|
163 |
-
server_port=args.port,
|
164 |
-
share=args.share,
|
165 |
)
|
166 |
|
167 |
-
|
168 |
-
|
169 |
-
main()
|
|
|
2 |
|
3 |
from __future__ import annotations
|
4 |
|
|
|
|
|
5 |
import os
|
6 |
import pathlib
|
7 |
import sys
|
|
|
8 |
|
9 |
import cv2
|
10 |
import gradio as gr
|
|
|
12 |
import numpy as np
|
13 |
import torch
|
14 |
|
15 |
+
sys.path.insert(0, "face_detection")
|
16 |
+
sys.path.insert(0, "face_parsing")
|
17 |
+
sys.path.insert(0, "fpage")
|
18 |
+
sys.path.insert(0, "roi_tanh_warping")
|
19 |
|
20 |
from ibug.age_estimation import AgeEstimator
|
21 |
from ibug.face_detection import RetinaFacePredictor
|
22 |
from ibug.face_parsing.utils import label_colormap
|
23 |
|
24 |
+
DESCRIPTION = "# [FP-Age](https://github.com/ibug-group/fpage)"
|
25 |
+
|
26 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
27 |
+
|
28 |
+
detector = RetinaFacePredictor(threshold=0.8, device=device, model=RetinaFacePredictor.get_model("mobilenet0.25"))
|
29 |
+
model = AgeEstimator(
|
30 |
+
device=device,
|
31 |
+
ckpt=huggingface_hub.hf_hub_download("hysts/ibug", "fpage/models/fpage-resnet50-fcn-14-97.torch"),
|
32 |
+
encoder="resnet50",
|
33 |
+
decoder="fcn",
|
34 |
+
age_classes=97,
|
35 |
+
face_classes=14,
|
36 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
|
39 |
+
def predict(image: np.ndarray, max_num_faces: int) -> np.ndarray:
|
|
|
40 |
colormap = label_colormap(14)
|
41 |
|
42 |
# RGB -> BGR
|
|
|
44 |
|
45 |
faces = detector(image, rgb=False)
|
46 |
if len(faces) == 0:
|
47 |
+
raise RuntimeError("No face was found.")
|
48 |
faces = sorted(list(faces), key=lambda x: -x[4])[:max_num_faces][::-1]
|
49 |
ages, masks = model.predict_img(image, faces, rgb=False)
|
50 |
|
|
|
66 |
thickness=2,
|
67 |
)
|
68 |
|
69 |
+
text_content = f"Age: ({age: .1f})"
|
70 |
cv2.putText(
|
71 |
res,
|
72 |
text_content,
|
|
|
80 |
return res[:, :, ::-1]
|
81 |
|
82 |
|
83 |
+
with gr.Blocks(css="style.css") as demo:
|
84 |
+
gr.Markdown(DESCRIPTION)
|
85 |
+
with gr.Row():
|
86 |
+
with gr.Column():
|
87 |
+
image = gr.Image(label="Input")
|
88 |
+
max_num_faces = gr.Slider(label="Max Number of Faces", minimum=1, maximum=20, step=1, value=5)
|
89 |
+
run_button = gr.Button()
|
90 |
+
with gr.Column():
|
91 |
+
result = gr.Image(label="Output")
|
92 |
+
gr.Examples(
|
93 |
+
examples=[[path.as_posix(), 5] for path in sorted(pathlib.Path("images").rglob("*.jpg"))],
|
94 |
+
inputs=[image, max_num_faces],
|
95 |
+
outputs=result,
|
96 |
+
fn=predict,
|
97 |
+
cache_examples=os.getenv("CACHE_EXAMPLES") == "1",
|
98 |
+
)
|
99 |
+
run_button.click(
|
100 |
+
fn=predict,
|
101 |
+
inputs=[image, max_num_faces],
|
102 |
+
outputs=result,
|
103 |
+
api_name="predict",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
)
|
105 |
|
106 |
+
if __name__ == "__main__":
|
107 |
+
demo.queue(max_size=20).launch()
|
|
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/
|
images/pexels-alexey-makhinko-929436.jpg
ADDED
![]() |
Git LFS Details
|
images/pexels-andrea-piacquadio-2672979.jpg
ADDED
![]() |
Git LFS Details
|
images/pexels-andrea-piacquadio-774909.jpg
ADDED
![]() |
Git LFS Details
|
images/pexels-andrea-piacquadio-834863.jpg
ADDED
![]() |
Git LFS Details
|
images/pexels-linkedin-sales-navigator-2182970.jpg
ADDED
![]() |
Git LFS Details
|
images/pexels-mentatdgt-937416.jpg
ADDED
![]() |
Git LFS Details
|
images/pexels-sebastian-voortman-214576.jpg
ADDED
![]() |
Git LFS Details
|
requirements.txt
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
-
numpy==1.
|
2 |
-
opencv-python-headless==4.
|
3 |
-
timm==0.
|
4 |
-
torch==
|
5 |
-
torchvision==0.
|
|
|
1 |
+
numpy==1.26.4
|
2 |
+
opencv-python-headless==4.9.0.80
|
3 |
+
timm==0.9.16
|
4 |
+
torch==2.0.1
|
5 |
+
torchvision==0.15.2
|
style.css
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
h1 {
|
2 |
+
text-align: center;
|
3 |
+
display: block;
|
4 |
+
}
|
5 |
+
|
6 |
+
#duplicate-button {
|
7 |
+
margin: auto;
|
8 |
+
color: #fff;
|
9 |
+
background: #1565c0;
|
10 |
+
border-radius: 100vh;
|
11 |
+
}
|