Jessicaly commited on
Commit
1d0c1f1
·
verified ·
1 Parent(s): 95a4528

Upload 7 files

Browse files
Files changed (7) hide show
  1. app.py +140 -0
  2. gitattributes +27 -0
  3. gitmodules (1) +3 -0
  4. pre-commit-config (1).yaml +35 -0
  5. requirements.txt +3 -0
  6. style (1).yapf +5 -0
  7. style.css +3 -0
app.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ from __future__ import annotations
4
+
5
+ import functools
6
+ import os
7
+ import pathlib
8
+ import sys
9
+ import tarfile
10
+ import urllib.request
11
+ from typing import Callable
12
+
13
+ import cv2
14
+ import gradio as gr
15
+ import huggingface_hub
16
+ import numpy as np
17
+ import PIL.Image
18
+ import torch
19
+ import torchvision.transforms as T
20
+
21
+ sys.path.insert(0, 'anime_face_landmark_detection')
22
+
23
+ from CFA import CFA
24
+
25
+ DESCRIPTION = '# [kanosawa/anime_face_landmark_detection](https://github.com/kanosawa/anime_face_landmark_detection)'
26
+
27
+ NUM_LANDMARK = 24
28
+ CROP_SIZE = 128
29
+
30
+
31
+ def load_sample_image_paths() -> list[pathlib.Path]:
32
+ image_dir = pathlib.Path('images')
33
+ if not image_dir.exists():
34
+ dataset_repo = 'hysts/sample-images-TADNE'
35
+ path = huggingface_hub.hf_hub_download(dataset_repo,
36
+ 'images.tar.gz',
37
+ repo_type='dataset')
38
+ with tarfile.open(path) as f:
39
+ f.extractall()
40
+ return sorted(image_dir.glob('*'))
41
+
42
+
43
+ def load_face_detector() -> cv2.CascadeClassifier:
44
+ url = 'https://raw.githubusercontent.com/nagadomi/lbpcascade_animeface/master/lbpcascade_animeface.xml'
45
+ path = pathlib.Path('lbpcascade_animeface.xml')
46
+ if not path.exists():
47
+ urllib.request.urlretrieve(url, path.as_posix())
48
+ return cv2.CascadeClassifier(path.as_posix())
49
+
50
+
51
+ def load_landmark_detector(device: torch.device) -> torch.nn.Module:
52
+ path = huggingface_hub.hf_hub_download(
53
+ 'public-data/anime_face_landmark_detection',
54
+ 'checkpoint_landmark_191116.pth')
55
+ model = CFA(output_channel_num=NUM_LANDMARK + 1, checkpoint_name=path)
56
+ model.to(device)
57
+ model.eval()
58
+ return model
59
+
60
+
61
+ @torch.inference_mode()
62
+ def detect(image_path: str, face_detector: cv2.CascadeClassifier,
63
+ device: torch.device, transform: Callable,
64
+ landmark_detector: torch.nn.Module) -> np.ndarray:
65
+ image = cv2.imread(image_path)
66
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
67
+ preds = face_detector.detectMultiScale(gray,
68
+ scaleFactor=1.1,
69
+ minNeighbors=5,
70
+ minSize=(24, 24))
71
+
72
+ image_h, image_w = image.shape[:2]
73
+ pil_image = PIL.Image.fromarray(image[:, :, ::-1].copy())
74
+
75
+ res = image.copy()
76
+ for x_orig, y_orig, w_orig, h_orig in preds:
77
+
78
+ x0 = round(max(x_orig - w_orig / 8, 0))
79
+ x1 = round(min(x_orig + w_orig * 9 / 8, image_w))
80
+ y0 = round(max(y_orig - h_orig / 4, 0))
81
+ y1 = y_orig + h_orig
82
+ w = x1 - x0
83
+ h = y1 - y0
84
+
85
+ temp = pil_image.crop((x0, y0, x1, y1))
86
+ temp = temp.resize((CROP_SIZE, CROP_SIZE), PIL.Image.BICUBIC)
87
+ data = transform(temp)
88
+ data = data.to(device).unsqueeze(0)
89
+
90
+ heatmaps = landmark_detector(data)
91
+ heatmaps = heatmaps[-1].cpu().numpy()[0]
92
+
93
+ cv2.rectangle(res, (x0, y0), (x1, y1), (0, 255, 0), 2)
94
+
95
+ for i in range(NUM_LANDMARK):
96
+ heatmap = cv2.resize(heatmaps[i], (CROP_SIZE, CROP_SIZE),
97
+ interpolation=cv2.INTER_CUBIC)
98
+ pty, ptx = np.unravel_index(np.argmax(heatmap), heatmap.shape)
99
+ pt_crop = np.round(np.array([ptx * w, pty * h]) /
100
+ CROP_SIZE).astype(int)
101
+ pt = np.array([x0, y0]) + pt_crop
102
+ cv2.circle(res, tuple(pt), 2, (0, 0, 255), cv2.FILLED)
103
+
104
+ return res[:, :, ::-1]
105
+
106
+
107
+ device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
108
+
109
+ image_paths = load_sample_image_paths()
110
+ examples = [[path.as_posix()] for path in image_paths]
111
+
112
+ face_detector = load_face_detector()
113
+ landmark_detector = load_landmark_detector(device)
114
+ transform = T.Compose([
115
+ T.ToTensor(),
116
+ T.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
117
+ ])
118
+
119
+ fn = functools.partial(detect,
120
+ face_detector=face_detector,
121
+ device=device,
122
+ transform=transform,
123
+ landmark_detector=landmark_detector)
124
+
125
+ with gr.Blocks(css='style.css') as demo:
126
+ gr.Markdown(DESCRIPTION)
127
+ with gr.Row():
128
+ with gr.Column():
129
+ image = gr.Image(label='Input', type='filepath')
130
+ run_button = gr.Button('Run')
131
+ with gr.Column():
132
+ result = gr.Image(label='Result')
133
+
134
+ gr.Examples(examples=examples,
135
+ inputs=image,
136
+ outputs=result,
137
+ fn=fn,
138
+ cache_examples=os.getenv('CACHE_EXAMPLES') == '1')
139
+ run_button.click(fn=fn, inputs=image, outputs=result, api_name='predict')
140
+ demo.queue(max_size=15).launch()
gitattributes ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bin.* filter=lfs diff=lfs merge=lfs -text
5
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.model filter=lfs diff=lfs merge=lfs -text
12
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
13
+ *.onnx filter=lfs diff=lfs merge=lfs -text
14
+ *.ot filter=lfs diff=lfs merge=lfs -text
15
+ *.parquet filter=lfs diff=lfs merge=lfs -text
16
+ *.pb filter=lfs diff=lfs merge=lfs -text
17
+ *.pt filter=lfs diff=lfs merge=lfs -text
18
+ *.pth filter=lfs diff=lfs merge=lfs -text
19
+ *.rar filter=lfs diff=lfs merge=lfs -text
20
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
21
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
22
+ *.tflite filter=lfs diff=lfs merge=lfs -text
23
+ *.tgz filter=lfs diff=lfs merge=lfs -text
24
+ *.xz filter=lfs diff=lfs merge=lfs -text
25
+ *.zip filter=lfs diff=lfs merge=lfs -text
26
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
27
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
gitmodules (1) ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ [submodule "anime_face_landmark_detection"]
2
+ path = anime_face_landmark_detection
3
+ url = https://github.com/kanosawa/anime_face_landmark_detection
pre-commit-config (1).yaml ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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']
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ opencv-python-headless>=4.7.0.72
2
+ torch==2.0.1
3
+ torchvision==0.15.2
style (1).yapf ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
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
style.css ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ }