Spaces:
Running
Running
dreamlessx commited on
Commit ·
b55ddea
1
Parent(s): 0332541
Restore side-by-side output, add aspect-ratio-preserving resize
Browse files
app.py
CHANGED
|
@@ -167,6 +167,19 @@ def mask_composite(warped, original, mask):
|
|
| 167 |
return (warped * mask_3 + original * (1.0 - mask_3)).astype(np.uint8)
|
| 168 |
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
PROCEDURES = list(PROCEDURE_LANDMARKS.keys())
|
| 171 |
|
| 172 |
|
|
@@ -192,7 +205,7 @@ def process_image(image_rgb, procedure, intensity):
|
|
| 192 |
|
| 193 |
try:
|
| 194 |
image_bgr = cv2.cvtColor(np.asarray(image_rgb, dtype=np.uint8), cv2.COLOR_RGB2BGR)
|
| 195 |
-
image_bgr =
|
| 196 |
image_rgb_512 = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
|
| 197 |
except Exception as exc:
|
| 198 |
logger.error("Image conversion failed: %s", exc)
|
|
@@ -226,8 +239,6 @@ def process_image(image_rgb, procedure, intensity):
|
|
| 226 |
composited = mask_composite(warped, image_bgr, mask)
|
| 227 |
composited_rgb = cv2.cvtColor(composited, cv2.COLOR_BGR2RGB)
|
| 228 |
|
| 229 |
-
side_by_side = np.hstack([image_rgb_512, composited_rgb])
|
| 230 |
-
|
| 231 |
displacement = np.mean(
|
| 232 |
np.linalg.norm(manipulated.pixel_coords - face.pixel_coords, axis=1)
|
| 233 |
)
|
|
@@ -243,6 +254,8 @@ def process_image(image_rgb, procedure, intensity):
|
|
| 243 |
f"Processing time: {elapsed:.2f}s\n"
|
| 244 |
f"Mode: TPS (CPU)"
|
| 245 |
)
|
|
|
|
|
|
|
| 246 |
return wireframe_rgb, mask_vis, composited_rgb, side_by_side, info
|
| 247 |
|
| 248 |
except Exception as exc:
|
|
@@ -260,7 +273,7 @@ def compare_procedures(image_rgb, intensity):
|
|
| 260 |
|
| 261 |
try:
|
| 262 |
image_bgr = cv2.cvtColor(np.asarray(image_rgb, dtype=np.uint8), cv2.COLOR_RGB2BGR)
|
| 263 |
-
image_bgr =
|
| 264 |
|
| 265 |
face = extract_landmarks(image_bgr)
|
| 266 |
if face is None:
|
|
@@ -291,7 +304,7 @@ def intensity_sweep(image_rgb, procedure):
|
|
| 291 |
|
| 292 |
try:
|
| 293 |
image_bgr = cv2.cvtColor(np.asarray(image_rgb, dtype=np.uint8), cv2.COLOR_RGB2BGR)
|
| 294 |
-
image_bgr =
|
| 295 |
|
| 296 |
face = extract_landmarks(image_bgr)
|
| 297 |
if face is None:
|
|
|
|
| 167 |
return (warped * mask_3 + original * (1.0 - mask_3)).astype(np.uint8)
|
| 168 |
|
| 169 |
|
| 170 |
+
def resize_preserve_aspect(image, size=512):
|
| 171 |
+
"""Resize image to size x size, padding to preserve aspect ratio."""
|
| 172 |
+
h, w = image.shape[:2]
|
| 173 |
+
scale = size / max(h, w)
|
| 174 |
+
new_w, new_h = int(w * scale), int(h * scale)
|
| 175 |
+
resized = cv2.resize(image, (new_w, new_h))
|
| 176 |
+
canvas = np.zeros((size, size, 3), dtype=np.uint8)
|
| 177 |
+
y_off = (size - new_h) // 2
|
| 178 |
+
x_off = (size - new_w) // 2
|
| 179 |
+
canvas[y_off : y_off + new_h, x_off : x_off + new_w] = resized
|
| 180 |
+
return canvas
|
| 181 |
+
|
| 182 |
+
|
| 183 |
PROCEDURES = list(PROCEDURE_LANDMARKS.keys())
|
| 184 |
|
| 185 |
|
|
|
|
| 205 |
|
| 206 |
try:
|
| 207 |
image_bgr = cv2.cvtColor(np.asarray(image_rgb, dtype=np.uint8), cv2.COLOR_RGB2BGR)
|
| 208 |
+
image_bgr = resize_preserve_aspect(image_bgr, 512)
|
| 209 |
image_rgb_512 = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
|
| 210 |
except Exception as exc:
|
| 211 |
logger.error("Image conversion failed: %s", exc)
|
|
|
|
| 239 |
composited = mask_composite(warped, image_bgr, mask)
|
| 240 |
composited_rgb = cv2.cvtColor(composited, cv2.COLOR_BGR2RGB)
|
| 241 |
|
|
|
|
|
|
|
| 242 |
displacement = np.mean(
|
| 243 |
np.linalg.norm(manipulated.pixel_coords - face.pixel_coords, axis=1)
|
| 244 |
)
|
|
|
|
| 254 |
f"Processing time: {elapsed:.2f}s\n"
|
| 255 |
f"Mode: TPS (CPU)"
|
| 256 |
)
|
| 257 |
+
side_by_side = np.hstack([image_rgb_512, composited_rgb])
|
| 258 |
+
|
| 259 |
return wireframe_rgb, mask_vis, composited_rgb, side_by_side, info
|
| 260 |
|
| 261 |
except Exception as exc:
|
|
|
|
| 273 |
|
| 274 |
try:
|
| 275 |
image_bgr = cv2.cvtColor(np.asarray(image_rgb, dtype=np.uint8), cv2.COLOR_RGB2BGR)
|
| 276 |
+
image_bgr = resize_preserve_aspect(image_bgr, 512)
|
| 277 |
|
| 278 |
face = extract_landmarks(image_bgr)
|
| 279 |
if face is None:
|
|
|
|
| 304 |
|
| 305 |
try:
|
| 306 |
image_bgr = cv2.cvtColor(np.asarray(image_rgb, dtype=np.uint8), cv2.COLOR_RGB2BGR)
|
| 307 |
+
image_bgr = resize_preserve_aspect(image_bgr, 512)
|
| 308 |
|
| 309 |
face = extract_landmarks(image_bgr)
|
| 310 |
if face is None:
|