Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,56 @@
|
|
1 |
"""
|
2 |
-
GLB μ λλ©μ΄μ
μμ±κΈ°
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
3. μμΈ μ²λ¦¬Β·μ£Όμ μΌλΆ 보κ°
|
9 |
"""
|
10 |
|
11 |
-
|
|
|
12 |
import numpy as np
|
13 |
-
import trimesh
|
14 |
from PIL import Image
|
15 |
-
import trimesh.transformations as tf
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
import gradio as gr
|
20 |
import spaces
|
21 |
|
22 |
-
# κ²°κ³Ό μ μ₯ λλ ν°λ¦¬
|
23 |
LOG_PATH = "./results/demo"
|
24 |
os.makedirs(LOG_PATH, exist_ok=True)
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
def create_model_animation_gif(
|
31 |
output_path: str,
|
32 |
input_glb_path: str,
|
@@ -35,20 +59,18 @@ def create_model_animation_gif(
|
|
35 |
fps: int = 30,
|
36 |
resolution=(640, 480),
|
37 |
):
|
38 |
-
"""
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
frames, num_frames = [], min(int(duration * fps), 60)
|
47 |
for i in range(num_frames):
|
48 |
t = i / (num_frames - 1)
|
|
|
49 |
|
50 |
-
#
|
51 |
-
scene_i = scene.copy()
|
52 |
if animation_type == "rotate":
|
53 |
M = tf.rotation_matrix(2 * math.pi * t, [0, 1, 0])
|
54 |
elif animation_type == "float":
|
@@ -63,160 +85,124 @@ def create_model_animation_gif(
|
|
63 |
M = tf.rotation_matrix(math.pi / 6 * math.sin(2 * math.pi * t), [0, 0, 1])
|
64 |
else:
|
65 |
M = np.eye(4)
|
|
|
66 |
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
# (3) μ€νμ€ν¬λ¦° λ λ β PNG λ°μ΄νΈ
|
70 |
-
png_bytes = scene_i.save_image(resolution=resolution, visible=True)
|
71 |
-
frame = Image.open(io.BytesIO(png_bytes)).convert("RGB")
|
72 |
frames.append(frame)
|
73 |
|
74 |
-
# (4) GIF μ μ₯
|
75 |
frames[0].save(
|
76 |
output_path,
|
77 |
save_all=True,
|
78 |
append_images=frames[1:],
|
79 |
-
optimize=False,
|
80 |
duration=int(1000 / fps),
|
81 |
loop=0,
|
82 |
)
|
83 |
-
print(
|
84 |
return output_path
|
85 |
|
86 |
-
|
87 |
-
# ------------------------------------------------------------------
|
88 |
-
# 2) GLB νμΌμ λ¨μ λ³ν μ μ©ν΄ μ μ₯
|
89 |
-
# ------------------------------------------------------------------
|
90 |
def modify_glb_file(input_glb_path, output_glb_path, animation_type="rotate"):
|
91 |
-
"""
|
92 |
-
μ
λ‘λλ GLB νμΌμ λ¨μΌ λ³ν(νμ Β·μ΄λΒ·μ€μΌμΌ)μ μ μ©ν΄ μ GLBλ‘ μ μ₯
|
93 |
-
"""
|
94 |
try:
|
95 |
-
|
96 |
-
if not isinstance(
|
97 |
-
|
98 |
|
99 |
-
# λ³ν λ§€νΈλ¦μ€ κ²°μ
|
100 |
if animation_type == "rotate":
|
101 |
-
|
102 |
elif animation_type == "float":
|
103 |
-
|
104 |
elif animation_type == "pulse":
|
105 |
-
|
106 |
elif animation_type == "explode":
|
107 |
-
|
108 |
elif animation_type == "assemble":
|
109 |
-
|
110 |
elif animation_type == "swing":
|
111 |
-
|
112 |
else:
|
113 |
-
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
print(f"Exported modified GLB to {output_glb_path}")
|
118 |
return output_glb_path
|
119 |
-
|
120 |
except Exception as e:
|
121 |
-
print("
|
122 |
-
# μ€ν¨ μ μλ³Έ 볡μ¬
|
123 |
-
import shutil
|
124 |
-
|
125 |
shutil.copy(input_glb_path, output_glb_path)
|
126 |
-
print(f"Copied original GLB to {output_glb_path}")
|
127 |
return output_glb_path
|
128 |
|
129 |
-
|
130 |
-
# ------------------------------------------------------------------
|
131 |
-
# 3) Gradioμμ νΈμΆν λ©μΈ νμ΄νλΌμΈ
|
132 |
-
# ------------------------------------------------------------------
|
133 |
@spaces.GPU
|
134 |
def process_3d_model(input_3d, animation_type, animation_duration, fps):
|
135 |
-
"""
|
136 |
-
β GLB λ³ν β β‘ GIF λ λ β β’ λ©νλ°μ΄ν° JSON μ μ₯
|
137 |
-
"""
|
138 |
try:
|
139 |
base = os.path.splitext(os.path.basename(input_3d))[0]
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
# 1) GLB λ³ν
|
145 |
-
modify_glb_file(input_3d, animated_glb, animation_type)
|
146 |
|
147 |
-
|
148 |
create_model_animation_gif(
|
149 |
-
|
150 |
-
input_3d,
|
151 |
-
animation_type,
|
152 |
-
animation_duration,
|
153 |
-
fps,
|
154 |
)
|
155 |
|
156 |
-
|
157 |
-
metadata = dict(
|
158 |
animation_type=animation_type,
|
159 |
duration=animation_duration,
|
160 |
fps=fps,
|
161 |
original_model=os.path.basename(input_3d),
|
162 |
created_at=time.strftime("%Y-%m-%d %H:%M:%S"),
|
163 |
)
|
164 |
-
with open(
|
165 |
-
json.dump(
|
166 |
|
167 |
-
return
|
168 |
|
169 |
except Exception as e:
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
return input_3d, fallback_gif, None
|
175 |
|
176 |
-
|
177 |
-
# ------------------------------------------------------------------
|
178 |
-
# 4) Gradio UI
|
179 |
-
# ------------------------------------------------------------------
|
180 |
with gr.Blocks(title="GLB μ λλ©μ΄μ
μμ±κΈ°") as demo:
|
181 |
gr.Markdown(
|
182 |
"""
|
183 |
<h2><b>GLB μ λλ©μ΄μ
μμ±κΈ° - 3D λͺ¨λΈ μμ§μ ν¨κ³Ό</b></h2>
|
184 |
-
|
185 |
"""
|
186 |
)
|
187 |
-
|
188 |
with gr.Row():
|
189 |
with gr.Column():
|
190 |
-
|
191 |
-
|
192 |
label="μ λλ©μ΄μ
μ ν",
|
193 |
choices=["rotate", "float", "explode", "assemble", "pulse", "swing"],
|
194 |
value="rotate",
|
195 |
)
|
196 |
-
|
197 |
-
|
198 |
-
)
|
199 |
-
fps = gr.Slider(label="FPS", minimum=15, maximum=60, value=30, step=1)
|
200 |
-
submit_btn = gr.Button("μ λλ©μ΄μ
μμ±")
|
201 |
-
|
202 |
with gr.Column():
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
|
207 |
-
|
208 |
fn=process_3d_model,
|
209 |
-
inputs=[
|
210 |
-
outputs=[
|
211 |
)
|
212 |
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
gr.Examples(examples=example_glbs, inputs=[input_3d])
|
217 |
|
218 |
-
# ------------------------------------------------------------------
|
219 |
-
# 5) μ± μ€ν
|
220 |
-
# ------------------------------------------------------------------
|
221 |
if __name__ == "__main__":
|
222 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
1 |
"""
|
2 |
+
GLB μ λλ©μ΄μ
μμ±κΈ°
|
3 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
4 |
+
μ
λ‘λν GLB 3D λͺ¨λΈμ νμ Β·λΆμ Β·νλ° λ± μ λλ©μ΄μ
μ μ μ©ν΄
|
5 |
+
β λ³νλ GLB β‘ μ€μ λͺ¨λΈμ λ λλ§ν GIF β’ λ©νλ°μ΄ν° JSON μ λλ €μ€λλ€.
|
6 |
+
β’ headless μλ² λμ: EGL + pyglet.headless + trimesh β μ€ν¨ μ pyrender ν΄λ°±
|
7 |
+
β’ μ΅λ 60 fpsΒ·60 νλ μλ‘ μ ν (λ°λͺ¨ λͺ©μ )
|
|
|
8 |
"""
|
9 |
|
10 |
+
# ββββββββββββββββββββ 1. κ³΅ν΅ λͺ¨λ ββββββββββββββββββββ
|
11 |
+
import os, io, time, glob, json, math, shutil
|
12 |
import numpy as np
|
|
|
13 |
from PIL import Image
|
|
|
14 |
|
15 |
+
import pyglet
|
16 |
+
pyglet.options["headless"] = True # λ°λμ trimesh μ μ!
|
17 |
+
os.environ["PYOPENGL_PLATFORM"] = "egl" # Off-screen GL
|
18 |
+
|
19 |
+
import trimesh
|
20 |
+
import trimesh.transformations as tf
|
21 |
|
22 |
import gradio as gr
|
23 |
import spaces
|
24 |
|
|
|
25 |
LOG_PATH = "./results/demo"
|
26 |
os.makedirs(LOG_PATH, exist_ok=True)
|
27 |
|
28 |
+
# ββββββββββββββββββββ 2. λ λ μ νΈ ββββββββββββββββββββ
|
29 |
+
def _render_with_trimesh(scene: trimesh.Scene, res):
|
30 |
+
png = scene.save_image(resolution=res, visible=True)
|
31 |
+
if png is None:
|
32 |
+
raise RuntimeError("trimesh.save_image returned None")
|
33 |
+
return Image.open(io.BytesIO(png)).convert("RGB")
|
34 |
+
|
35 |
+
def _render_with_pyrender(mesh_or_scene, res):
|
36 |
+
import pyrender # μ§μ° μν¬νΈ
|
37 |
+
if isinstance(mesh_or_scene, trimesh.Scene):
|
38 |
+
mesh = trimesh.util.concatenate(mesh_or_scene.dump())
|
39 |
+
else:
|
40 |
+
mesh = mesh_or_scene
|
41 |
+
mesh = pyrender.Mesh.from_trimesh(mesh, smooth=False)
|
42 |
+
scn = pyrender.Scene()
|
43 |
+
scn.add(mesh)
|
44 |
+
cam = pyrender.PerspectiveCamera(yfov=np.pi / 3)
|
45 |
+
scn.add(cam, pose=tf.translation_matrix([0, 0, 3]))
|
46 |
+
light = pyrender.DirectionalLight(intensity=3.0)
|
47 |
+
scn.add(light, pose=tf.translation_matrix([0, 5, 5]))
|
48 |
+
r = pyrender.OffscreenRenderer(*res)
|
49 |
+
color, _ = r.render(scn, flags=pyrender.RenderFlags.RGBA)
|
50 |
+
r.delete()
|
51 |
+
return Image.fromarray(color[..., :3])
|
52 |
+
|
53 |
+
# ββββββββββββββββββββ 3. GIF μμ± ββββββββββββββββββββ
|
54 |
def create_model_animation_gif(
|
55 |
output_path: str,
|
56 |
input_glb_path: str,
|
|
|
59 |
fps: int = 30,
|
60 |
resolution=(640, 480),
|
61 |
):
|
62 |
+
"""GLB λͺ¨λΈμ μ€μ λ λλ§νμ¬ μ λλ©μ΄μ
GIF μμ±"""
|
63 |
+
base = trimesh.load(input_glb_path)
|
64 |
+
if isinstance(base, trimesh.Trimesh):
|
65 |
+
base = trimesh.Scene(base)
|
66 |
+
|
67 |
+
num_frames = min(int(duration * fps), 60)
|
68 |
+
frames = []
|
|
|
|
|
69 |
for i in range(num_frames):
|
70 |
t = i / (num_frames - 1)
|
71 |
+
scene = base.copy()
|
72 |
|
73 |
+
# λ³ν νλ ¬
|
|
|
74 |
if animation_type == "rotate":
|
75 |
M = tf.rotation_matrix(2 * math.pi * t, [0, 1, 0])
|
76 |
elif animation_type == "float":
|
|
|
85 |
M = tf.rotation_matrix(math.pi / 6 * math.sin(2 * math.pi * t), [0, 0, 1])
|
86 |
else:
|
87 |
M = np.eye(4)
|
88 |
+
scene.apply_transform(M)
|
89 |
|
90 |
+
# β trimesh β β‘ pyrender ν΄λ°±
|
91 |
+
try:
|
92 |
+
frame = _render_with_trimesh(scene, resolution)
|
93 |
+
except Exception as e:
|
94 |
+
print("trimesh λ λ μ€ν¨, pyrender ν΄λ°±:", e)
|
95 |
+
frame = _render_with_pyrender(scene, resolution)
|
96 |
|
|
|
|
|
|
|
97 |
frames.append(frame)
|
98 |
|
|
|
99 |
frames[0].save(
|
100 |
output_path,
|
101 |
save_all=True,
|
102 |
append_images=frames[1:],
|
|
|
103 |
duration=int(1000 / fps),
|
104 |
loop=0,
|
105 |
)
|
106 |
+
print("GIF saved:", output_path)
|
107 |
return output_path
|
108 |
|
109 |
+
# ββββββββββββββββββββ 4. GLB λ³ν ββββββββββββββββββββ
|
|
|
|
|
|
|
110 |
def modify_glb_file(input_glb_path, output_glb_path, animation_type="rotate"):
|
111 |
+
"""λ¨μΌ λ³νμ μ μ©ν μ GLB μ μ₯ (μ€μ μ λλ©μ΄μ
μ μλ)"""
|
|
|
|
|
112 |
try:
|
113 |
+
scn = trimesh.load(input_glb_path)
|
114 |
+
if not isinstance(scn, trimesh.Scene):
|
115 |
+
scn = trimesh.Scene(scn)
|
116 |
|
|
|
117 |
if animation_type == "rotate":
|
118 |
+
T = tf.rotation_matrix(math.pi / 4, [0, 1, 0])
|
119 |
elif animation_type == "float":
|
120 |
+
T = tf.translation_matrix([0, 0.5, 0])
|
121 |
elif animation_type == "pulse":
|
122 |
+
T = tf.scale_matrix(1.2)
|
123 |
elif animation_type == "explode":
|
124 |
+
T = tf.translation_matrix([0.5, 0, 0])
|
125 |
elif animation_type == "assemble":
|
126 |
+
T = tf.translation_matrix([-0.5, 0, 0])
|
127 |
elif animation_type == "swing":
|
128 |
+
T = tf.rotation_matrix(math.pi / 8, [0, 0, 1])
|
129 |
else:
|
130 |
+
T = np.eye(4)
|
131 |
|
132 |
+
scn.apply_transform(T)
|
133 |
+
scn.export(output_glb_path)
|
|
|
134 |
return output_glb_path
|
|
|
135 |
except Exception as e:
|
136 |
+
print("GLB λ³ν μ€ν¨, μλ³Έ 볡μ¬:", e)
|
|
|
|
|
|
|
137 |
shutil.copy(input_glb_path, output_glb_path)
|
|
|
138 |
return output_glb_path
|
139 |
|
140 |
+
# ββββββββββββββββββββ 5. Gradio νμ΄νλΌμΈ ββββββββββββββββββββ
|
|
|
|
|
|
|
141 |
@spaces.GPU
|
142 |
def process_3d_model(input_3d, animation_type, animation_duration, fps):
|
143 |
+
"""μ 체 νμ΄νλΌμΈ: GLB λ³ν β GIF λ λ β JSON μμ±"""
|
|
|
|
|
144 |
try:
|
145 |
base = os.path.splitext(os.path.basename(input_3d))[0]
|
146 |
+
glb_out = os.path.join(LOG_PATH, f"animated_{base}.glb")
|
147 |
+
gif_out = os.path.join(LOG_PATH, f"preview_{base}.gif")
|
148 |
+
json_out = os.path.join(LOG_PATH, f"metadata_{base}.json")
|
|
|
|
|
|
|
149 |
|
150 |
+
modify_glb_file(input_3d, glb_out, animation_type)
|
151 |
create_model_animation_gif(
|
152 |
+
gif_out, input_3d, animation_type, animation_duration, fps
|
|
|
|
|
|
|
|
|
153 |
)
|
154 |
|
155 |
+
meta = dict(
|
|
|
156 |
animation_type=animation_type,
|
157 |
duration=animation_duration,
|
158 |
fps=fps,
|
159 |
original_model=os.path.basename(input_3d),
|
160 |
created_at=time.strftime("%Y-%m-%d %H:%M:%S"),
|
161 |
)
|
162 |
+
with open(json_out, "w") as f:
|
163 |
+
json.dump(meta, f, indent=4)
|
164 |
|
165 |
+
return glb_out, gif_out, json_out
|
166 |
|
167 |
except Exception as e:
|
168 |
+
print("process_3d_model μ€ν¨:", e)
|
169 |
+
err_gif = os.path.join(LOG_PATH, "error.gif")
|
170 |
+
Image.new("RGB", (640, 480), (255, 0, 0)).save(err_gif)
|
171 |
+
return input_3d, err_gif, None
|
|
|
172 |
|
173 |
+
# ββββββββββββββββββββ 6. Gradio UI ββββββββββββββββββββ
|
|
|
|
|
|
|
174 |
with gr.Blocks(title="GLB μ λλ©μ΄μ
μμ±κΈ°") as demo:
|
175 |
gr.Markdown(
|
176 |
"""
|
177 |
<h2><b>GLB μ λλ©μ΄μ
μμ±κΈ° - 3D λͺ¨λΈ μμ§μ ν¨κ³Ό</b></h2>
|
178 |
+
μ
λ‘λν GLB λͺ¨λΈμ μ λλ©μ΄μ
μ μ μ©ν΄ λ³νλ GLBΒ·GIFΒ·JSONμ μ 곡ν©λλ€.
|
179 |
"""
|
180 |
)
|
|
|
181 |
with gr.Row():
|
182 |
with gr.Column():
|
183 |
+
inp = gr.Model3D(label="3D λͺ¨λΈ μ
λ‘λ (GLB)")
|
184 |
+
typ = gr.Dropdown(
|
185 |
label="μ λλ©μ΄μ
μ ν",
|
186 |
choices=["rotate", "float", "explode", "assemble", "pulse", "swing"],
|
187 |
value="rotate",
|
188 |
)
|
189 |
+
dur = gr.Slider("μ λλ©μ΄μ
κΈΈμ΄ (μ΄)", 1.0, 10.0, 3.0, 0.5)
|
190 |
+
fps = gr.Slider("FPS", 15, 60, 30, 1)
|
191 |
+
btn = gr.Button("μ λλ©μ΄μ
μμ±")
|
|
|
|
|
|
|
192 |
with gr.Column():
|
193 |
+
out_glb = gr.Model3D(label="μ λλ©μ΄μ
λ GLB")
|
194 |
+
out_gif = gr.Image(label="미리보기 GIF")
|
195 |
+
out_json = gr.File(label="λ©νλ°μ΄ν° JSON")
|
196 |
|
197 |
+
btn.click(
|
198 |
fn=process_3d_model,
|
199 |
+
inputs=[inp, typ, dur, fps],
|
200 |
+
outputs=[out_glb, out_gif, out_json],
|
201 |
)
|
202 |
|
203 |
+
ex = [[f] for f in glob.glob("./data/demo_glb/*.glb")]
|
204 |
+
if ex:
|
205 |
+
gr.Examples(examples=ex, inputs=[inp])
|
|
|
206 |
|
|
|
|
|
|
|
207 |
if __name__ == "__main__":
|
208 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|