Spaces:
Running
on
T4
Running
on
T4
Ahsen Khaliq
commited on
Commit
•
e763147
1
Parent(s):
92b7005
Update app.py
Browse files
app.py
CHANGED
@@ -47,120 +47,17 @@ def face2paint(
|
|
47 |
|
48 |
|
49 |
import os
|
50 |
-
import dlib
|
51 |
import collections
|
52 |
from typing import Union, List
|
53 |
import numpy as np
|
54 |
from PIL import Image
|
55 |
|
56 |
|
57 |
-
def get_dlib_face_detector(predictor_path: str = "shape_predictor_68_face_landmarks.dat"):
|
58 |
-
|
59 |
-
if not os.path.isfile(predictor_path):
|
60 |
-
model_file = "shape_predictor_68_face_landmarks.dat.bz2"
|
61 |
-
os.system("wget http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2")
|
62 |
-
os.system("bzip2 -dk shape_predictor_68_face_landmarks.dat.bz2")
|
63 |
-
|
64 |
-
detector = dlib.get_frontal_face_detector()
|
65 |
-
shape_predictor = dlib.shape_predictor(predictor_path)
|
66 |
-
|
67 |
-
def detect_face_landmarks(img: Union[Image.Image, np.ndarray]):
|
68 |
-
if isinstance(img, Image.Image):
|
69 |
-
img = np.array(img)
|
70 |
-
faces = []
|
71 |
-
dets = detector(img)
|
72 |
-
for d in dets:
|
73 |
-
shape = shape_predictor(img, d)
|
74 |
-
faces.append(np.array([[v.x, v.y] for v in shape.parts()]))
|
75 |
-
return faces
|
76 |
-
|
77 |
-
return detect_face_landmarks
|
78 |
-
|
79 |
-
|
80 |
import PIL.Image
|
81 |
import PIL.ImageFile
|
82 |
import numpy as np
|
83 |
import scipy.ndimage
|
84 |
-
|
85 |
-
|
86 |
-
def align_and_crop_face(
|
87 |
-
img: Image.Image,
|
88 |
-
landmarks: np.ndarray,
|
89 |
-
expand: float = 1.0,
|
90 |
-
output_size: int = 1024,
|
91 |
-
transform_size: int = 4096,
|
92 |
-
enable_padding: bool = True,
|
93 |
-
):
|
94 |
-
# Parse landmarks.
|
95 |
-
# pylint: disable=unused-variable
|
96 |
-
lm = landmarks
|
97 |
-
lm_chin = lm[0 : 17] # left-right
|
98 |
-
lm_eyebrow_left = lm[17 : 22] # left-right
|
99 |
-
lm_eyebrow_right = lm[22 : 27] # left-right
|
100 |
-
lm_nose = lm[27 : 31] # top-down
|
101 |
-
lm_nostrils = lm[31 : 36] # top-down
|
102 |
-
lm_eye_left = lm[36 : 42] # left-clockwise
|
103 |
-
lm_eye_right = lm[42 : 48] # left-clockwise
|
104 |
-
lm_mouth_outer = lm[48 : 60] # left-clockwise
|
105 |
-
lm_mouth_inner = lm[60 : 68] # left-clockwise
|
106 |
-
|
107 |
-
# Calculate auxiliary vectors.
|
108 |
-
eye_left = np.mean(lm_eye_left, axis=0)
|
109 |
-
eye_right = np.mean(lm_eye_right, axis=0)
|
110 |
-
eye_avg = (eye_left + eye_right) * 0.5
|
111 |
-
eye_to_eye = eye_right - eye_left
|
112 |
-
mouth_left = lm_mouth_outer[0]
|
113 |
-
mouth_right = lm_mouth_outer[6]
|
114 |
-
mouth_avg = (mouth_left + mouth_right) * 0.5
|
115 |
-
eye_to_mouth = mouth_avg - eye_avg
|
116 |
-
|
117 |
-
# Choose oriented crop rectangle.
|
118 |
-
x = eye_to_eye - np.flipud(eye_to_mouth) * [-1, 1]
|
119 |
-
x /= np.hypot(*x)
|
120 |
-
x *= max(np.hypot(*eye_to_eye) * 2.0, np.hypot(*eye_to_mouth) * 1.8)
|
121 |
-
x *= expand
|
122 |
-
y = np.flipud(x) * [-1, 1]
|
123 |
-
c = eye_avg + eye_to_mouth * 0.1
|
124 |
-
quad = np.stack([c - x - y, c - x + y, c + x + y, c + x - y])
|
125 |
-
qsize = np.hypot(*x) * 2
|
126 |
-
|
127 |
-
# Shrink.
|
128 |
-
shrink = int(np.floor(qsize / output_size * 0.5))
|
129 |
-
if shrink > 1:
|
130 |
-
rsize = (int(np.rint(float(img.size[0]) / shrink)), int(np.rint(float(img.size[1]) / shrink)))
|
131 |
-
img = img.resize(rsize, PIL.Image.ANTIALIAS)
|
132 |
-
quad /= shrink
|
133 |
-
qsize /= shrink
|
134 |
-
|
135 |
-
# Crop.
|
136 |
-
border = max(int(np.rint(qsize * 0.1)), 3)
|
137 |
-
crop = (int(np.floor(min(quad[:,0]))), int(np.floor(min(quad[:,1]))), int(np.ceil(max(quad[:,0]))), int(np.ceil(max(quad[:,1]))))
|
138 |
-
crop = (max(crop[0] - border, 0), max(crop[1] - border, 0), min(crop[2] + border, img.size[0]), min(crop[3] + border, img.size[1]))
|
139 |
-
if crop[2] - crop[0] < img.size[0] or crop[3] - crop[1] < img.size[1]:
|
140 |
-
img = img.crop(crop)
|
141 |
-
quad -= crop[0:2]
|
142 |
-
|
143 |
-
# Pad.
|
144 |
-
pad = (int(np.floor(min(quad[:,0]))), int(np.floor(min(quad[:,1]))), int(np.ceil(max(quad[:,0]))), int(np.ceil(max(quad[:,1]))))
|
145 |
-
pad = (max(-pad[0] + border, 0), max(-pad[1] + border, 0), max(pad[2] - img.size[0] + border, 0), max(pad[3] - img.size[1] + border, 0))
|
146 |
-
if enable_padding and max(pad) > border - 4:
|
147 |
-
pad = np.maximum(pad, int(np.rint(qsize * 0.3)))
|
148 |
-
img = np.pad(np.float32(img), ((pad[1], pad[3]), (pad[0], pad[2]), (0, 0)), 'reflect')
|
149 |
-
h, w, _ = img.shape
|
150 |
-
y, x, _ = np.ogrid[:h, :w, :1]
|
151 |
-
mask = np.maximum(1.0 - np.minimum(np.float32(x) / pad[0], np.float32(w-1-x) / pad[2]), 1.0 - np.minimum(np.float32(y) / pad[1], np.float32(h-1-y) / pad[3]))
|
152 |
-
blur = qsize * 0.02
|
153 |
-
img += (scipy.ndimage.gaussian_filter(img, [blur, blur, 0]) - img) * np.clip(mask * 3.0 + 1.0, 0.0, 1.0)
|
154 |
-
img += (np.median(img, axis=(0,1)) - img) * np.clip(mask, 0.0, 1.0)
|
155 |
-
img = PIL.Image.fromarray(np.uint8(np.clip(np.rint(img), 0, 255)), 'RGB')
|
156 |
-
quad += pad[:2]
|
157 |
-
|
158 |
-
# Transform.
|
159 |
-
img = img.transform((transform_size, transform_size), PIL.Image.QUAD, (quad + 0.5).flatten(), PIL.Image.BILINEAR)
|
160 |
-
if output_size < transform_size:
|
161 |
-
img = img.resize((output_size, output_size), PIL.Image.ANTIALIAS)
|
162 |
-
|
163 |
-
return img
|
164 |
|
165 |
|
166 |
import requests
|
|
|
47 |
|
48 |
|
49 |
import os
|
50 |
+
#import dlib
|
51 |
import collections
|
52 |
from typing import Union, List
|
53 |
import numpy as np
|
54 |
from PIL import Image
|
55 |
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
import PIL.Image
|
58 |
import PIL.ImageFile
|
59 |
import numpy as np
|
60 |
import scipy.ndimage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
|
63 |
import requests
|