Ashrafb commited on
Commit
f4dd637
·
verified ·
1 Parent(s): ae4009d

Update vtoonify_model.py

Browse files
Files changed (1) hide show
  1. vtoonify_model.py +9 -3
vtoonify_model.py CHANGED
@@ -186,14 +186,18 @@ class Model():
186
 
187
  return np.array(img)
188
 
 
189
  def image_toonify(self, aligned_face: np.ndarray, instyle: torch.Tensor, exstyle: torch.Tensor, style_degree: float, style_type: str) -> tuple:
190
  if instyle is None or aligned_face is None:
 
191
  return np.zeros((256, 256, 3), np.uint8), 'Oops, something wrong with the input. Please go to Step 2 and Rescale Image/First Frame again.'
 
192
  if self.style_name != style_type:
193
  exstyle, _ = self.load_model(style_type)
194
  if exstyle is None:
 
195
  return np.zeros((256, 256, 3), np.uint8), 'Oops, something wrong with the style type. Please go to Step 1 and load model again.'
196
-
197
  try:
198
  with torch.no_grad():
199
  if self.color_transfer:
@@ -202,7 +206,9 @@ class Model():
202
  s_w = instyle.clone()
203
  s_w[:, :7] = exstyle[:, :7]
204
 
205
- x = self.transform(aligned_face).unsqueeze(dim=0).to(self.device)
 
 
206
  logging.info(f"Input to VToonify shape: {x.shape}")
207
  x_p = F.interpolate(self.parsingpredictor(2 * (F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=False)))[0],
208
  scale_factor=0.5, recompute_scale_factor=False).detach()
@@ -213,10 +219,10 @@ class Model():
213
  print('*** Toonify %dx%d image with style of %s' % (y_tilde.shape[2], y_tilde.shape[3], style_type))
214
 
215
  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)
 
216
  except Exception as e:
217
  logging.error(f"Error during model execution: {e}")
218
  return np.zeros((256, 256, 3), np.uint8), f"Error during processing: {str(e)}"
219
-
220
  def tensor2cv2(self, img):
221
  """Convert a tensor image to OpenCV format."""
222
  tmp = ((img.cpu().numpy().transpose(1, 2, 0) + 1.0) * 127.5).astype(np.uint8).copy()
 
186
 
187
  return np.array(img)
188
 
189
+
190
  def image_toonify(self, aligned_face: np.ndarray, instyle: torch.Tensor, exstyle: torch.Tensor, style_degree: float, style_type: str) -> tuple:
191
  if instyle is None or aligned_face is None:
192
+ logging.error("Invalid input: instyle or aligned_face is None.")
193
  return np.zeros((256, 256, 3), np.uint8), 'Oops, something wrong with the input. Please go to Step 2 and Rescale Image/First Frame again.'
194
+
195
  if self.style_name != style_type:
196
  exstyle, _ = self.load_model(style_type)
197
  if exstyle is None:
198
+ logging.error("Failed to load style model.")
199
  return np.zeros((256, 256, 3), np.uint8), 'Oops, something wrong with the style type. Please go to Step 1 and load model again.'
200
+
201
  try:
202
  with torch.no_grad():
203
  if self.color_transfer:
 
206
  s_w = instyle.clone()
207
  s_w[:, :7] = exstyle[:, :7]
208
 
209
+ # Ensure the input is resized to 256x256
210
+ aligned_face_resized = cv2.resize(aligned_face, (256, 256))
211
+ x = self.transform(aligned_face_resized).unsqueeze(dim=0).to(self.device)
212
  logging.info(f"Input to VToonify shape: {x.shape}")
213
  x_p = F.interpolate(self.parsingpredictor(2 * (F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=False)))[0],
214
  scale_factor=0.5, recompute_scale_factor=False).detach()
 
219
  print('*** Toonify %dx%d image with style of %s' % (y_tilde.shape[2], y_tilde.shape[3], style_type))
220
 
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
  except Exception as e:
224
  logging.error(f"Error during model execution: {e}")
225
  return np.zeros((256, 256, 3), np.uint8), f"Error during processing: {str(e)}"
 
226
  def tensor2cv2(self, img):
227
  """Convert a tensor image to OpenCV format."""
228
  tmp = ((img.cpu().numpy().transpose(1, 2, 0) + 1.0) * 127.5).astype(np.uint8).copy()