Merlimhhs commited on
Commit
08ba32c
·
verified ·
1 Parent(s): 6622e48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -19
app.py CHANGED
@@ -1,44 +1,104 @@
1
  import gradio as gr
2
- import torch
3
  import numpy as np
4
  from PIL import Image, ImageFilter, ImageOps
5
  from transformers import pipeline
6
  from pathlib import Path
7
  import zipfile
8
  import shutil
 
 
9
 
10
- print("DEPTH AAA ENGINE (PURE MODE)")
11
 
12
- device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
 
13
 
14
  pipe = pipeline(
15
- task="depth-estimation",
16
- model="depth-anything/Depth-Anything-V2-Base-hf",
17
- device=device
18
  )
19
 
20
- #========================= NORMALIZAÇÃO PROFISSIONAL =========================
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- def normalize_depth(depth):
23
- d = np.array(depth).astype(np.float32)
24
 
25
- # normalização estável d = (d - d.min()) / (d.max() - d.min() + 1e-6) return d ========================= REFINO (SEM QUEBRAR O MAPA) =========================
 
26
 
27
- def refine_depth(depth_np):
28
- img = Image.fromarray((depth_np * 255).astype(np.uint8))
29
 
30
- # 🔥 suavização leve (ANTI-BANDING) img = img.filter(ImageFilter.GaussianBlur(1.0)) # 🔥 contraste leve (mantém detalhe) img = ImageOps.autocontrast(img, cutoff=0.3) return img ========================= PIPELINE =========================
 
 
 
 
31
 
32
  def process(files):
33
- if not files:
34
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- out = Path("depth_clean_output") if out.exists(): shutil.rmtree(out) out.mkdir() zip_path = "DEPTH_AAA_ONLY.zip" with zipfile.ZipFile(zip_path, 'w') as zipf: for file in files: name = Path(file.name).stem img = Image.open(file.name).convert("RGB") # 💥 DEPTH DIRETO DO BG (VOCÊ JÁ VAI FAZER ISSO CERTO) depth_raw = pipe(img)["depth"] # normalização real depth_np = normalize_depth(depth_raw) # refinamento seguro depth_final = refine_depth(depth_np) # salvar path = out / f"depth_{name}.png" depth_final.save(path) zipf.write(path, path.name) print("✅ DEPTH PERFEITO GERADO") return zip_path ========================= UI =========================
 
 
37
 
 
 
 
38
  with gr.Blocks() as demo:
39
- gr.Markdown("# 🧠 DEPTH AAA (PARALLAX READY)")
40
 
41
- inp = gr.File(file_count="multiple") out = gr.File() btn = gr.Button("GERAR DEPTH") btn.click(fn=process, inputs=inp, outputs=out)
 
 
42
 
43
- demo.launch()
 
44
 
 
 
1
  import gradio as gr
 
2
  import numpy as np
3
  from PIL import Image, ImageFilter, ImageOps
4
  from transformers import pipeline
5
  from pathlib import Path
6
  import zipfile
7
  import shutil
8
+ import os
9
+ import cv2
10
 
11
+ print("DEPTH AAA ENGINE (CLEAN REBUILD)")
12
 
13
+ # -------------------------
14
+ # CONFIG
15
+ # -------------------------
16
+ MODEL_ID = "depth-anything/Depth-Anything-V2-Base-hf"
17
+
18
+ # Use cuda if available, otherwise cpu
19
+ device = 0 if os.environ.get("CUDA_VISIBLE_DEVICES", "") != "" else -1
20
 
21
  pipe = pipeline(
22
+ task="depth-estimation",
23
+ model=MODEL_ID,
24
+ device=device
25
  )
26
 
27
+ # -------------------------
28
+ # HELPERS
29
+ # -------------------------
30
+ def normalize_depth(depth_img: Image.Image) -> np.ndarray:
31
+ d = np.array(depth_img).astype(np.float32)
32
+ d_min = d.min()
33
+ d_max = d.max()
34
+ d = (d - d_min) / (d_max - d_min + 1e-6)
35
+ return d
36
+
37
+ def refine_depth(depth_np: np.ndarray) -> Image.Image:
38
+ img = Image.fromarray((depth_np * 255).astype(np.uint8))
39
 
40
+ # leve blur para suavizar banding
41
+ img = img.filter(ImageFilter.GaussianBlur(1.0))
42
 
43
+ # melhora contraste sem destruir detalhe
44
+ img = ImageOps.autocontrast(img, cutoff=0.3)
45
 
46
+ return img
 
47
 
48
+ def colorize_depth(depth_gray: Image.Image) -> Image.Image:
49
+ arr = np.array(depth_gray)
50
+ color = cv2.applyColorMap(arr, cv2.COLORMAP_INFERNO)
51
+ color = cv2.cvtColor(color, cv2.COLOR_BGR2RGB)
52
+ return Image.fromarray(color)
53
 
54
  def process(files):
55
+ if not files:
56
+ return None, None
57
+
58
+ out_dir = Path("depth_output")
59
+ if out_dir.exists():
60
+ shutil.rmtree(out_dir)
61
+ out_dir.mkdir(parents=True, exist_ok=True)
62
+
63
+ zip_path = "DEPTH_RESULT.zip"
64
+
65
+ preview_items = []
66
+
67
+ with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
68
+ for item in files:
69
+ path = item if isinstance(item, str) else getattr(item, "name", item)
70
+ stem = Path(path).stem
71
+
72
+ img = Image.open(path).convert("RGB")
73
+
74
+ # depth-estimation pipeline returns a dict with depth image
75
+ result = pipe(img)
76
+ depth_raw = result["depth"]
77
+
78
+ depth_np = normalize_depth(depth_raw)
79
+ depth_final = refine_depth(depth_np)
80
+
81
+ out_path = out_dir / f"{stem}_depth.png"
82
+ depth_final.save(out_path)
83
+ zipf.write(out_path, out_path.name)
84
+
85
+ preview_items.append((str(out_path), f"{stem}_depth"))
86
 
87
+ # show first result as preview if available
88
+ preview = preview_items[0][0] if preview_items else None
89
+ return zip_path, preview
90
 
91
+ # -------------------------
92
+ # UI
93
+ # -------------------------
94
  with gr.Blocks() as demo:
95
+ gr.Markdown("# 🧠 DEPTH AAA (CLEAN REBUILD)")
96
 
97
+ inp = gr.File(file_count="multiple", type="filepath")
98
+ out_zip = gr.File(label="ZIP")
99
+ preview = gr.Image(label="Preview", type="filepath")
100
 
101
+ btn = gr.Button("GERAR DEPTH")
102
+ btn.click(fn=process, inputs=inp, outputs=[out_zip, preview])
103
 
104
+ demo.launch()