Spaces:
Paused
Paused
Update vtoonify_model.py
Browse files- vtoonify_model.py +23 -19
vtoonify_model.py
CHANGED
|
@@ -200,26 +200,30 @@ class Model():
|
|
| 200 |
|
| 201 |
return 'input.mp4', instyle, 'Successfully rescale the video to (%d, %d)'%(bottom-top, right-left)
|
| 202 |
|
| 203 |
-
def image_toonify(self, aligned_face: np.ndarray, instyle: torch.Tensor, exstyle: torch.Tensor, style_degree: float, style_type: str) -> tuple[np.ndarray, str]:
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
inputs = torch.cat((x, x_p / 16.), dim=1)
|
| 218 |
-
y_tilde = self.vtoonify(inputs, s_w.repeat(inputs.size(0), 1, 1), d_s=style_degree)
|
| 219 |
-
y_tilde = torch.clamp(y_tilde, -1, 1)
|
| 220 |
-
print('*** Toonify %dx%d image with style of %s' % (y_tilde.shape[2], y_tilde.shape[3], style_type))
|
| 221 |
-
return ((y_tilde[0].cpu().numpy().transpose(1, 2, 0) + 1.0) * 127.5).astype(np.uint8), 'Successfully toonify the image with style of %s' % (self.style_name)
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
def video_tooniy(self, aligned_video: str, instyle: torch.Tensor, exstyle: torch.Tensor, style_degree: float, style_type: str) -> tuple[str, str]:
|
| 224 |
#print(style_type + ' ' + self.style_name)
|
| 225 |
if aligned_video is None:
|
|
|
|
| 200 |
|
| 201 |
return 'input.mp4', instyle, 'Successfully rescale the video to (%d, %d)'%(bottom-top, right-left)
|
| 202 |
|
| 203 |
+
def image_toonify(self, aligned_face: np.ndarray, instyle: torch.Tensor, exstyle: torch.Tensor, style_degree: float, style_type: str) -> tuple[np.ndarray, str]:
|
| 204 |
+
#print(style_type + ' ' + self.style_name)
|
| 205 |
+
if instyle is None or aligned_face is None:
|
| 206 |
+
return np.zeros((256,256,3), np.uint8), 'Opps, something wrong with the input. Please go to Step 2 and Rescale Image/First Frame again.'
|
| 207 |
+
if self.style_name != style_type:
|
| 208 |
+
exstyle, _ = self.load_model(style_type)
|
| 209 |
+
if exstyle is None:
|
| 210 |
+
return np.zeros((256,256,3), np.uint8), 'Opps, something wrong with the style type. Please go to Step 1 and load model again.'
|
| 211 |
+
with torch.no_grad():
|
| 212 |
+
if self.color_transfer:
|
| 213 |
+
s_w = exstyle
|
| 214 |
+
else:
|
| 215 |
+
s_w = instyle.clone()
|
| 216 |
+
s_w[:,:7] = exstyle[:,:7]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
+
x = self.transform(aligned_face).unsqueeze(dim=0).to(self.device)
|
| 219 |
+
x_p = F.interpolate(self.parsingpredictor(2*(F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=False)))[0],
|
| 220 |
+
scale_factor=0.5, recompute_scale_factor=False).detach()
|
| 221 |
+
inputs = torch.cat((x, x_p/16.), dim=1)
|
| 222 |
+
y_tilde = self.vtoonify(inputs, s_w.repeat(inputs.size(0), 1, 1), d_s = style_degree)
|
| 223 |
+
y_tilde = torch.clamp(y_tilde, -1, 1)
|
| 224 |
+
print('*** Toonify %dx%d image with style of %s'%(y_tilde.shape[2], y_tilde.shape[3], style_type))
|
| 225 |
+
return ((y_tilde[0].cpu().numpy().transpose(1, 2, 0) + 1.0) * 127.5).astype(np.uint8), 'Successfully toonify the image with style of %s'%(self.style_name)
|
| 226 |
+
|
| 227 |
def video_tooniy(self, aligned_video: str, instyle: torch.Tensor, exstyle: torch.Tensor, style_degree: float, style_type: str) -> tuple[str, str]:
|
| 228 |
#print(style_type + ' ' + self.style_name)
|
| 229 |
if aligned_video is None:
|