Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from pathlib import Path
|
|
| 8 |
import zipfile
|
| 9 |
import shutil
|
| 10 |
|
| 11 |
-
print("AAA INPAINT + DEPTH
|
| 12 |
|
| 13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
|
|
@@ -19,7 +19,7 @@ pipe = pipeline(
|
|
| 19 |
)
|
| 20 |
|
| 21 |
# =========================
|
| 22 |
-
#
|
| 23 |
# =========================
|
| 24 |
def normalize_depth(depth):
|
| 25 |
d = np.array(depth).astype(np.float32)
|
|
@@ -27,59 +27,86 @@ def normalize_depth(depth):
|
|
| 27 |
return d
|
| 28 |
|
| 29 |
# =========================
|
| 30 |
-
#
|
| 31 |
# =========================
|
| 32 |
-
def
|
| 33 |
-
|
| 34 |
-
mask = np.array(mask_pil.convert("L"))
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
mask =
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
mask = cv2.dilate(mask, kernel, iterations=1)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
img_cv = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
blended =
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# =========================
|
| 60 |
# PROCESS
|
| 61 |
# =========================
|
| 62 |
-
def process(
|
| 63 |
-
if not
|
| 64 |
return None
|
| 65 |
|
| 66 |
-
out = Path("
|
| 67 |
if out.exists():
|
| 68 |
shutil.rmtree(out)
|
| 69 |
out.mkdir()
|
| 70 |
|
| 71 |
-
zip_path = "
|
| 72 |
|
| 73 |
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 74 |
-
for
|
| 75 |
|
| 76 |
-
name = Path(
|
| 77 |
|
| 78 |
-
img = Image.open(
|
| 79 |
-
mask = Image.open(mask_file.name)
|
| 80 |
|
| 81 |
-
# 💥 INPAINT
|
| 82 |
-
clean =
|
| 83 |
|
| 84 |
# 💥 DEPTH
|
| 85 |
depth_raw = pipe(clean)["depth"]
|
|
@@ -97,21 +124,19 @@ def process(images, masks):
|
|
| 97 |
zipf.write(img_path, img_path.name)
|
| 98 |
zipf.write(depth_path, depth_path.name)
|
| 99 |
|
| 100 |
-
print("✅
|
| 101 |
return zip_path
|
| 102 |
|
| 103 |
# =========================
|
| 104 |
# UI
|
| 105 |
# =========================
|
| 106 |
with gr.Blocks() as demo:
|
| 107 |
-
gr.Markdown("# 🧠 AAA INPAINT + DEPTH
|
| 108 |
-
|
| 109 |
-
inp_img = gr.File(file_count="multiple", label="IMAGENS (BG com buraco)")
|
| 110 |
-
inp_mask = gr.File(file_count="multiple", label="MÁSCARAS (buraco branco)")
|
| 111 |
|
|
|
|
| 112 |
out = gr.File()
|
| 113 |
|
| 114 |
-
btn = gr.Button("
|
| 115 |
-
btn.click(fn=process, inputs=
|
| 116 |
|
| 117 |
demo.launch()
|
|
|
|
| 8 |
import zipfile
|
| 9 |
import shutil
|
| 10 |
|
| 11 |
+
print("AAA CINEMATIC INPAINT + DEPTH")
|
| 12 |
|
| 13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
# =========================
|
| 22 |
+
# DEPTH NORMALIZATION
|
| 23 |
# =========================
|
| 24 |
def normalize_depth(depth):
|
| 25 |
d = np.array(depth).astype(np.float32)
|
|
|
|
| 27 |
return d
|
| 28 |
|
| 29 |
# =========================
|
| 30 |
+
# MASK INTELIGENTE
|
| 31 |
# =========================
|
| 32 |
+
def create_smart_mask(img):
|
| 33 |
+
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
|
|
|
| 34 |
|
| 35 |
+
# detecta preto
|
| 36 |
+
mask = cv2.threshold(gray, 12, 255, cv2.THRESH_BINARY_INV)[1]
|
| 37 |
|
| 38 |
+
# remove ruído pequeno
|
| 39 |
+
mask = cv2.medianBlur(mask, 5)
|
|
|
|
| 40 |
|
| 41 |
+
# expande borda (anti halo)
|
| 42 |
+
kernel = np.ones((7,7), np.uint8)
|
| 43 |
+
mask = cv2.dilate(mask, kernel, iterations=2)
|
| 44 |
+
|
| 45 |
+
return mask
|
| 46 |
+
|
| 47 |
+
# =========================
|
| 48 |
+
# EDGE MAP (GUIDE)
|
| 49 |
+
# =========================
|
| 50 |
+
def edge_map(img):
|
| 51 |
+
edges = cv2.Canny(img, 50, 150)
|
| 52 |
+
edges = cv2.GaussianBlur(edges, (5,5), 0)
|
| 53 |
+
return edges
|
| 54 |
+
|
| 55 |
+
# =========================
|
| 56 |
+
# INPAINT AAA MULTI PASS
|
| 57 |
+
# =========================
|
| 58 |
+
def inpaint_master(img_pil):
|
| 59 |
+
img = np.array(img_pil)
|
| 60 |
img_cv = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 61 |
|
| 62 |
+
mask = create_smart_mask(img)
|
| 63 |
+
|
| 64 |
+
# PASSO 1 - ESTRUTURA GLOBAL
|
| 65 |
+
ns = cv2.inpaint(img_cv, mask, 5, cv2.INPAINT_NS)
|
| 66 |
+
|
| 67 |
+
# PASSO 2 - DETALHE FINO
|
| 68 |
+
telea = cv2.inpaint(ns, mask, 3, cv2.INPAINT_TELEA)
|
| 69 |
|
| 70 |
+
# PASSO 3 - EDGE AWARE BLEND
|
| 71 |
+
edges = edge_map(img_cv)
|
| 72 |
+
edges = edges.astype(np.float32) / 255.0
|
| 73 |
+
edges = np.expand_dims(edges, axis=2)
|
| 74 |
|
| 75 |
+
blended = telea * (1 - edges) + ns * edges
|
| 76 |
+
blended = blended.astype(np.uint8)
|
| 77 |
|
| 78 |
+
# PASSO 4 - ANTI-SEAM (CRÍTICO)
|
| 79 |
+
blurred = cv2.GaussianBlur(blended, (7,7), 0)
|
| 80 |
+
final = cv2.addWeighted(blended, 0.85, blurred, 0.15, 0)
|
| 81 |
|
| 82 |
+
# PASSO 5 - REFINO FINAL
|
| 83 |
+
final = cv2.bilateralFilter(final, 9, 75, 75)
|
| 84 |
+
|
| 85 |
+
return Image.fromarray(cv2.cvtColor(final, cv2.COLOR_BGR2RGB))
|
| 86 |
|
| 87 |
# =========================
|
| 88 |
# PROCESS
|
| 89 |
# =========================
|
| 90 |
+
def process(files):
|
| 91 |
+
if not files:
|
| 92 |
return None
|
| 93 |
|
| 94 |
+
out = Path("AAA_MASTER_OUTPUT")
|
| 95 |
if out.exists():
|
| 96 |
shutil.rmtree(out)
|
| 97 |
out.mkdir()
|
| 98 |
|
| 99 |
+
zip_path = "AAA_MASTER_RESULT.zip"
|
| 100 |
|
| 101 |
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 102 |
+
for file in files:
|
| 103 |
|
| 104 |
+
name = Path(file.name).stem
|
| 105 |
|
| 106 |
+
img = Image.open(file.name).convert("RGB")
|
|
|
|
| 107 |
|
| 108 |
+
# 💥 INPAINT MASTER
|
| 109 |
+
clean = inpaint_master(img)
|
| 110 |
|
| 111 |
# 💥 DEPTH
|
| 112 |
depth_raw = pipe(clean)["depth"]
|
|
|
|
| 124 |
zipf.write(img_path, img_path.name)
|
| 125 |
zipf.write(depth_path, depth_path.name)
|
| 126 |
|
| 127 |
+
print("✅ AAA MASTER FINALIZADO")
|
| 128 |
return zip_path
|
| 129 |
|
| 130 |
# =========================
|
| 131 |
# UI
|
| 132 |
# =========================
|
| 133 |
with gr.Blocks() as demo:
|
| 134 |
+
gr.Markdown("# 🧠 AAA MASTER (INPAINT + DEPTH FINAL)")
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
+
inp = gr.File(file_count="multiple", label="BG com buraco preto")
|
| 137 |
out = gr.File()
|
| 138 |
|
| 139 |
+
btn = gr.Button("GERAR AAA MASTER")
|
| 140 |
+
btn.click(fn=process, inputs=inp, outputs=out)
|
| 141 |
|
| 142 |
demo.launch()
|