Spaces:
Running
on
T4
Running
on
T4
PKUWilliamYang
commited on
Commit
•
e541a90
1
Parent(s):
7ab3ef3
Update vtoonify_model.py
Browse files- vtoonify_model.py +6 -5
vtoonify_model.py
CHANGED
@@ -205,10 +205,10 @@ class Model():
|
|
205 |
print(style_type + ' ' + self.style_name)
|
206 |
if instyle is None or aligned_face is None:
|
207 |
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.'
|
|
|
|
|
208 |
if exstyle is None:
|
209 |
return np.zeros((256,256,3), np.uint8), 'Opps, something wrong with the style type. Please go to Step 1 and load model again.'
|
210 |
-
if exstyle is None:
|
211 |
-
exstyle = self.exstyle
|
212 |
with torch.no_grad():
|
213 |
if self.color_transfer:
|
214 |
s_w = exstyle
|
@@ -222,17 +222,18 @@ class Model():
|
|
222 |
inputs = torch.cat((x, x_p/16.), dim=1)
|
223 |
y_tilde = self.vtoonify(inputs, s_w.repeat(inputs.size(0), 1, 1), d_s = style_degree)
|
224 |
y_tilde = torch.clamp(y_tilde, -1, 1)
|
225 |
-
print('*** Toonify %dx%d image'%(y_tilde.shape[2], y_tilde.shape[3]))
|
226 |
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)
|
227 |
|
228 |
def video_tooniy(self, aligned_video: str, instyle: torch.Tensor, exstyle: torch.Tensor, style_degree: float, style_type: str) -> tuple[str, str]:
|
229 |
-
print(style_type + ' ' + self.style_name)
|
230 |
if aligned_video is None:
|
231 |
return 'default.mp4', 'Opps, something wrong with the input. Please go to Step 2 and Rescale Video again.'
|
232 |
video_cap = cv2.VideoCapture(aligned_video)
|
233 |
if instyle is None or aligned_video is None or video_cap.get(7) == 0:
|
234 |
video_cap.release()
|
235 |
return 'default.mp4', 'Opps, something wrong with the input. Please go to Step 2 and Rescale Video again.'
|
|
|
|
|
236 |
if exstyle is None:
|
237 |
return 'default.mp4', 'Opps, something wrong with the style type. Please go to Step 1 and load model again.'
|
238 |
num = min(self.video_limit_gpu, int(video_cap.get(7)))
|
@@ -251,7 +252,7 @@ class Model():
|
|
251 |
batch_size = min(max(1, int(4 * 400 * 360/ video_cap.get(3) / video_cap.get(4))), 4)
|
252 |
else:
|
253 |
batch_size = 1
|
254 |
-
print('*** Toonify using batch size of %d on %dx%d video of %d frames'%(batch_size, int(video_cap.get(3)*4), int(video_cap.get(4)*4), num))
|
255 |
with torch.no_grad():
|
256 |
if self.color_transfer:
|
257 |
s_w = exstyle
|
|
|
205 |
print(style_type + ' ' + self.style_name)
|
206 |
if instyle is None or aligned_face is None:
|
207 |
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.'
|
208 |
+
if self.style_name != style_type:
|
209 |
+
exstyle = self.load_model(style_type)
|
210 |
if exstyle is None:
|
211 |
return np.zeros((256,256,3), np.uint8), 'Opps, something wrong with the style type. Please go to Step 1 and load model again.'
|
|
|
|
|
212 |
with torch.no_grad():
|
213 |
if self.color_transfer:
|
214 |
s_w = exstyle
|
|
|
222 |
inputs = torch.cat((x, x_p/16.), dim=1)
|
223 |
y_tilde = self.vtoonify(inputs, s_w.repeat(inputs.size(0), 1, 1), d_s = style_degree)
|
224 |
y_tilde = torch.clamp(y_tilde, -1, 1)
|
225 |
+
print('*** Toonify %dx%d image with style of %s'%(y_tilde.shape[2], y_tilde.shape[3], style_type))
|
226 |
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)
|
227 |
|
228 |
def video_tooniy(self, aligned_video: str, instyle: torch.Tensor, exstyle: torch.Tensor, style_degree: float, style_type: str) -> tuple[str, str]:
|
|
|
229 |
if aligned_video is None:
|
230 |
return 'default.mp4', 'Opps, something wrong with the input. Please go to Step 2 and Rescale Video again.'
|
231 |
video_cap = cv2.VideoCapture(aligned_video)
|
232 |
if instyle is None or aligned_video is None or video_cap.get(7) == 0:
|
233 |
video_cap.release()
|
234 |
return 'default.mp4', 'Opps, something wrong with the input. Please go to Step 2 and Rescale Video again.'
|
235 |
+
if self.style_name != style_type:
|
236 |
+
exstyle = self.load_model(style_type)
|
237 |
if exstyle is None:
|
238 |
return 'default.mp4', 'Opps, something wrong with the style type. Please go to Step 1 and load model again.'
|
239 |
num = min(self.video_limit_gpu, int(video_cap.get(7)))
|
|
|
252 |
batch_size = min(max(1, int(4 * 400 * 360/ video_cap.get(3) / video_cap.get(4))), 4)
|
253 |
else:
|
254 |
batch_size = 1
|
255 |
+
print('*** Toonify using batch size of %d on %dx%d video of %d frames with style of %s'%(batch_size, int(video_cap.get(3)*4), int(video_cap.get(4)*4), num, style_type))
|
256 |
with torch.no_grad():
|
257 |
if self.color_transfer:
|
258 |
s_w = exstyle
|