Update
Browse files- .pre-commit-config.yaml +59 -34
- .style.yapf +0 -5
- .vscode/settings.json +30 -0
- README.md +2 -1
- app.py +44 -51
- requirements.txt +4 -4
- style.css +4 -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,9 +4,10 @@ emoji: 📚
|
|
4 |
colorFrom: purple
|
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: purple
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.19.2
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
short_description: head pose estimation
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
|
app.py
CHANGED
@@ -19,19 +19,18 @@ import torchvision
|
|
19 |
import torchvision.transforms as T
|
20 |
from scipy.spatial.transform import Rotation
|
21 |
|
22 |
-
sys.path.insert(0,
|
23 |
-
sys.path.insert(0,
|
24 |
|
25 |
from hopenet import Hopenet
|
26 |
from ibug.face_detection import RetinaFacePredictor
|
27 |
|
28 |
-
DESCRIPTION =
|
29 |
|
30 |
|
31 |
def load_model(model_name: str, device: torch.device) -> nn.Module:
|
32 |
-
path = huggingface_hub.hf_hub_download(
|
33 |
-
|
34 |
-
state_dict = torch.load(path, map_location='cpu')
|
35 |
model = Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66)
|
36 |
model.load_state_dict(state_dict)
|
37 |
model.to(device)
|
@@ -40,12 +39,14 @@ def load_model(model_name: str, device: torch.device) -> nn.Module:
|
|
40 |
|
41 |
|
42 |
def create_transform() -> Callable:
|
43 |
-
transform = T.Compose(
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
49 |
return transform
|
50 |
|
51 |
|
@@ -66,8 +67,7 @@ def crop_face(image: np.ndarray, box: tuple[int, int, int, int]) -> np.ndarray:
|
|
66 |
|
67 |
|
68 |
@torch.inference_mode()
|
69 |
-
def predict(image: np.ndarray, transform: Callable, model: nn.Module,
|
70 |
-
device: torch.device) -> np.ndarray:
|
71 |
indices = torch.arange(66).float().to(device)
|
72 |
|
73 |
image = PIL.Image.fromarray(image)
|
@@ -84,12 +84,11 @@ def predict(image: np.ndarray, transform: Callable, model: nn.Module,
|
|
84 |
return out
|
85 |
|
86 |
|
87 |
-
def draw_axis(image: np.ndarray, pose: np.ndarray, origin: np.ndarray,
|
88 |
-
length: int) -> None:
|
89 |
# (yaw, pitch, roll) -> (roll, yaw, pitch)
|
90 |
pose = pose[[2, 0, 1]]
|
91 |
pose *= np.array([1, -1, 1])
|
92 |
-
rot = Rotation.from_euler(
|
93 |
|
94 |
vectors = rot.as_matrix().T[:, :2] # shape: (3, 2)
|
95 |
pts = np.round(vectors * length + origin).astype(int)
|
@@ -99,9 +98,14 @@ def draw_axis(image: np.ndarray, pose: np.ndarray, origin: np.ndarray,
|
|
99 |
cv2.line(image, tuple(origin), tuple(pts[2]), (255, 0, 0), 2)
|
100 |
|
101 |
|
102 |
-
def run(
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
105 |
model = models[model_name]
|
106 |
|
107 |
# RGB -> BGR
|
@@ -124,47 +128,36 @@ def run(image: np.ndarray, model_name: str, face_detector: RetinaFacePredictor,
|
|
124 |
return res[:, :, ::-1]
|
125 |
|
126 |
|
127 |
-
device = torch.device(
|
128 |
-
face_detector = RetinaFacePredictor(
|
129 |
-
threshold=0.8,
|
130 |
-
device=device,
|
131 |
-
model=RetinaFacePredictor.get_model('mobilenet0.25'))
|
132 |
|
133 |
model_names = [
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
]
|
138 |
models = {name: load_model(name, device) for name in model_names}
|
139 |
transform = create_transform()
|
140 |
|
141 |
-
fn = functools.partial(run,
|
142 |
-
face_detector=face_detector,
|
143 |
-
models=models,
|
144 |
-
transform=transform,
|
145 |
-
device=device)
|
146 |
|
147 |
-
examples = [[
|
148 |
|
149 |
-
with gr.Blocks(css=
|
150 |
gr.Markdown(DESCRIPTION)
|
151 |
with gr.Row():
|
152 |
with gr.Column():
|
153 |
-
image = gr.Image(label=
|
154 |
-
model_name = gr.Radio(label=
|
155 |
-
|
156 |
-
type='value',
|
157 |
-
value=model_names[0])
|
158 |
-
run_button = gr.Button('Run')
|
159 |
with gr.Column():
|
160 |
-
result = gr.Image(label=
|
161 |
-
gr.Examples(
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
api_name='run')
|
170 |
demo.queue().launch()
|
|
|
19 |
import torchvision.transforms as T
|
20 |
from scipy.spatial.transform import Rotation
|
21 |
|
22 |
+
sys.path.insert(0, "face_detection")
|
23 |
+
sys.path.insert(0, "deep-head-pose/code")
|
24 |
|
25 |
from hopenet import Hopenet
|
26 |
from ibug.face_detection import RetinaFacePredictor
|
27 |
|
28 |
+
DESCRIPTION = "# [Hopenet](https://github.com/natanielruiz/deep-head-pose)"
|
29 |
|
30 |
|
31 |
def load_model(model_name: str, device: torch.device) -> nn.Module:
|
32 |
+
path = huggingface_hub.hf_hub_download("public-data/Hopenet", f"models/{model_name}.pkl")
|
33 |
+
state_dict = torch.load(path, map_location="cpu")
|
|
|
34 |
model = Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66)
|
35 |
model.load_state_dict(state_dict)
|
36 |
model.to(device)
|
|
|
39 |
|
40 |
|
41 |
def create_transform() -> Callable:
|
42 |
+
transform = T.Compose(
|
43 |
+
[
|
44 |
+
T.Resize(224),
|
45 |
+
T.CenterCrop(224),
|
46 |
+
T.ToTensor(),
|
47 |
+
T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
48 |
+
]
|
49 |
+
)
|
50 |
return transform
|
51 |
|
52 |
|
|
|
67 |
|
68 |
|
69 |
@torch.inference_mode()
|
70 |
+
def predict(image: np.ndarray, transform: Callable, model: nn.Module, device: torch.device) -> np.ndarray:
|
|
|
71 |
indices = torch.arange(66).float().to(device)
|
72 |
|
73 |
image = PIL.Image.fromarray(image)
|
|
|
84 |
return out
|
85 |
|
86 |
|
87 |
+
def draw_axis(image: np.ndarray, pose: np.ndarray, origin: np.ndarray, length: int) -> None:
|
|
|
88 |
# (yaw, pitch, roll) -> (roll, yaw, pitch)
|
89 |
pose = pose[[2, 0, 1]]
|
90 |
pose *= np.array([1, -1, 1])
|
91 |
+
rot = Rotation.from_euler("zyx", pose, degrees=True)
|
92 |
|
93 |
vectors = rot.as_matrix().T[:, :2] # shape: (3, 2)
|
94 |
pts = np.round(vectors * length + origin).astype(int)
|
|
|
98 |
cv2.line(image, tuple(origin), tuple(pts[2]), (255, 0, 0), 2)
|
99 |
|
100 |
|
101 |
+
def run(
|
102 |
+
image: np.ndarray,
|
103 |
+
model_name: str,
|
104 |
+
face_detector: RetinaFacePredictor,
|
105 |
+
models: dict[str, nn.Module],
|
106 |
+
transform: Callable,
|
107 |
+
device: torch.device,
|
108 |
+
) -> np.ndarray:
|
109 |
model = models[model_name]
|
110 |
|
111 |
# RGB -> BGR
|
|
|
128 |
return res[:, :, ::-1]
|
129 |
|
130 |
|
131 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
132 |
+
face_detector = RetinaFacePredictor(threshold=0.8, device=device, model=RetinaFacePredictor.get_model("mobilenet0.25"))
|
|
|
|
|
|
|
133 |
|
134 |
model_names = [
|
135 |
+
"hopenet_alpha1",
|
136 |
+
"hopenet_alpha2",
|
137 |
+
"hopenet_robust_alpha1",
|
138 |
]
|
139 |
models = {name: load_model(name, device) for name in model_names}
|
140 |
transform = create_transform()
|
141 |
|
142 |
+
fn = functools.partial(run, face_detector=face_detector, models=models, transform=transform, device=device)
|
|
|
|
|
|
|
|
|
143 |
|
144 |
+
examples = [["images/pexels-ksenia-chernaya-8535230.jpg", "hopenet_alpha1"]]
|
145 |
|
146 |
+
with gr.Blocks(css="style.css") as demo:
|
147 |
gr.Markdown(DESCRIPTION)
|
148 |
with gr.Row():
|
149 |
with gr.Column():
|
150 |
+
image = gr.Image(label="Input", type="numpy")
|
151 |
+
model_name = gr.Radio(label="Model", choices=model_names, type="value", value=model_names[0])
|
152 |
+
run_button = gr.Button("Run")
|
|
|
|
|
|
|
153 |
with gr.Column():
|
154 |
+
result = gr.Image(label="Output")
|
155 |
+
gr.Examples(
|
156 |
+
examples=examples,
|
157 |
+
inputs=[image, model_name],
|
158 |
+
outputs=result,
|
159 |
+
fn=fn,
|
160 |
+
cache_examples=os.getenv("CACHE_EXAMPLES") == "1",
|
161 |
+
)
|
162 |
+
run_button.click(fn=fn, inputs=[image, model_name], outputs=result, api_name="run")
|
|
|
163 |
demo.queue().launch()
|
requirements.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
-
numpy==1.
|
2 |
-
opencv-python-headless==4.
|
3 |
-
Pillow==10.
|
4 |
-
scipy==1.
|
5 |
torch==2.0.1
|
6 |
torchvision==0.15.2
|
|
|
1 |
+
numpy==1.26.4
|
2 |
+
opencv-python-headless==4.9.0.80
|
3 |
+
Pillow==10.2.0
|
4 |
+
scipy==1.12.0
|
5 |
torch==2.0.1
|
6 |
torchvision==0.15.2
|
style.css
CHANGED
@@ -1,7 +1,11 @@
|
|
1 |
h1 {
|
2 |
text-align: center;
|
|
|
3 |
}
|
4 |
|
5 |
#duplicate-button {
|
6 |
margin: auto;
|
|
|
|
|
|
|
7 |
}
|
|
|
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 |
}
|