fffiloni commited on
Commit
62a5a90
1 Parent(s): 139dfb5

Create new file

Browse files
Files changed (1) hide show
  1. utils.py +115 -0
utils.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import dlib
2
+ import numpy as np
3
+ import scipy
4
+ import scipy.ndimage
5
+ from PIL import Image
6
+ from huggingface_hub import hf_hub_download
7
+
8
+ shape_predictor_path = hf_hub_download(repo_id="jjeamin/ArcaneStyleTransfer", filename="shape_predictor_68_face_landmarks.dat")
9
+
10
+ def get_landmark(img, predictor):
11
+ """get landmark with dlib
12
+ :return: np.array shape=(68, 2)
13
+ """
14
+ detector = dlib.get_frontal_face_detector()
15
+
16
+ dets = detector(img, 1)
17
+ assert len(dets) > 0, "Face not detected, try another face image"
18
+
19
+ for k, d in enumerate(dets):
20
+ shape = predictor(img, d)
21
+
22
+ t = list(shape.parts())
23
+ a = []
24
+ for tt in t:
25
+ a.append([tt.x, tt.y])
26
+ lm = np.array(a)
27
+ return lm
28
+
29
+
30
+ def align_face(img, output_size=512, transform_size=1024, enable_padding=True):
31
+
32
+ """
33
+ :param filepath: str
34
+ :return: PIL Image
35
+ """
36
+ np_img = np.array(img)
37
+ predictor = dlib.shape_predictor(shape_predictor_path)
38
+ lm = get_landmark(np_img, predictor)
39
+
40
+ lm_chin = lm[0: 17] # left-right
41
+ lm_eyebrow_left = lm[17: 22] # left-right
42
+ lm_eyebrow_right = lm[22: 27] # left-right
43
+ lm_nose = lm[27: 31] # top-down
44
+ lm_nostrils = lm[31: 36] # top-down
45
+ lm_eye_left = lm[36: 42] # left-clockwise
46
+ lm_eye_right = lm[42: 48] # left-clockwise
47
+ lm_mouth_outer = lm[48: 60] # left-clockwise
48
+ lm_mouth_inner = lm[60: 68] # left-clockwise
49
+
50
+ # Calculate auxiliary vectors.
51
+ eye_left = np.mean(lm_eye_left, axis=0)
52
+ eye_right = np.mean(lm_eye_right, axis=0)
53
+ eye_avg = (eye_left + eye_right) * 0.5
54
+ eye_to_eye = eye_right - eye_left
55
+ mouth_left = lm_mouth_outer[0]
56
+ mouth_right = lm_mouth_outer[6]
57
+ mouth_avg = (mouth_left + mouth_right) * 0.5
58
+ eye_to_mouth = mouth_avg - eye_avg
59
+
60
+ # Choose oriented crop rectangle.
61
+ x = eye_to_eye - np.flipud(eye_to_mouth) * [-1, 1]
62
+ x /= np.hypot(*x)
63
+ x *= max(np.hypot(*eye_to_eye) * 2.0, np.hypot(*eye_to_mouth) * 1.8)
64
+ y = np.flipud(x) * [-1, 1]
65
+ c = eye_avg + eye_to_mouth * 0.1
66
+ quad = np.stack([c - x - y, c - x + y, c + x + y, c + x - y])
67
+ qsize = np.hypot(*x) * 2
68
+
69
+ # read image
70
+ transform_size = output_size
71
+ enable_padding = True
72
+
73
+ # Shrink.
74
+ shrink = int(np.floor(qsize / output_size * 0.5))
75
+ if shrink > 1:
76
+ rsize = (int(np.rint(float(img.size[0]) / shrink)), int(np.rint(float(img.size[1]) / shrink)))
77
+ img = img.resize(rsize, Image.ANTIALIAS)
78
+ quad /= shrink
79
+ qsize /= shrink
80
+
81
+ # Crop.
82
+ border = max(int(np.rint(qsize * 0.1)), 3)
83
+ crop = (int(np.floor(min(quad[:, 0]))), int(np.floor(min(quad[:, 1]))), int(np.ceil(max(quad[:, 0]))),
84
+ int(np.ceil(max(quad[:, 1]))))
85
+ crop = (max(crop[0] - border, 0), max(crop[1] - border, 0), min(crop[2] + border, img.size[0]),
86
+ min(crop[3] + border, img.size[1]))
87
+ if crop[2] - crop[0] < img.size[0] or crop[3] - crop[1] < img.size[1]:
88
+ img = img.crop(crop)
89
+ quad -= crop[0:2]
90
+
91
+ # Pad.
92
+ pad = (int(np.floor(min(quad[:, 0]))), int(np.floor(min(quad[:, 1]))), int(np.ceil(max(quad[:, 0]))),
93
+ int(np.ceil(max(quad[:, 1]))))
94
+ pad = (max(-pad[0] + border, 0), max(-pad[1] + border, 0), max(pad[2] - img.size[0] + border, 0),
95
+ max(pad[3] - img.size[1] + border, 0))
96
+ if enable_padding and max(pad) > border - 4:
97
+ pad = np.maximum(pad, int(np.rint(qsize * 0.3)))
98
+ img = np.pad(np.float32(img), ((pad[1], pad[3]), (pad[0], pad[2]), (0, 0)), 'reflect')
99
+ h, w, _ = img.shape
100
+ y, x, _ = np.ogrid[:h, :w, :1]
101
+ mask = np.maximum(1.0 - np.minimum(np.float32(x) / pad[0], np.float32(w - 1 - x) / pad[2]),
102
+ 1.0 - np.minimum(np.float32(y) / pad[1], np.float32(h - 1 - y) / pad[3]))
103
+ blur = qsize * 0.02
104
+ img += (scipy.ndimage.gaussian_filter(img, [blur, blur, 0]) - img) * np.clip(mask * 3.0 + 1.0, 0.0, 1.0)
105
+ img += (np.median(img, axis=(0, 1)) - img) * np.clip(mask, 0.0, 1.0)
106
+ img = Image.fromarray(np.uint8(np.clip(np.rint(img), 0, 255)), 'RGB')
107
+ quad += pad[:2]
108
+
109
+ # Transform.
110
+ img = img.transform((transform_size, transform_size), Image.QUAD, (quad + 0.5).flatten(), Image.BILINEAR)
111
+ if output_size < transform_size:
112
+ img = img.resize((output_size, output_size), Image.ANTIALIAS)
113
+
114
+ # Return aligned image.
115
+ return img