Spaces:
Running
Running
Upload 4 files
Browse files- .gitattributes +1 -0
- Protector_Cromwell_style.png +3 -0
- app.py +17 -0
- control_requirements.txt +20 -0
- pred_color.py +128 -0
.gitattributes
CHANGED
@@ -37,3 +37,4 @@ SPIGA/spiga/data/annotations/merlrav/train.json filter=lfs diff=lfs merge=lfs -t
|
|
37 |
SPIGA/spiga/data/annotations/wflw/train.json filter=lfs diff=lfs merge=lfs -text
|
38 |
SPIGA/spiga/eval/results/merlrav/results_merlrav_test.json filter=lfs diff=lfs merge=lfs -text
|
39 |
SPIGA/spiga/eval/results/wflw/results_wflw_test.json filter=lfs diff=lfs merge=lfs -text
|
|
|
|
37 |
SPIGA/spiga/data/annotations/wflw/train.json filter=lfs diff=lfs merge=lfs -text
|
38 |
SPIGA/spiga/eval/results/merlrav/results_merlrav_test.json filter=lfs diff=lfs merge=lfs -text
|
39 |
SPIGA/spiga/eval/results/wflw/results_wflw_test.json filter=lfs diff=lfs merge=lfs -text
|
40 |
+
Protector_Cromwell_style.png filter=lfs diff=lfs merge=lfs -text
|
Protector_Cromwell_style.png
ADDED
Git LFS Details
|
app.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from pred_color import *
|
3 |
+
|
4 |
+
example_sample = [
|
5 |
+
"babyxiang_ai.png"
|
6 |
+
]
|
7 |
+
|
8 |
+
def pred_func(img):
|
9 |
+
out = single_pred_features(img)
|
10 |
+
if type(out) == type({}):
|
11 |
+
return out["spiga_seg"]
|
12 |
+
|
13 |
+
gr=gr.Interface(fn=pred_func, inputs=['image',],
|
14 |
+
outputs=[gr.Image(label='output').style(height=512)],
|
15 |
+
examples=example_sample if example_sample else None,
|
16 |
+
)
|
17 |
+
gr.launch(share=False)
|
control_requirements.txt
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate
|
2 |
+
torchvision
|
3 |
+
transformers>=4.25.1
|
4 |
+
ftfy
|
5 |
+
tensorboard
|
6 |
+
datasets
|
7 |
+
torch
|
8 |
+
git+https://github.com/huggingface/diffusers
|
9 |
+
matplotlib>=3.2.1
|
10 |
+
numpy>=1.18.2
|
11 |
+
opencv-python-headless
|
12 |
+
Pillow>=7.0.0
|
13 |
+
torch>=1.4.0
|
14 |
+
torchvision>=0.5.0
|
15 |
+
torchaudio
|
16 |
+
scipy
|
17 |
+
scikit-learn
|
18 |
+
datasets
|
19 |
+
retinaface-py>=0.0.2
|
20 |
+
gradio
|
pred_color.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
###
|
2 |
+
'''
|
3 |
+
!git clone https://huggingface.co/spaces/radames/SPIGA-face-alignment-headpose-estimator
|
4 |
+
!cp -r SPIGA-face-alignment-headpose-estimator/SPIGA .
|
5 |
+
!pip install -r SPIGA/requirements.txt
|
6 |
+
!pip install datasets
|
7 |
+
!pip install retinaface-py>=0.0.2
|
8 |
+
!huggingface-cli login
|
9 |
+
'''
|
10 |
+
|
11 |
+
import sys
|
12 |
+
sys.path.insert(0, "SPIGA")
|
13 |
+
|
14 |
+
import numpy as np
|
15 |
+
|
16 |
+
from datasets import load_dataset
|
17 |
+
from spiga.inference.config import ModelConfig
|
18 |
+
from spiga.inference.framework import SPIGAFramework
|
19 |
+
|
20 |
+
processor = SPIGAFramework(ModelConfig("300wpublic"))
|
21 |
+
|
22 |
+
import matplotlib.pyplot as plt
|
23 |
+
import matplotlib.patches as patches
|
24 |
+
from matplotlib.path import Path
|
25 |
+
import PIL
|
26 |
+
|
27 |
+
def get_patch(landmarks, color='lime', closed=False):
|
28 |
+
contour = landmarks
|
29 |
+
ops = [Path.MOVETO] + [Path.LINETO]*(len(contour)-1)
|
30 |
+
facecolor = (0, 0, 0, 0) # Transparent fill color, if open
|
31 |
+
if closed:
|
32 |
+
contour.append(contour[0])
|
33 |
+
ops.append(Path.CLOSEPOLY)
|
34 |
+
facecolor = color
|
35 |
+
path = Path(contour, ops)
|
36 |
+
return patches.PathPatch(path, facecolor=facecolor, edgecolor=color, lw=4)
|
37 |
+
|
38 |
+
# Draw to a buffer.
|
39 |
+
|
40 |
+
def conditioning_from_landmarks(landmarks, size=512):
|
41 |
+
# Precisely control output image size
|
42 |
+
dpi = 72
|
43 |
+
fig, ax = plt.subplots(1, figsize=[size/dpi, size/dpi], tight_layout={'pad':0})
|
44 |
+
fig.set_dpi(dpi)
|
45 |
+
|
46 |
+
black = np.zeros((size, size, 3))
|
47 |
+
ax.imshow(black)
|
48 |
+
|
49 |
+
face_patch = get_patch(landmarks[0:17])
|
50 |
+
l_eyebrow = get_patch(landmarks[17:22], color='yellow')
|
51 |
+
r_eyebrow = get_patch(landmarks[22:27], color='yellow')
|
52 |
+
nose_v = get_patch(landmarks[27:31], color='orange')
|
53 |
+
nose_h = get_patch(landmarks[31:36], color='orange')
|
54 |
+
l_eye = get_patch(landmarks[36:42], color='magenta', closed=True)
|
55 |
+
r_eye = get_patch(landmarks[42:48], color='magenta', closed=True)
|
56 |
+
outer_lips = get_patch(landmarks[48:60], color='cyan', closed=True)
|
57 |
+
inner_lips = get_patch(landmarks[60:68], color='blue', closed=True)
|
58 |
+
|
59 |
+
ax.add_patch(face_patch)
|
60 |
+
ax.add_patch(l_eyebrow)
|
61 |
+
ax.add_patch(r_eyebrow)
|
62 |
+
ax.add_patch(nose_v)
|
63 |
+
ax.add_patch(nose_h)
|
64 |
+
ax.add_patch(l_eye)
|
65 |
+
ax.add_patch(r_eye)
|
66 |
+
ax.add_patch(outer_lips)
|
67 |
+
ax.add_patch(inner_lips)
|
68 |
+
|
69 |
+
plt.axis('off')
|
70 |
+
|
71 |
+
fig.canvas.draw()
|
72 |
+
buffer, (width, height) = fig.canvas.print_to_buffer()
|
73 |
+
assert width == height
|
74 |
+
assert width == size
|
75 |
+
|
76 |
+
buffer = np.frombuffer(buffer, np.uint8).reshape((height, width, 4))
|
77 |
+
buffer = buffer[:, :, 0:3]
|
78 |
+
plt.close(fig)
|
79 |
+
return PIL.Image.fromarray(buffer)
|
80 |
+
|
81 |
+
import retinaface
|
82 |
+
import spiga.demo.analyze.track.retinasort.config as cfg
|
83 |
+
|
84 |
+
config = cfg.cfg_retinasort
|
85 |
+
device = "cpu"
|
86 |
+
face_detector = retinaface.RetinaFaceDetector(model=config['retina']['model_name'],
|
87 |
+
device=device,
|
88 |
+
extra_features=config['retina']['extra_features'],
|
89 |
+
cfg_postreat=config['retina']['postreat'])
|
90 |
+
|
91 |
+
import cv2
|
92 |
+
Image = PIL.Image
|
93 |
+
import os
|
94 |
+
def single_pred_features(image):
|
95 |
+
if type(image) == type("") and os.path.exists(image):
|
96 |
+
image = Image.open(image).convert("RGB")
|
97 |
+
elif hasattr(image, "shape"):
|
98 |
+
image = Image.fromarray(image).convert("RGB")
|
99 |
+
else:
|
100 |
+
image = image.convert("RGB")
|
101 |
+
image = image.resize((512, 512))
|
102 |
+
cv2_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
103 |
+
face_detector.set_input_shape(image.size[1], image.size[0])
|
104 |
+
features = face_detector.inference(image)
|
105 |
+
if features:
|
106 |
+
bboxes = features['bbox']
|
107 |
+
bboxes_n = []
|
108 |
+
for bbox in bboxes:
|
109 |
+
x1, y1, x2, y2 = bbox[:4]
|
110 |
+
bbox_wh = [x1, y1, x2-x1, y2-y1]
|
111 |
+
bboxes_n.append(bbox_wh)
|
112 |
+
face_features = processor.inference(cv2_image, bboxes_n)
|
113 |
+
landmarks = face_features["landmarks"][0]
|
114 |
+
face_features["spiga"] = landmarks
|
115 |
+
face_features['spiga_seg'] = conditioning_from_landmarks(landmarks)
|
116 |
+
return face_features
|
117 |
+
|
118 |
+
if __name__ == "__main__":
|
119 |
+
from datasets import load_dataset, Dataset
|
120 |
+
ds = load_dataset("svjack/facesyntheticsspigacaptioned_en_zh_1")
|
121 |
+
dss = ds["train"]
|
122 |
+
|
123 |
+
xiangbaobao = PIL.Image.open("babyxiang.png")
|
124 |
+
out = single_pred_features(xiangbaobao.resize((512, 512)))
|
125 |
+
out["spiga_seg"]
|
126 |
+
|
127 |
+
out = single_pred_features(dss[0]["image"])
|
128 |
+
out["spiga_seg"]
|