Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,86 +2,104 @@ import gradio as gr
|
|
2 |
import subprocess
|
3 |
import os
|
4 |
import spaces
|
|
|
5 |
from pathlib import Path
|
6 |
|
7 |
-
|
8 |
-
def
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
subprocess.run(["git", "clone", "https://github.com/princeton-vl/infinigen.git"], check=True)
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
64 |
command = [
|
65 |
-
blender_python, "-m", "infinigen.datagen.manage_jobs",
|
66 |
-
"--output_folder", str(output_dir),
|
67 |
"--num_scenes", "1",
|
68 |
-
"--specific_seed", str(int(seed))
|
69 |
-
|
70 |
-
"infinigen/infinigen_examples/configs/simple.gin",
|
71 |
-
"--pipeline_configs", "infinigen/infinigen_examples/configs/local_16GB.gin",
|
72 |
-
"infinigen/infinigen_examples/configs/monocular.gin",
|
73 |
-
"infinigen/infinigen_examples/configs/blender_gt.gin"
|
74 |
-
]
|
75 |
-
result = subprocess.run(command, capture_output=True, text=True, check=True)
|
76 |
-
print(f"STDOUT: {result.stdout}")
|
77 |
-
print(f"STDERR: {result.stderr}")
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
# Gradio-Oberfläche
|
87 |
with gr.Blocks(title="Infinigen Demo") as demo:
|
@@ -90,6 +108,6 @@ with gr.Blocks(title="Infinigen Demo") as demo:
|
|
90 |
output_image = gr.Image(label="Generierte Szene")
|
91 |
generate_button = gr.Button("Szene generieren")
|
92 |
|
93 |
-
generate_button.click(fn=generate_scene, inputs=[seed_input], outputs=[output_image])
|
94 |
|
95 |
demo.launch()
|
|
|
2 |
import subprocess
|
3 |
import os
|
4 |
import spaces
|
5 |
+
import glob
|
6 |
from pathlib import Path
|
7 |
|
8 |
+
class InfinigenManager:
|
9 |
+
def __init__(self, base_dir="infinigen", output_dir="outputs"):
|
10 |
+
self.base_dir = Path(base_dir)
|
11 |
+
self.output_dir = Path(output_dir)
|
12 |
+
self.blender_bin = None
|
13 |
+
self.blender_python = None
|
14 |
+
self._setup_blender()
|
15 |
+
self._setup_infinigen()
|
16 |
+
|
17 |
+
def _setup_blender(self):
|
18 |
+
"""Stellt sicher, dass die neueste Blender-Version installiert ist."""
|
19 |
+
try:
|
20 |
+
# Prüfe vorhandene Blender-Version
|
21 |
+
blender_bins = glob.glob("/usr/bin/blender")
|
22 |
+
if blender_bins:
|
23 |
+
self.blender_bin = blender_bins[0]
|
24 |
+
version_result = subprocess.run([self.blender_bin, "-v"], capture_output=True, text=True)
|
25 |
+
blender_version = next((line.split()[1] for line in version_result.stdout.split("\n") if "Blender" in line and "." in line), None)
|
26 |
+
print(f"Vorhandene Blender-Version: {blender_version or 'unbekannt'}")
|
27 |
+
else:
|
28 |
+
raise FileNotFoundError("Blender nicht vorhanden")
|
29 |
+
|
30 |
+
# Finde Blender-Python
|
31 |
+
base_path = "/usr/share/blender"
|
32 |
+
python_patterns = [
|
33 |
+
f"{base_path}/{blender_version}/python/bin/python*",
|
34 |
+
f"{base_path}/*/python/bin/python*",
|
35 |
+
"/usr/lib/blender/*/python/bin/python*"
|
36 |
+
]
|
37 |
+
for pattern in python_patterns:
|
38 |
+
python_bins = glob.glob(pattern, recursive=True)
|
39 |
+
if python_bins:
|
40 |
+
self.blender_python = python_bins[0]
|
41 |
+
print(f"Blender Python gefunden: {self.blender_python}")
|
42 |
+
return
|
43 |
+
|
44 |
+
raise RuntimeError("Blender Python-Interpreter nicht gefunden!")
|
45 |
+
except Exception as e:
|
46 |
+
print(f"Fehler bei Blender-Setup: {e}")
|
47 |
+
raise
|
48 |
|
49 |
+
def _setup_infinigen(self):
|
50 |
+
"""Installiert Infinigen, falls nicht vorhanden."""
|
51 |
+
if not self.base_dir.exists():
|
52 |
+
print("Klone Infinigen...")
|
53 |
+
subprocess.run(["git", "clone", "https://github.com/princeton-vl/infinigen.git"], check=True)
|
|
|
54 |
|
55 |
+
if not self._is_infinigen_installed():
|
56 |
+
print("Installiere Infinigen...")
|
57 |
+
subprocess.run([
|
58 |
+
self.blender_python, "-m", "pip", "install", "-e", str(self.base_dir) + "[terrain,vis]", "--no-deps", "--user"
|
59 |
+
], check=True)
|
60 |
+
print("Infinigen installiert.")
|
61 |
|
62 |
+
def _is_infinigen_installed(self):
|
63 |
+
"""Prüft, ob Infinigen bereits installiert ist."""
|
64 |
+
try:
|
65 |
+
subprocess.run([self.blender_python, "-c", "import infinigen"], check=True, capture_output=True)
|
66 |
+
return True
|
67 |
+
except subprocess.CalledProcessError:
|
68 |
+
return False
|
69 |
|
70 |
+
@spaces.GPU
|
71 |
+
def generate_scene(self, seed, configs=None, pipeline_configs=None):
|
72 |
+
"""Generiert eine Szene mit Infinigen."""
|
73 |
+
self.output_dir.mkdir(exist_ok=True)
|
74 |
+
|
75 |
+
configs = configs or ["infinigen_examples/configs/desert.gin", "infinigen_examples/configs/simple.gin"]
|
76 |
+
pipeline_configs = pipeline_configs or [
|
77 |
+
"infinigen_examples/configs/local_16GB.gin",
|
78 |
+
"infinigen_examples/configs/monocular.gin",
|
79 |
+
"infinigen_examples/configs/blender_gt.gin"
|
80 |
+
]
|
81 |
+
|
82 |
command = [
|
83 |
+
self.blender_python, "-m", "infinigen.datagen.manage_jobs",
|
84 |
+
"--output_folder", str(self.output_dir),
|
85 |
"--num_scenes", "1",
|
86 |
+
"--specific_seed", str(int(seed))
|
87 |
+
] + ["--configs"] + configs + ["--pipeline_configs"] + pipeline_configs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
+
try:
|
90 |
+
result = subprocess.run(command, capture_output=True, text=True, check=True)
|
91 |
+
print(f"STDOUT: {result.stdout}")
|
92 |
+
print(f"STDERR: {result.stderr}")
|
93 |
+
output_path = self.output_dir / "0000000000.png"
|
94 |
+
return str(output_path) if output_path.exists() else f"Fehler: Bild nicht gefunden. STDERR: {result.stderr}"
|
95 |
+
except subprocess.CalledProcessError as e:
|
96 |
+
return f"Fehler: {e.stderr}"
|
97 |
+
|
98 |
+
# Manager initialisieren
|
99 |
+
try:
|
100 |
+
manager = InfinigenManager()
|
101 |
+
except Exception as e:
|
102 |
+
print(f"Fehler bei der Initialisierung: {e}")
|
103 |
|
104 |
# Gradio-Oberfläche
|
105 |
with gr.Blocks(title="Infinigen Demo") as demo:
|
|
|
108 |
output_image = gr.Image(label="Generierte Szene")
|
109 |
generate_button = gr.Button("Szene generieren")
|
110 |
|
111 |
+
generate_button.click(fn=manager.generate_scene, inputs=[seed_input], outputs=[output_image])
|
112 |
|
113 |
demo.launch()
|