Update vtoonify_model.py
Browse files- vtoonify_model.py +10 -12
vtoonify_model.py
CHANGED
@@ -22,6 +22,8 @@ import logging
|
|
22 |
from PIL import Image
|
23 |
|
24 |
|
|
|
|
|
25 |
# Configure logging
|
26 |
logging.basicConfig(level=logging.INFO)
|
27 |
|
@@ -113,6 +115,13 @@ class Model():
|
|
113 |
|
114 |
return landmarks_68
|
115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
def detect_and_align(self, frame, top, bottom, left, right, return_para=False):
|
117 |
message = 'Error: no face detected! Please retry or change the photo.'
|
118 |
instyle = None
|
@@ -177,15 +186,6 @@ class Model():
|
|
177 |
|
178 |
return np.array(img)
|
179 |
|
180 |
-
def detect_and_align_image(self, image: str, top: int, bottom: int, left: int, right: int) -> tuple:
|
181 |
-
if image is None:
|
182 |
-
return np.zeros((256, 256, 3), np.uint8), None, 'Error: fail to load empty file.'
|
183 |
-
frame = cv2.imread(image)
|
184 |
-
if frame is None:
|
185 |
-
return np.zeros((256, 256, 3), np.uint8), None, 'Error: fail to load the image.'
|
186 |
-
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
187 |
-
return self.detect_and_align(frame, top, bottom, left, right)
|
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.'
|
@@ -216,6 +216,4 @@ class Model():
|
|
216 |
"""Convert a tensor image to OpenCV format."""
|
217 |
tmp = ((img.cpu().numpy().transpose(1, 2, 0) + 1.0) * 127.5).astype(np.uint8).copy()
|
218 |
logging.debug(f"Converted image shape: {tmp.shape}, strides: {tmp.strides}")
|
219 |
-
return cv2.cvtColor(tmp, cv2.COLOR_RGB2BGR)
|
220 |
-
|
221 |
-
|
|
|
22 |
from PIL import Image
|
23 |
|
24 |
|
25 |
+
|
26 |
+
|
27 |
# Configure logging
|
28 |
logging.basicConfig(level=logging.INFO)
|
29 |
|
|
|
115 |
|
116 |
return landmarks_68
|
117 |
|
118 |
+
def detect_and_align_image(self, frame_bgr: np.ndarray, top: int, bottom: int, left: int, right: int) -> tuple:
|
119 |
+
if frame_bgr is None:
|
120 |
+
return np.zeros((256, 256, 3), np.uint8), None, 'Error: fail to load the image.'
|
121 |
+
|
122 |
+
# Proceed with detection and alignment
|
123 |
+
return self.detect_and_align(frame_bgr, top, bottom, left, right)
|
124 |
+
|
125 |
def detect_and_align(self, frame, top, bottom, left, right, return_para=False):
|
126 |
message = 'Error: no face detected! Please retry or change the photo.'
|
127 |
instyle = None
|
|
|
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.'
|
|
|
216 |
"""Convert a tensor image to OpenCV format."""
|
217 |
tmp = ((img.cpu().numpy().transpose(1, 2, 0) + 1.0) * 127.5).astype(np.uint8).copy()
|
218 |
logging.debug(f"Converted image shape: {tmp.shape}, strides: {tmp.strides}")
|
219 |
+
return cv2.cvtColor(tmp, cv2.COLOR_RGB2BGR)
|
|
|
|