hysts HF staff commited on
Commit
ef451ee
1 Parent(s): fafee53
Files changed (7) hide show
  1. .pre-commit-config.yaml +59 -34
  2. .style.yapf +0 -5
  3. .vscode/settings.json +30 -0
  4. README.md +2 -1
  5. app.py +44 -51
  6. requirements.txt +4 -4
  7. 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
- 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']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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: 3.36.1
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, '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',
33
- f'models/{model_name}.pkl')
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
- 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
  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('zyx', pose, degrees=True)
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(image: np.ndarray, model_name: str, face_detector: RetinaFacePredictor,
103
- models: dict[str, nn.Module], transform: Callable,
104
- device: torch.device) -> np.ndarray:
 
 
 
 
 
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('cuda:0' if torch.cuda.is_available() else 'cpu')
128
- face_detector = RetinaFacePredictor(
129
- threshold=0.8,
130
- device=device,
131
- model=RetinaFacePredictor.get_model('mobilenet0.25'))
132
 
133
  model_names = [
134
- 'hopenet_alpha1',
135
- 'hopenet_alpha2',
136
- 'hopenet_robust_alpha1',
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 = [['images/pexels-ksenia-chernaya-8535230.jpg', 'hopenet_alpha1']]
148
 
149
- with gr.Blocks(css='style.css') as demo:
150
  gr.Markdown(DESCRIPTION)
151
  with gr.Row():
152
  with gr.Column():
153
- image = gr.Image(label='Input', type='numpy')
154
- model_name = gr.Radio(label='Model',
155
- choices=model_names,
156
- type='value',
157
- value=model_names[0])
158
- run_button = gr.Button('Run')
159
  with gr.Column():
160
- result = gr.Image(label='Output')
161
- gr.Examples(examples=examples,
162
- inputs=[image, model_name],
163
- outputs=result,
164
- fn=fn,
165
- cache_examples=os.getenv('CACHE_EXAMPLES') == '1')
166
- run_button.click(fn=fn,
167
- inputs=[image, model_name],
168
- outputs=result,
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.23.5
2
- opencv-python-headless==4.8.0.74
3
- Pillow==10.0.0
4
- scipy==1.11.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
  }