Spaces:
Sleeping
Sleeping
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 +41 -100
- images/95UF6LXe-Lo.jpg +3 -0
- images/ILip77SbmOE.jpg +3 -0
- images/README.md +7 -0
- images/et_78QkMMQs.jpg +3 -0
- images/pexels-ksenia-chernaya-8535230.jpg +3 -0
- images/rDEOVtE7vOs.jpg +3 -0
- requirements.txt +4 -4
- 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: green
|
| 5 |
colorTo: gray
|
| 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: green
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.19.2
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
short_description: face parsing
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
|
app.py
CHANGED
|
@@ -2,85 +2,42 @@
|
|
| 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 gradio as gr
|
| 13 |
-
import huggingface_hub
|
| 14 |
import numpy as np
|
| 15 |
import torch
|
| 16 |
|
| 17 |
-
sys.path.insert(0,
|
| 18 |
-
sys.path.insert(0,
|
| 19 |
-
sys.path.insert(0,
|
| 20 |
|
| 21 |
from ibug.face_detection import RetinaFacePredictor
|
| 22 |
from ibug.face_parsing.parser import WEIGHT, FaceParser
|
| 23 |
from ibug.face_parsing.utils import label_colormap
|
| 24 |
|
| 25 |
-
|
| 26 |
-
DESCRIPTION = 'This is an unofficial demo for https://github.com/hhj1897/face_parsing.'
|
| 27 |
-
ARTICLE = '<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.ibug-face_parsing" alt="visitor badge"/></center>'
|
| 28 |
-
|
| 29 |
-
TOKEN = os.environ['TOKEN']
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
def parse_args() -> argparse.Namespace:
|
| 33 |
-
parser = argparse.ArgumentParser()
|
| 34 |
-
parser.add_argument('--device', type=str, default='cpu')
|
| 35 |
-
parser.add_argument('--theme', type=str)
|
| 36 |
-
parser.add_argument('--live', action='store_true')
|
| 37 |
-
parser.add_argument('--share', action='store_true')
|
| 38 |
-
parser.add_argument('--port', type=int)
|
| 39 |
-
parser.add_argument('--disable-queue',
|
| 40 |
-
dest='enable_queue',
|
| 41 |
-
action='store_false')
|
| 42 |
-
parser.add_argument('--allow-flagging', type=str, default='never')
|
| 43 |
-
return parser.parse_args()
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
def load_sample_images() -> list[pathlib.Path]:
|
| 47 |
-
image_dir = pathlib.Path('images')
|
| 48 |
-
if not image_dir.exists():
|
| 49 |
-
image_dir.mkdir()
|
| 50 |
-
dataset_repo = 'hysts/input-images'
|
| 51 |
-
filenames = ['000.tar', '001.tar']
|
| 52 |
-
for name in filenames:
|
| 53 |
-
path = huggingface_hub.hf_hub_download(dataset_repo,
|
| 54 |
-
name,
|
| 55 |
-
repo_type='dataset',
|
| 56 |
-
use_auth_token=TOKEN)
|
| 57 |
-
with tarfile.open(path) as f:
|
| 58 |
-
f.extractall(image_dir.as_posix())
|
| 59 |
-
return sorted(image_dir.rglob('*.jpg'))
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
def load_detector(device: torch.device) -> RetinaFacePredictor:
|
| 63 |
-
model = RetinaFacePredictor(
|
| 64 |
-
threshold=0.8,
|
| 65 |
-
device=device,
|
| 66 |
-
model=RetinaFacePredictor.get_model('mobilenet0.25'))
|
| 67 |
-
return model
|
| 68 |
|
| 69 |
|
| 70 |
def load_model(model_name: str, device: torch.device) -> FaceParser:
|
| 71 |
-
encoder, decoder, num_classes = model_name.split(
|
| 72 |
-
num_classes = int(num_classes)
|
| 73 |
-
model = FaceParser(device=device,
|
| 74 |
-
encoder=encoder,
|
| 75 |
-
decoder=decoder,
|
| 76 |
-
num_classes=num_classes)
|
| 77 |
model.num_classes = num_classes
|
| 78 |
return model
|
| 79 |
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
model = models[model_name]
|
| 85 |
colormap = label_colormap(model.num_classes)
|
| 86 |
|
|
@@ -89,7 +46,7 @@ def predict(image: np.ndarray, model_name: str, max_num_faces: int,
|
|
| 89 |
|
| 90 |
faces = detector(image, rgb=False)
|
| 91 |
if len(faces) == 0:
|
| 92 |
-
raise RuntimeError(
|
| 93 |
faces = sorted(list(faces), key=lambda x: -x[4])[:max_num_faces][::-1]
|
| 94 |
masks = model.predict_img(image, faces, rgb=False)
|
| 95 |
|
|
@@ -103,46 +60,30 @@ def predict(image: np.ndarray, model_name: str, max_num_faces: int,
|
|
| 103 |
return res[:, :, ::-1]
|
| 104 |
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
func,
|
| 123 |
-
[
|
| 124 |
-
gr.inputs.Image(type='numpy', label='Input'),
|
| 125 |
-
gr.inputs.Radio(model_names,
|
| 126 |
-
type='value',
|
| 127 |
-
default=model_names[1],
|
| 128 |
-
label='Model'),
|
| 129 |
-
gr.inputs.Slider(
|
| 130 |
-
1, 20, step=1, default=10, label='Max Number of Faces'),
|
| 131 |
-
],
|
| 132 |
-
gr.outputs.Image(type='numpy', label='Output'),
|
| 133 |
-
examples=examples,
|
| 134 |
-
title=TITLE,
|
| 135 |
-
description=DESCRIPTION,
|
| 136 |
-
article=ARTICLE,
|
| 137 |
-
theme=args.theme,
|
| 138 |
-
allow_flagging=args.allow_flagging,
|
| 139 |
-
live=args.live,
|
| 140 |
-
).launch(
|
| 141 |
-
enable_queue=args.enable_queue,
|
| 142 |
-
server_port=args.port,
|
| 143 |
-
share=args.share,
|
| 144 |
)
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
if __name__ ==
|
| 148 |
-
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
|
|
|
| 5 |
import os
|
| 6 |
import pathlib
|
| 7 |
import sys
|
|
|
|
| 8 |
|
| 9 |
import gradio as gr
|
|
|
|
| 10 |
import numpy as np
|
| 11 |
import torch
|
| 12 |
|
| 13 |
+
sys.path.insert(0, "face_detection")
|
| 14 |
+
sys.path.insert(0, "face_parsing")
|
| 15 |
+
sys.path.insert(0, "roi_tanh_warping")
|
| 16 |
|
| 17 |
from ibug.face_detection import RetinaFacePredictor
|
| 18 |
from ibug.face_parsing.parser import WEIGHT, FaceParser
|
| 19 |
from ibug.face_parsing.utils import label_colormap
|
| 20 |
|
| 21 |
+
DESCRIPTION = "# [hhj1897/face_parsing](https://github.com/hhj1897/face_parsing)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
def load_model(model_name: str, device: torch.device) -> FaceParser:
|
| 25 |
+
encoder, decoder, num_classes = model_name.split("-")
|
| 26 |
+
num_classes = int(num_classes) # type: ignore
|
| 27 |
+
model = FaceParser(device=device, encoder=encoder, decoder=decoder, num_classes=num_classes)
|
|
|
|
|
|
|
|
|
|
| 28 |
model.num_classes = num_classes
|
| 29 |
return model
|
| 30 |
|
| 31 |
|
| 32 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 33 |
+
|
| 34 |
+
detector = RetinaFacePredictor(threshold=0.8, device=device, model=RetinaFacePredictor.get_model("mobilenet0.25"))
|
| 35 |
+
|
| 36 |
+
model_names = list(WEIGHT.keys())
|
| 37 |
+
models = {name: load_model(name, device=device) for name in model_names}
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def predict(image: np.ndarray, model_name: str, max_num_faces: int) -> np.ndarray:
|
| 41 |
model = models[model_name]
|
| 42 |
colormap = label_colormap(model.num_classes)
|
| 43 |
|
|
|
|
| 46 |
|
| 47 |
faces = detector(image, rgb=False)
|
| 48 |
if len(faces) == 0:
|
| 49 |
+
raise RuntimeError("No face was found.")
|
| 50 |
faces = sorted(list(faces), key=lambda x: -x[4])[:max_num_faces][::-1]
|
| 51 |
masks = model.predict_img(image, faces, rgb=False)
|
| 52 |
|
|
|
|
| 60 |
return res[:, :, ::-1]
|
| 61 |
|
| 62 |
|
| 63 |
+
with gr.Blocks(css="style.css") as demo:
|
| 64 |
+
gr.Markdown(DESCRIPTION)
|
| 65 |
+
with gr.Row():
|
| 66 |
+
with gr.Column():
|
| 67 |
+
image = gr.Image(type="numpy", label="Input")
|
| 68 |
+
model_name = gr.Radio(choices=model_names, type="value", value=model_names[1], label="Model")
|
| 69 |
+
max_num_faces = gr.Slider(minimum=1, maximum=20, step=1, value=10, label="Max Number of Faces")
|
| 70 |
+
run_button = gr.Button()
|
| 71 |
+
with gr.Column():
|
| 72 |
+
result = gr.Image(label="Output")
|
| 73 |
+
gr.Examples(
|
| 74 |
+
examples=[[path.as_posix(), model_names[1], 10] for path in pathlib.Path("images").rglob("*.jpg")],
|
| 75 |
+
inputs=[image, model_name, max_num_faces],
|
| 76 |
+
outputs=result,
|
| 77 |
+
fn=predict,
|
| 78 |
+
cache_examples=os.getenv("CACHE_EXAMPLES") == "1",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
)
|
| 80 |
|
| 81 |
+
run_button.click(
|
| 82 |
+
fn=predict,
|
| 83 |
+
inputs=[image, model_name, max_num_faces],
|
| 84 |
+
outputs=result,
|
| 85 |
+
api_name="predict",
|
| 86 |
+
)
|
| 87 |
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
demo.queue(max_size=20).launch()
|
images/95UF6LXe-Lo.jpg
ADDED
|
Git LFS Details
|
images/ILip77SbmOE.jpg
ADDED
|
Git LFS Details
|
images/README.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
These images are freely-usable ones from [Unsplash](https://unsplash.com/) and public domain:
|
| 2 |
+
|
| 3 |
+
- https://unsplash.com/photos/rDEOVtE7vOs
|
| 4 |
+
- https://unsplash.com/photos/et_78QkMMQs
|
| 5 |
+
- https://unsplash.com/photos/ILip77SbmOE
|
| 6 |
+
- https://unsplash.com/photos/95UF6LXe-Lo
|
| 7 |
+
- https://www.pexels.com/photo/children-with-her-students-holding-different-color-bells-8535230/
|
images/et_78QkMMQs.jpg
ADDED
|
Git LFS Details
|
images/pexels-ksenia-chernaya-8535230.jpg
ADDED
|
Git LFS Details
|
images/rDEOVtE7vOs.jpg
ADDED
|
Git LFS Details
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
numpy==1.
|
| 2 |
-
opencv-python-headless==4.
|
| 3 |
-
torch==
|
| 4 |
-
torchvision==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 |
+
}
|