Spaces:
Running
Running
Update
Browse files- .pre-commit-config.yaml +59 -34
- .style.yapf +0 -5
- .vscode/settings.json +30 -0
- README.md +1 -1
- app.py +36 -48
- requirements.txt +6 -6
- style.css +8 -0
.pre-commit-config.yaml
CHANGED
@@ -1,35 +1,60 @@
|
|
1 |
repos:
|
2 |
-
- repo: https://github.com/pre-commit/pre-commit-hooks
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
- repo: https://github.com/pre-commit/mirrors-mypy
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"]
|
.style.yapf
DELETED
@@ -1,5 +0,0 @@
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
.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,7 +4,7 @@ emoji: 🦀
|
|
4 |
colorFrom: blue
|
5 |
colorTo: pink
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
4 |
colorFrom: blue
|
5 |
colorTo: pink
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.19.2
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
app.py
CHANGED
@@ -14,12 +14,10 @@ import torch
|
|
14 |
import torch.nn as nn
|
15 |
import torch.nn.functional as F
|
16 |
|
17 |
-
DESCRIPTION =
|
18 |
|
19 |
|
20 |
-
def get_model(model_name=
|
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)
|
@@ -28,9 +26,8 @@ def get_model(model_name='se_resnext50_32x4d',
|
|
28 |
|
29 |
|
30 |
def load_model(device):
|
31 |
-
model = get_model(model_name=
|
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()
|
@@ -47,23 +44,11 @@ def load_image(path):
|
|
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 |
-
|
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()
|
@@ -77,55 +62,58 @@ def predict(image, model, face_detector, device, margin=0.4, input_size=224):
|
|
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
|
102 |
return image
|
103 |
|
104 |
|
105 |
-
device = torch.device(
|
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(
|
114 |
-
examples = [path.as_posix() for path in sorted(image_dir.glob(
|
115 |
|
116 |
-
with gr.Blocks(css=
|
117 |
gr.Markdown(DESCRIPTION)
|
118 |
with gr.Row():
|
119 |
with gr.Column():
|
120 |
-
image = gr.Image(label=
|
121 |
-
run_button = gr.Button(
|
122 |
with gr.Column():
|
123 |
-
result = gr.Image(label=
|
124 |
-
|
125 |
-
gr.Examples(
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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", num_classes=101, pretrained="imagenet"):
|
|
|
|
|
21 |
model = pretrainedmodels.__dict__[model_name](pretrained=pretrained)
|
22 |
dim_feats = model.last_linear.in_features
|
23 |
model.last_linear = nn.Linear(dim_feats, num_classes)
|
|
|
26 |
|
27 |
|
28 |
def load_model(device):
|
29 |
+
model = get_model(model_name="se_resnext50_32x4d", pretrained=None)
|
30 |
+
path = huggingface_hub.hf_hub_download("public-data/yu4u-age-estimation-pytorch", "pretrained.pth")
|
|
|
31 |
model.load_state_dict(torch.load(path))
|
32 |
model = model.to(device)
|
33 |
model.eval()
|
|
|
44 |
return image
|
45 |
|
46 |
|
47 |
+
def draw_label(image, point, label, font=cv2.FONT_HERSHEY_SIMPLEX, font_scale=0.8, thickness=1):
|
|
|
|
|
|
|
|
|
|
|
48 |
size = cv2.getTextSize(label, font, font_scale, thickness)[0]
|
49 |
x, y = point
|
50 |
+
cv2.rectangle(image, (x, y - size[1]), (x + size[0], y), (255, 0, 0), cv2.FILLED)
|
51 |
+
cv2.putText(image, label, point, font, font_scale, (255, 255, 255), thickness, lineType=cv2.LINE_AA)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
|
54 |
@torch.inference_mode()
|
|
|
62 |
|
63 |
if len(detected) > 0:
|
64 |
for i, d in enumerate(detected):
|
65 |
+
x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
|
|
|
66 |
xw1 = max(int(x1 - margin * w), 0)
|
67 |
yw1 = max(int(y1 - margin * h), 0)
|
68 |
xw2 = min(int(x2 + margin * w), image_w - 1)
|
69 |
yw2 = min(int(y2 + margin * h), image_h - 1)
|
70 |
+
faces[i] = cv2.resize(image[yw1 : yw2 + 1, xw1 : xw2 + 1], (input_size, input_size))
|
|
|
71 |
|
72 |
cv2.rectangle(image, (x1, y1), (x2, y2), (255, 255, 255), 2)
|
73 |
cv2.rectangle(image, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
|
74 |
|
75 |
# predict ages
|
76 |
+
inputs = torch.from_numpy(np.transpose(faces.astype(np.float32), (0, 3, 1, 2))).to(device)
|
|
|
77 |
outputs = F.softmax(model(inputs), dim=-1).cpu().numpy()
|
78 |
ages = np.arange(0, 101)
|
79 |
predicted_ages = (outputs * ages).sum(axis=-1)
|
80 |
|
81 |
# draw results
|
82 |
for age, d in zip(predicted_ages, detected):
|
83 |
+
draw_label(image, (d.left(), d.top()), f"{int(age)}")
|
84 |
return image
|
85 |
|
86 |
|
87 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
88 |
model = load_model(device)
|
89 |
face_detector = dlib.get_frontal_face_detector()
|
90 |
+
fn = functools.partial(predict, model=model, face_detector=face_detector, device=device)
|
|
|
|
|
|
|
91 |
|
92 |
+
image_dir = pathlib.Path("sample_images")
|
93 |
+
examples = [path.as_posix() for path in sorted(image_dir.glob("*.jpg"))]
|
94 |
|
95 |
+
with gr.Blocks(css="style.css") as demo:
|
96 |
gr.Markdown(DESCRIPTION)
|
97 |
with gr.Row():
|
98 |
with gr.Column():
|
99 |
+
image = gr.Image(label="Input", type="filepath")
|
100 |
+
run_button = gr.Button("Run")
|
101 |
with gr.Column():
|
102 |
+
result = gr.Image(label="Result")
|
103 |
+
|
104 |
+
gr.Examples(
|
105 |
+
examples=examples,
|
106 |
+
inputs=image,
|
107 |
+
outputs=result,
|
108 |
+
fn=fn,
|
109 |
+
cache_examples=os.getenv("CACHE_EXAMPLES") == "1",
|
110 |
+
)
|
111 |
+
run_button.click(
|
112 |
+
fn=fn,
|
113 |
+
inputs=image,
|
114 |
+
outputs=result,
|
115 |
+
api_name="predict",
|
116 |
+
)
|
117 |
+
|
118 |
+
if __name__ == "__main__":
|
119 |
+
demo.queue(max_size=15).launch()
|
requirements.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
-
dlib
|
2 |
-
numpy
|
3 |
-
opencv-python-headless
|
4 |
-
pretrainedmodels
|
5 |
-
torch
|
6 |
-
torchvision
|
|
|
1 |
+
dlib==19.24.2
|
2 |
+
numpy==1.26.4
|
3 |
+
opencv-python-headless==4.9.0.80
|
4 |
+
pretrainedmodels==0.7.4
|
5 |
+
torch==2.0.1
|
6 |
+
torchvision==0.15.2
|
style.css
CHANGED
@@ -1,3 +1,11 @@
|
|
1 |
h1 {
|
2 |
text-align: center;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
}
|
|
|
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 |
}
|