hysts HF Staff commited on
Commit
6d6900d
·
1 Parent(s): 01b3ef9
.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,7 +4,7 @@ emoji: 🔥
4
  colorFrom: pink
5
  colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 3.0.5
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: pink
5
  colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 4.19.2
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py CHANGED
@@ -2,79 +2,34 @@
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
14
- import huggingface_hub
15
  import numpy as np
16
  import torch
17
 
18
- sys.path.insert(0, 'face_detection')
19
- sys.path.insert(0, 'face_alignment')
20
 
21
  from ibug.face_alignment import FANPredictor
22
  from ibug.face_detection import RetinaFacePredictor
23
 
24
- TITLE = 'ibug-group/face_alignment'
25
- DESCRIPTION = 'This is an unofficial demo for https://github.com/ibug-group/face_alignment.'
26
- ARTICLE = '<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.ibug-face_alignment" alt="visitor badge"/></center>'
27
-
28
- TOKEN = os.environ['TOKEN']
29
-
30
-
31
- def parse_args() -> argparse.Namespace:
32
- parser = argparse.ArgumentParser()
33
- parser.add_argument('--device', type=str, default='cpu')
34
- parser.add_argument('--theme', type=str)
35
- parser.add_argument('--live', action='store_true')
36
- parser.add_argument('--share', action='store_true')
37
- parser.add_argument('--port', type=int)
38
- parser.add_argument('--disable-queue',
39
- dest='enable_queue',
40
- action='store_false')
41
- parser.add_argument('--allow-flagging', type=str, default='never')
42
- return parser.parse_args()
43
-
44
-
45
- def load_sample_images() -> list[pathlib.Path]:
46
- image_dir = pathlib.Path('images')
47
- if not image_dir.exists():
48
- image_dir.mkdir()
49
- dataset_repo = 'hysts/input-images'
50
- filenames = ['001.tar']
51
- for name in filenames:
52
- path = huggingface_hub.hf_hub_download(dataset_repo,
53
- name,
54
- repo_type='dataset',
55
- use_auth_token=TOKEN)
56
- with tarfile.open(path) as f:
57
- f.extractall(image_dir.as_posix())
58
- return sorted(image_dir.rglob('*.jpg'))
59
-
60
-
61
- def load_detector(device: torch.device) -> RetinaFacePredictor:
62
- model = RetinaFacePredictor(
63
- threshold=0.8,
64
- device=device,
65
- model=RetinaFacePredictor.get_model('mobilenet0.25'))
66
- return model
67
-
68
-
69
- def load_model(model_name: str, device: torch.device) -> FANPredictor:
70
- model = FANPredictor(device=device,
71
- model=FANPredictor.get_model(model_name))
72
- return model
73
-
74
-
75
- def predict(image: np.ndarray, model_name: str, max_num_faces: int,
76
- landmark_score_threshold: int, detector: RetinaFacePredictor,
77
- models: dict[str, FANPredictor]) -> np.ndarray:
78
  model = models[model_name]
79
 
80
  # RGB -> BGR
@@ -82,7 +37,7 @@ def predict(image: np.ndarray, model_name: str, max_num_faces: int,
82
 
83
  faces = detector(image, rgb=False)
84
  if len(faces) == 0:
85
- raise RuntimeError('No face was found.')
86
  faces = sorted(list(faces), key=lambda x: -x[4])[:max_num_faces]
87
  faces = np.asarray(faces)
88
  landmarks, landmark_scores = model(image, faces, rgb=False)
@@ -99,54 +54,33 @@ def predict(image: np.ndarray, model_name: str, max_num_faces: int,
99
  return res[:, :, ::-1]
100
 
101
 
102
- def main():
103
- args = parse_args()
104
- device = torch.device(args.device)
105
-
106
- detector = load_detector(device)
107
-
108
- model_names = [
109
- '2dfan2',
110
- '2dfan4',
111
- '2dfan2_alt',
112
- ]
113
- models = {name: load_model(name, device=device) for name in model_names}
114
-
115
- func = functools.partial(predict, detector=detector, models=models)
116
- func = functools.update_wrapper(func, predict)
117
-
118
- image_paths = load_sample_images()
119
- examples = [[path.as_posix(), model_names[0], 10, 0.2]
120
- for path in image_paths]
121
-
122
- gr.Interface(
123
- func,
124
- [
125
- gr.inputs.Image(type='numpy', label='Input'),
126
- gr.inputs.Radio(model_names,
127
- type='value',
128
- default=model_names[0],
129
- label='Model'),
130
- gr.inputs.Slider(
131
- 1, 20, step=1, default=10, label='Max Number of Faces'),
132
- gr.inputs.Slider(
133
- 0, 1, step=0.05, default=0.2,
134
- label='Landmark Score Threshold'),
135
- ],
136
- gr.outputs.Image(type='numpy', label='Output'),
137
  examples=examples,
138
- title=TITLE,
139
- description=DESCRIPTION,
140
- article=ARTICLE,
141
- theme=args.theme,
142
- allow_flagging=args.allow_flagging,
143
- live=args.live,
144
- ).launch(
145
- enable_queue=args.enable_queue,
146
- server_port=args.port,
147
- share=args.share,
148
  )
149
 
150
 
151
- if __name__ == '__main__':
152
- 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
 
11
  import numpy as np
12
  import torch
13
 
14
+ sys.path.insert(0, "face_detection")
15
+ sys.path.insert(0, "face_alignment")
16
 
17
  from ibug.face_alignment import FANPredictor
18
  from ibug.face_detection import RetinaFacePredictor
19
 
20
+ DESCRIPTION = "# [ibug-group/face_alignment](https://github.com/ibug-group/face_alignment)"
21
+
22
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
23
+ detector = RetinaFacePredictor(threshold=0.8, device=device, model=RetinaFacePredictor.get_model("mobilenet0.25"))
24
+ model_names = [
25
+ "2dfan2",
26
+ "2dfan4",
27
+ "2dfan2_alt",
28
+ ]
29
+ models = {name: FANPredictor(device=device, model=FANPredictor.get_model(name)) for name in model_names}
30
+
31
+
32
+ def predict(image: np.ndarray, model_name: str, max_num_faces: int, landmark_score_threshold: int) -> np.ndarray:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  model = models[model_name]
34
 
35
  # RGB -> BGR
 
37
 
38
  faces = detector(image, rgb=False)
39
  if len(faces) == 0:
40
+ raise RuntimeError("No face was found.")
41
  faces = sorted(list(faces), key=lambda x: -x[4])[:max_num_faces]
42
  faces = np.asarray(faces)
43
  landmarks, landmark_scores = model(image, faces, rgb=False)
 
54
  return res[:, :, ::-1]
55
 
56
 
57
+ examples = [[path.as_posix(), model_names[0], 10, 0.2] for path in pathlib.Path("images").rglob("*.jpg")]
58
+
59
+ with gr.Blocks(css="style.css") as demo:
60
+ gr.Markdown(DESCRIPTION)
61
+ with gr.Row():
62
+ with gr.Column():
63
+ image = gr.Image(type="numpy", label="Input")
64
+ model_name = gr.Radio(model_names, type="value", value=model_names[0], label="Model")
65
+ max_num_faces = gr.Slider(1, 20, step=1, value=10, label="Max Number of Faces")
66
+ landmark_score_thrshold = gr.Slider(0, 1, step=0.05, value=0.2, label="Landmark Score Threshold")
67
+ run_button = gr.Button()
68
+ with gr.Column():
69
+ result = gr.Image(label="Output")
70
+ gr.Examples(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  examples=examples,
72
+ inputs=[image, model_name, max_num_faces, landmark_score_thrshold],
73
+ outputs=result,
74
+ fn=predict,
75
+ cache_examples=os.getenv("CACHE_EXAMPLES") == "1",
76
+ )
77
+ run_button.click(
78
+ fn=predict,
79
+ inputs=[image, model_name, max_num_faces, landmark_score_thrshold],
80
+ outputs=result,
81
+ api_name="predict",
82
  )
83
 
84
 
85
+ if __name__ == "__main__":
86
+ demo.queue(max_size=20).launch()
images/README.md ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ These images are from the following public domain:
2
+
3
+ - https://www.pexels.com/photo/children-with-her-students-holding-different-color-bells-8535230/
images/pexels-ksenia-chernaya-8535230.jpg ADDED

Git LFS Details

  • SHA256: 79b6b7ee58972731ed7d83e45a13c18544690df9b384f36327b627714c2f5cab
  • Pointer size: 131 Bytes
  • Size of remote file: 409 kB
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- numpy==1.22.3
2
- opencv-python-headless==4.5.5.64
3
- torch==1.11.0
4
- torchvision==0.12.0
 
1
+ numpy==1.26.4
2
+ opencv-python-headless==4.9.0.80
3
+ torch==2.0.1
4
+ 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
+ }