Update vtoonify_model.py
Browse files- vtoonify_model.py +21 -55
vtoonify_model.py
CHANGED
@@ -115,71 +115,37 @@ class Model():
|
|
115 |
with torch.no_grad():
|
116 |
exstyle = self.vtoonify.zplus2wplus(exstyle)
|
117 |
return exstyle, 'Model of %s loaded.' % (style_type)
|
118 |
-
|
119 |
def detect_and_align(self, frame, top, bottom, left, right, return_para=False):
|
120 |
message = 'Error: no face detected! Please retry or change the photo.'
|
|
|
121 |
instyle = None
|
122 |
h, w, scale = 0, 0, 0
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
I = self.transform(aligned_face).unsqueeze(dim=0).to(self.device)
|
138 |
instyle = self.pspencoder(I)
|
139 |
instyle = self.vtoonify.zplus2wplus(instyle)
|
140 |
-
message = 'Successfully
|
141 |
-
|
142 |
-
|
143 |
-
frame = np.zeros((256, 256, 3), np.uint8)
|
144 |
else:
|
145 |
-
|
146 |
-
frame = np.zeros((256, 256, 3), np.uint8)
|
147 |
-
|
148 |
if return_para:
|
149 |
-
return frame, instyle, message,
|
150 |
return frame, instyle, message
|
151 |
|
152 |
-
|
153 |
-
# Calculate auxiliary vectors for alignment
|
154 |
-
eye_left = np.mean(landmarks[36:42], axis=0)
|
155 |
-
eye_right = np.mean(landmarks[42:48], axis=0)
|
156 |
-
mouth_left = landmarks[48]
|
157 |
-
mouth_right = landmarks[54]
|
158 |
-
|
159 |
-
# Calculate transformation parameters
|
160 |
-
eye_center = (eye_left + eye_right) / 2
|
161 |
-
mouth_center = (mouth_left + mouth_right) / 2
|
162 |
-
eye_to_eye = eye_right - eye_left
|
163 |
-
eye_to_mouth = mouth_center - eye_center
|
164 |
-
|
165 |
-
# Define the transformation matrix
|
166 |
-
x = eye_to_eye - np.flipud(eye_to_mouth) * [-1, 1]
|
167 |
-
x /= np.hypot(*x)
|
168 |
-
x *= np.hypot(*eye_to_eye) * 2.0
|
169 |
-
y = np.flipud(x) * [-1, 1]
|
170 |
-
c = eye_center + eye_to_mouth * 0.1
|
171 |
-
quad = np.stack([c - x - y, c - x + y, c + x + y, c + x - y])
|
172 |
-
qsize = np.hypot(*x) * 2
|
173 |
-
|
174 |
-
# Transform and crop the image
|
175 |
-
transform_size = 256
|
176 |
-
output_size = 256
|
177 |
-
img = Image.fromarray(image)
|
178 |
-
img = img.transform((transform_size, transform_size), Image.QUAD, (quad + 0.5).flatten(), Image.BILINEAR)
|
179 |
-
if output_size < transform_size:
|
180 |
-
img = img.resize((output_size, output_size), Image.ANTIALIAS)
|
181 |
-
|
182 |
-
return np.array(img)
|
183 |
|
184 |
def detect_and_align_image(self, frame_rgb: np.ndarray, top: int, bottom: int, left: int, right: int) -> tuple:
|
185 |
if frame_rgb is None:
|
|
|
115 |
with torch.no_grad():
|
116 |
exstyle = self.vtoonify.zplus2wplus(exstyle)
|
117 |
return exstyle, 'Model of %s loaded.' % (style_type)
|
|
|
118 |
def detect_and_align(self, frame, top, bottom, left, right, return_para=False):
|
119 |
message = 'Error: no face detected! Please retry or change the photo.'
|
120 |
+
paras = get_video_crop_parameter(frame, self.landmarkpredictor, [left, right, top, bottom])
|
121 |
instyle = None
|
122 |
h, w, scale = 0, 0, 0
|
123 |
+
if paras is not None:
|
124 |
+
h,w,top,bottom,left,right,scale = paras
|
125 |
+
H, W = int(bottom-top), int(right-left)
|
126 |
+
# for HR image, we apply gaussian blur to it to avoid over-sharp stylization results
|
127 |
+
kernel_1d = np.array([[0.125],[0.375],[0.375],[0.125]])
|
128 |
+
if scale <= 0.75:
|
129 |
+
frame = cv2.sepFilter2D(frame, -1, kernel_1d, kernel_1d)
|
130 |
+
if scale <= 0.375:
|
131 |
+
frame = cv2.sepFilter2D(frame, -1, kernel_1d, kernel_1d)
|
132 |
+
frame = cv2.resize(frame, (w, h))[top:bottom, left:right]
|
133 |
+
with torch.no_grad():
|
134 |
+
I = align_face(frame, self.landmarkpredictor)
|
135 |
+
if I is not None:
|
136 |
+
I = self.transform(I).unsqueeze(dim=0).to(self.device)
|
|
|
137 |
instyle = self.pspencoder(I)
|
138 |
instyle = self.vtoonify.zplus2wplus(instyle)
|
139 |
+
message = 'Successfully rescale the frame to (%d, %d)'%(bottom-top, right-left)
|
140 |
+
else:
|
141 |
+
frame = np.zeros((256,256,3), np.uint8)
|
|
|
142 |
else:
|
143 |
+
frame = np.zeros((256,256,3), np.uint8)
|
|
|
|
|
144 |
if return_para:
|
145 |
+
return frame, instyle, message, w, h, top, bottom, left, right, scale
|
146 |
return frame, instyle, message
|
147 |
|
148 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
def detect_and_align_image(self, frame_rgb: np.ndarray, top: int, bottom: int, left: int, right: int) -> tuple:
|
151 |
if frame_rgb is None:
|