Commit
·
fc3f561
1
Parent(s):
2eaee03
feat(api): robust painted mask detection; fallback to dark-threshold when diff empty; add logs
Browse files- api/main.py +13 -0
api/main.py
CHANGED
|
@@ -11,6 +11,10 @@ from fastapi.responses import FileResponse, JSONResponse
|
|
| 11 |
from pydantic import BaseModel
|
| 12 |
from PIL import Image
|
| 13 |
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
from src.core import process_inpaint
|
| 16 |
|
|
@@ -198,6 +202,15 @@ def inpaint_multipart(
|
|
| 198 |
_, binmask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 199 |
except Exception:
|
| 200 |
_, binmask = cv2.threshold(gray, 40, 255, cv2.THRESH_BINARY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
# Build RGBA mask where selected area has alpha=0
|
| 202 |
mask_rgba = np.zeros((binmask.shape[0], binmask.shape[1], 4), dtype=np.uint8)
|
| 203 |
mask_rgba[:, :, 3] = np.where(binmask > 0, 0, 255).astype(np.uint8)
|
|
|
|
| 11 |
from pydantic import BaseModel
|
| 12 |
from PIL import Image
|
| 13 |
import cv2
|
| 14 |
+
import logging
|
| 15 |
+
|
| 16 |
+
logging.basicConfig(level=logging.INFO)
|
| 17 |
+
log = logging.getLogger("api")
|
| 18 |
|
| 19 |
from src.core import process_inpaint
|
| 20 |
|
|
|
|
| 202 |
_, binmask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 203 |
except Exception:
|
| 204 |
_, binmask = cv2.threshold(gray, 40, 255, cv2.THRESH_BINARY)
|
| 205 |
+
nonzero = int((binmask > 0).sum())
|
| 206 |
+
log.info("painted-mask via diff, pixels: %d", nonzero)
|
| 207 |
+
# If nothing detected (user uploaded plain original as mask), detect dark strokes directly
|
| 208 |
+
if nonzero < 50:
|
| 209 |
+
gray_painted = cv2.cvtColor(m_rgb, cv2.COLOR_RGB2GRAY)
|
| 210 |
+
# pick very dark paint
|
| 211 |
+
_, binmask = cv2.threshold(gray_painted, 60, 255, cv2.THRESH_BINARY_INV)
|
| 212 |
+
nonzero = int((binmask > 0).sum())
|
| 213 |
+
log.info("painted-mask via dark-thresh, pixels: %d", nonzero)
|
| 214 |
# Build RGBA mask where selected area has alpha=0
|
| 215 |
mask_rgba = np.zeros((binmask.shape[0], binmask.shape[1], 4), dtype=np.uint8)
|
| 216 |
mask_rgba[:, :, 3] = np.where(binmask > 0, 0, 255).astype(np.uint8)
|