HairFastGAN
194 строки · 6.9 Кб
1import os2from pathlib import Path3
4import PIL5import dlib6import numpy as np7import scipy8import scipy.ndimage9import torch10from PIL import Image11from torchvision import transforms as T12
13from utils.drive import open_url14
15"""
16brief: face alignment with FFHQ method (https://github.com/NVlabs/ffhq-dataset)
17author: lzhbrian (https://lzhbrian.me)
18date: 2020.1.5
19note: code is heavily borrowed from
20https://github.com/NVlabs/ffhq-dataset
21http://dlib.net/face_landmark_detection.py.html
22
23requirements:
24apt install cmake
25conda install Pillow numpy scipy
26pip install dlib
27# download face landmark model from:
28# http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
29"""
30
31
32def get_landmark(filepath, predictor):33"""get landmark with dlib34:return: np.array shape=(68, 2)
35"""
36detector = dlib.get_frontal_face_detector()37
38img = dlib.load_rgb_image(filepath)39dets = detector(img, 1)40filepath = Path(filepath)41print(f"{filepath.name}: Number of faces detected: {len(dets)}")42shapes = [predictor(img, d) for k, d in enumerate(dets)]43
44lms = [np.array([[tt.x, tt.y] for tt in shape.parts()]) for shape in shapes]45
46return lms47
48
49def get_landmark_from_tensors(tensors: list[torch.Tensor | Image.Image | np.ndarray], predictor):50detector = dlib.get_frontal_face_detector()51transform = T.ToPILImage()52images = []53lms = []54
55for k, tensor in enumerate(tensors):56if isinstance(tensor, torch.Tensor):57img_pil = transform(tensor)58else:59img_pil = tensor60img = np.array(img_pil)61images.append(img_pil)62
63dets = detector(img, 1)64if len(dets) == 0:65raise ValueError(f"No faces detected in the image {k}.")66elif len(dets) == 1:67print(f"Number of faces detected: {len(dets)}")68else:69print(f"Number of faces detected: {len(dets)}, get largest face")70
71# Find the largest face72dets = sorted(dets, key=lambda det: det.width() * det.height(), reverse=True)73shape = predictor(img, dets[0])74lm = np.array([[tt.x, tt.y] for tt in shape.parts()])75lms.append(lm)76
77return images, lms78
79
80def align_face(data, predictor=None, is_filepath=False, return_tensors=True):81"""82:param data: filepath or list torch Tensors
83:return: list of PIL Images
84"""
85if predictor is None:86predictor_path = 'pretrained_models/ShapeAdaptor/shape_predictor_68_face_landmarks.dat'87
88if not os.path.isfile(predictor_path):89print("Downloading Shape Predictor")90data_io = open_url("https://drive.google.com/uc?id=1huhv8PYpNNKbGCLOaYUjOgR1pY5pmbJx")91with open(predictor_path, 'wb') as f:92f.write(data_io.getbuffer())93
94predictor = dlib.shape_predictor(predictor_path)95
96if is_filepath:97lms = get_landmark(data, predictor)98else:99if not isinstance(data, list):100data = [data]101images, lms = get_landmark_from_tensors(data, predictor)102
103imgs = []104for num_img, lm in enumerate(lms):105lm_chin = lm[0: 17] # left-right106lm_eyebrow_left = lm[17: 22] # left-right107lm_eyebrow_right = lm[22: 27] # left-right108lm_nose = lm[27: 31] # top-down109lm_nostrils = lm[31: 36] # top-down110lm_eye_left = lm[36: 42] # left-clockwise111lm_eye_right = lm[42: 48] # left-clockwise112lm_mouth_outer = lm[48: 60] # left-clockwise113lm_mouth_inner = lm[60: 68] # left-clockwise114
115# Calculate auxiliary vectors.116eye_left = np.mean(lm_eye_left, axis=0)117eye_right = np.mean(lm_eye_right, axis=0)118eye_avg = (eye_left + eye_right) * 0.5119eye_to_eye = eye_right - eye_left120mouth_left = lm_mouth_outer[0]121mouth_right = lm_mouth_outer[6]122mouth_avg = (mouth_left + mouth_right) * 0.5123eye_to_mouth = mouth_avg - eye_avg124
125# Choose oriented crop rectangle.126x = eye_to_eye - np.flipud(eye_to_mouth) * [-1, 1]127x /= np.hypot(*x)128x *= max(np.hypot(*eye_to_eye) * 2.0, np.hypot(*eye_to_mouth) * 1.8)129y = np.flipud(x) * [-1, 1]130c = eye_avg + eye_to_mouth * 0.1131quad = np.stack([c - x - y, c - x + y, c + x + y, c + x - y])132qsize = np.hypot(*x) * 2133
134# read image135if is_filepath:136img = PIL.Image.open(data)137else:138img = images[num_img]139
140output_size = 1024141# output_size = 256142transform_size = 4096143enable_padding = True144
145# Shrink.146shrink = int(np.floor(qsize / output_size * 0.5))147if shrink > 1:148rsize = (int(np.rint(float(img.size[0]) / shrink)), int(np.rint(float(img.size[1]) / shrink)))149img = img.resize(rsize, PIL.Image.ANTIALIAS)150quad /= shrink151qsize /= shrink152
153# Crop.154border = max(int(np.rint(qsize * 0.1)), 3)155crop = (int(np.floor(min(quad[:, 0]))), int(np.floor(min(quad[:, 1]))), int(np.ceil(max(quad[:, 0]))),156int(np.ceil(max(quad[:, 1]))))157crop = (max(crop[0] - border, 0), max(crop[1] - border, 0), min(crop[2] + border, img.size[0]),158min(crop[3] + border, img.size[1]))159if crop[2] - crop[0] < img.size[0] or crop[3] - crop[1] < img.size[1]:160img = img.crop(crop)161quad -= crop[0:2]162
163# Pad.164pad = (int(np.floor(min(quad[:, 0]))), int(np.floor(min(quad[:, 1]))), int(np.ceil(max(quad[:, 0]))),165int(np.ceil(max(quad[:, 1]))))166pad = (max(-pad[0] + border, 0), max(-pad[1] + border, 0), max(pad[2] - img.size[0] + border, 0),167max(pad[3] - img.size[1] + border, 0))168if enable_padding and max(pad) > border - 4:169pad = np.maximum(pad, int(np.rint(qsize * 0.3)))170img = np.pad(np.float32(img), ((pad[1], pad[3]), (pad[0], pad[2]), (0, 0)), 'reflect')171h, w, _ = img.shape172y, x, _ = np.ogrid[:h, :w, :1]173mask = np.maximum(1.0 - np.minimum(np.float32(x) / pad[0], np.float32(w - 1 - x) / pad[2]),1741.0 - np.minimum(np.float32(y) / pad[1], np.float32(h - 1 - y) / pad[3]))175blur = qsize * 0.02176img += (scipy.ndimage.gaussian_filter(img, [blur, blur, 0]) - img) * np.clip(mask * 3.0 + 1.0, 0.0, 1.0)177img += (np.median(img, axis=(0, 1)) - img) * np.clip(mask, 0.0, 1.0)178img = PIL.Image.fromarray(np.uint8(np.clip(np.rint(img), 0, 255)), 'RGB')179quad += pad[:2]180
181# Transform.182img = img.transform((transform_size, transform_size), PIL.Image.QUAD, (quad + 0.5).flatten(),183PIL.Image.BILINEAR)184if output_size < transform_size:185img = img.resize((output_size, output_size), PIL.Image.LANCZOS)186
187# Save aligned image.188imgs.append(img)189
190if return_tensors:191transform = T.ToTensor()192tensors = [transform(img).clamp(0, 1) for img in imgs]193return tensors194return imgs195