Spaces:
Paused
Paused
github-actions[bot] commited on
Commit ·
26ba662
1
Parent(s): 485bb51
Automated Deploy: d1298af262438255194f9ecd833cc04e10de85a2
Browse files- routes/harmonization.py +50 -35
routes/harmonization.py
CHANGED
|
@@ -7,7 +7,7 @@ import base64
|
|
| 7 |
import io
|
| 8 |
|
| 9 |
import numpy as np
|
| 10 |
-
from fastapi import APIRouter
|
| 11 |
from PIL import Image
|
| 12 |
|
| 13 |
from errors import bad_request
|
|
@@ -64,41 +64,56 @@ async def harmonize(payload: dict):
|
|
| 64 |
mode: actual mode used
|
| 65 |
elapsed_ms: processing time
|
| 66 |
"""
|
| 67 |
-
|
| 68 |
-
mask_value = payload.get("mask")
|
| 69 |
-
original_value = payload.get("original")
|
| 70 |
-
mode = payload.get("mode", "fast")
|
| 71 |
-
strength = float(payload.get("strength", 0.7))
|
| 72 |
-
|
| 73 |
-
if not image_value or not mask_value:
|
| 74 |
-
bad_request("Both 'image' and 'mask' are required.")
|
| 75 |
-
|
| 76 |
-
composite = await asyncio.to_thread(_decode_image, image_value)
|
| 77 |
-
mask = await asyncio.to_thread(_decode_mask, mask_value)
|
| 78 |
-
original = await asyncio.to_thread(_decode_image, original_value) if original_value else None
|
| 79 |
-
|
| 80 |
-
# Ensure mask matches composite dimensions
|
| 81 |
-
if mask.shape[:2] != composite.shape[:2]:
|
| 82 |
-
import cv2
|
| 83 |
-
mask = cv2.resize(mask, (composite.shape[1], composite.shape[0]), interpolation=cv2.INTER_NEAREST)
|
| 84 |
-
|
| 85 |
-
if mode == "generate":
|
| 86 |
-
# Diffusion inpainting mode
|
| 87 |
-
result = await asyncio.to_thread(
|
| 88 |
-
_run_diffusion_inpaint, composite, mask, original, strength
|
| 89 |
-
)
|
| 90 |
-
else:
|
| 91 |
-
# Harmonization mode (fast or neural)
|
| 92 |
-
from services.harmonization.harmonizer import harmonize_composite
|
| 93 |
-
result = await asyncio.to_thread(
|
| 94 |
-
harmonize_composite, composite, mask, original, strength, mode
|
| 95 |
-
)
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
|
| 104 |
def _run_diffusion_inpaint(
|
|
|
|
| 7 |
import io
|
| 8 |
|
| 9 |
import numpy as np
|
| 10 |
+
from fastapi import APIRouter, HTTPException
|
| 11 |
from PIL import Image
|
| 12 |
|
| 13 |
from errors import bad_request
|
|
|
|
| 64 |
mode: actual mode used
|
| 65 |
elapsed_ms: processing time
|
| 66 |
"""
|
| 67 |
+
import traceback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
try:
|
| 70 |
+
image_value = payload.get("image")
|
| 71 |
+
mask_value = payload.get("mask")
|
| 72 |
+
original_value = payload.get("original")
|
| 73 |
+
mode = payload.get("mode", "fast")
|
| 74 |
+
strength = float(payload.get("strength", 0.7))
|
| 75 |
+
|
| 76 |
+
if not image_value or not mask_value:
|
| 77 |
+
bad_request("Both 'image' and 'mask' are required.")
|
| 78 |
+
|
| 79 |
+
print(f"[HARMONIZE] Request: mode={mode}, strength={strength}, "
|
| 80 |
+
f"image_len={len(image_value) if image_value else 0}, "
|
| 81 |
+
f"mask_len={len(mask_value) if mask_value else 0}", flush=True)
|
| 82 |
+
|
| 83 |
+
composite = await asyncio.to_thread(_decode_image, image_value)
|
| 84 |
+
mask = await asyncio.to_thread(_decode_mask, mask_value)
|
| 85 |
+
original = await asyncio.to_thread(_decode_image, original_value) if original_value else None
|
| 86 |
+
|
| 87 |
+
print(f"[HARMONIZE] Decoded: composite={composite.shape}, mask={mask.shape}", flush=True)
|
| 88 |
+
|
| 89 |
+
# Ensure mask matches composite dimensions
|
| 90 |
+
if mask.shape[:2] != composite.shape[:2]:
|
| 91 |
+
import cv2
|
| 92 |
+
mask = cv2.resize(mask, (composite.shape[1], composite.shape[0]), interpolation=cv2.INTER_NEAREST)
|
| 93 |
+
|
| 94 |
+
if mode == "generate":
|
| 95 |
+
result = await asyncio.to_thread(
|
| 96 |
+
_run_diffusion_inpaint, composite, mask, original, strength
|
| 97 |
+
)
|
| 98 |
+
else:
|
| 99 |
+
from services.harmonization.harmonizer import harmonize_composite
|
| 100 |
+
result = await asyncio.to_thread(
|
| 101 |
+
harmonize_composite, composite, mask, original, strength, mode
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
print(f"[HARMONIZE] Done: mode={result['mode']}, elapsed={result['elapsed_ms']}ms", flush=True)
|
| 105 |
+
|
| 106 |
+
return {
|
| 107 |
+
"image": _encode_image(result["image"]),
|
| 108 |
+
"mode": result["mode"],
|
| 109 |
+
"elapsed_ms": result["elapsed_ms"],
|
| 110 |
+
}
|
| 111 |
+
except HTTPException:
|
| 112 |
+
raise
|
| 113 |
+
except Exception as exc:
|
| 114 |
+
print(f"[HARMONIZE] ERROR: {exc}", flush=True)
|
| 115 |
+
traceback.print_exc()
|
| 116 |
+
raise HTTPException(status_code=500, detail=f"Harmonization failed: {exc}")
|
| 117 |
|
| 118 |
|
| 119 |
def _run_diffusion_inpaint(
|