Spaces:
Sleeping
Sleeping
redesigned the home page and accounts page
Browse files- config.py +1 -0
- inpaint_lama.py +29 -0
- pipeline.py +11 -7
config.py
CHANGED
|
@@ -8,6 +8,7 @@ BACKEND_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
| 8 |
UPLOAD_DIR = os.path.join(BACKEND_DIR, os.getenv("UPLOAD_DIR", "uploads"))
|
| 9 |
OUTPUT_DIR = os.path.join(BACKEND_DIR, os.getenv("OUTPUT_DIR", "outputs"))
|
| 10 |
MAX_FILE_SIZE_MB = int(os.getenv("MAX_FILE_SIZE_MB", 20))
|
|
|
|
| 11 |
|
| 12 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 13 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
|
|
|
| 8 |
UPLOAD_DIR = os.path.join(BACKEND_DIR, os.getenv("UPLOAD_DIR", "uploads"))
|
| 9 |
OUTPUT_DIR = os.path.join(BACKEND_DIR, os.getenv("OUTPUT_DIR", "outputs"))
|
| 10 |
MAX_FILE_SIZE_MB = int(os.getenv("MAX_FILE_SIZE_MB", 20))
|
| 11 |
+
INPAINT_BACKGROUND_METHOD = os.getenv("INPAINT_BACKGROUND_METHOD", "telea")
|
| 12 |
|
| 13 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 14 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
inpaint_lama.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
_lama_instance = None
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def get_lama_model():
|
| 10 |
+
global _lama_instance
|
| 11 |
+
if _lama_instance is None:
|
| 12 |
+
from simple_lama_inpainting import SimpleLama
|
| 13 |
+
_lama_instance = SimpleLama()
|
| 14 |
+
return _lama_instance
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def lama_reconstruct_background(image_bgr: np.ndarray, combined_mask: np.ndarray) -> np.ndarray:
|
| 18 |
+
if torch.cuda.is_available():
|
| 19 |
+
torch.cuda.empty_cache()
|
| 20 |
+
|
| 21 |
+
image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
|
| 22 |
+
pil_image = Image.fromarray(image_rgb)
|
| 23 |
+
pil_mask = Image.fromarray(combined_mask)
|
| 24 |
+
|
| 25 |
+
model = get_lama_model()
|
| 26 |
+
result = model(pil_image, pil_mask)
|
| 27 |
+
|
| 28 |
+
result_rgb = np.array(result)
|
| 29 |
+
return cv2.cvtColor(result_rgb, cv2.COLOR_RGB2BGR)
|
pipeline.py
CHANGED
|
@@ -8,6 +8,7 @@ import numpy as np
|
|
| 8 |
from PIL import Image
|
| 9 |
|
| 10 |
from layer_extractor import build_layers
|
|
|
|
| 11 |
|
| 12 |
BACKEND_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 13 |
|
|
@@ -28,7 +29,6 @@ def reconstruct_background(image_path: str, layers: list) -> str:
|
|
| 28 |
h, w = image_bgr.shape[:2]
|
| 29 |
|
| 30 |
combined_mask = np.zeros((h, w), dtype=np.uint8)
|
| 31 |
-
|
| 32 |
for layer in layers:
|
| 33 |
x = max(0, layer["x"])
|
| 34 |
y = max(0, layer["y"])
|
|
@@ -36,12 +36,16 @@ def reconstruct_background(image_path: str, layers: list) -> str:
|
|
| 36 |
y2 = min(h, layer["y"] + layer["h"])
|
| 37 |
combined_mask[y:y2, x:x2] = 255
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
_, buffer = cv2.imencode('.jpg', clean_bg, [cv2.IMWRITE_JPEG_QUALITY, 95])
|
| 47 |
return base64.b64encode(buffer).decode("utf-8")
|
|
|
|
| 8 |
from PIL import Image
|
| 9 |
|
| 10 |
from layer_extractor import build_layers
|
| 11 |
+
from config import INPAINT_BACKGROUND_METHOD
|
| 12 |
|
| 13 |
BACKEND_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 14 |
|
|
|
|
| 29 |
h, w = image_bgr.shape[:2]
|
| 30 |
|
| 31 |
combined_mask = np.zeros((h, w), dtype=np.uint8)
|
|
|
|
| 32 |
for layer in layers:
|
| 33 |
x = max(0, layer["x"])
|
| 34 |
y = max(0, layer["y"])
|
|
|
|
| 36 |
y2 = min(h, layer["y"] + layer["h"])
|
| 37 |
combined_mask[y:y2, x:x2] = 255
|
| 38 |
|
| 39 |
+
if INPAINT_BACKGROUND_METHOD == "lama":
|
| 40 |
+
try:
|
| 41 |
+
from inpaint_lama import lama_reconstruct_background
|
| 42 |
+
print("[PIPELINE] Using LaMa for background reconstruction")
|
| 43 |
+
clean_bg = lama_reconstruct_background(image_bgr, combined_mask)
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"[PIPELINE] LaMa failed ({e}), falling back to TELEA")
|
| 46 |
+
clean_bg = cv2.inpaint(image_bgr, combined_mask, inpaintRadius=25, flags=cv2.INPAINT_TELEA)
|
| 47 |
+
else:
|
| 48 |
+
clean_bg = cv2.inpaint(image_bgr, combined_mask, inpaintRadius=25, flags=cv2.INPAINT_TELEA)
|
| 49 |
|
| 50 |
_, buffer = cv2.imencode('.jpg', clean_bg, [cv2.IMWRITE_JPEG_QUALITY, 95])
|
| 51 |
return base64.b64encode(buffer).decode("utf-8")
|