Update app.py
Browse files
app.py
CHANGED
|
@@ -1,298 +1,222 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import trimesh
|
| 7 |
-
from PIL import Image
|
| 8 |
-
import math
|
| 9 |
import trimesh.transformations as tf
|
| 10 |
|
| 11 |
-
os.environ[
|
| 12 |
|
| 13 |
import gradio as gr
|
| 14 |
import spaces
|
| 15 |
|
| 16 |
-
# ๊ฒฐ๊ณผ ์ ์ฅ
|
| 17 |
-
LOG_PATH =
|
| 18 |
os.makedirs(LOG_PATH, exist_ok=True)
|
| 19 |
|
| 20 |
-
def create_textual_animation_gif(output_path, model_name, animation_type, duration=3.0, fps=30):
|
| 21 |
-
"""ํ
์คํธ ๊ธฐ๋ฐ์ ๊ฐ๋จํ ์ ๋๋ฉ์ด์
GIF ์์ฑ"""
|
| 22 |
-
try:
|
| 23 |
-
frames = []
|
| 24 |
-
num_frames = int(duration * fps)
|
| 25 |
-
if num_frames > 60:
|
| 26 |
-
num_frames = 60
|
| 27 |
-
|
| 28 |
-
for i in range(num_frames):
|
| 29 |
-
t = i / (num_frames - 1)
|
| 30 |
-
angle = t * 360
|
| 31 |
-
|
| 32 |
-
img = Image.new('RGB', (640, 480), color=(240, 240, 240))
|
| 33 |
-
draw = ImageDraw.Draw(img)
|
| 34 |
-
|
| 35 |
-
draw.text((50, 50), f"Model: {os.path.basename(model_name)}", fill=(0, 0, 0))
|
| 36 |
-
draw.text((50, 100), f"Animation Type: {animation_type}", fill=(0, 0, 0))
|
| 37 |
-
draw.text((50, 150), f"Frame: {i+1}/{num_frames}", fill=(0, 0, 0))
|
| 38 |
-
|
| 39 |
-
# ์ ๋๋ฉ์ด์
์ ํ๋ณ ์๊ฐํ
|
| 40 |
-
center_x, center_y = 320, 240
|
| 41 |
-
if animation_type == 'rotate':
|
| 42 |
-
radius = 100
|
| 43 |
-
x = center_x + radius * math.cos(math.radians(angle))
|
| 44 |
-
y = center_y + radius * math.sin(math.radians(angle))
|
| 45 |
-
draw.rectangle((x-40, y-40, x+40, y+40), outline=(0, 0, 0), fill=(255, 0, 0))
|
| 46 |
-
elif animation_type == 'float':
|
| 47 |
-
offset_y = 50 * math.sin(2 * math.pi * t)
|
| 48 |
-
draw.ellipse((center_x-50, center_y-50+offset_y, center_x+50, center_y+50+offset_y),
|
| 49 |
-
outline=(0, 0, 0), fill=(0, 0, 255))
|
| 50 |
-
elif animation_type in ['explode', 'assemble']:
|
| 51 |
-
scale = t if animation_type == 'explode' else 1 - t
|
| 52 |
-
for j in range(8):
|
| 53 |
-
angle_j = j * 45
|
| 54 |
-
dist = 120 * scale
|
| 55 |
-
x = center_x + dist * math.cos(math.radians(angle_j))
|
| 56 |
-
y = center_y + dist * math.sin(math.radians(angle_j))
|
| 57 |
-
|
| 58 |
-
if j % 3 == 0:
|
| 59 |
-
draw.rectangle((x-20, y-20, x+20, y+20), outline=(0, 0, 0), fill=(255, 0, 0))
|
| 60 |
-
elif j % 3 == 1:
|
| 61 |
-
draw.ellipse((x-20, y-20, x+20, y+20), outline=(0, 0, 0), fill=(0, 255, 0))
|
| 62 |
-
else:
|
| 63 |
-
draw.polygon([(x, y-20), (x+20, y+20), (x-20, y+20)], outline=(0, 0, 0), fill=(0, 0, 255))
|
| 64 |
-
elif animation_type == 'pulse':
|
| 65 |
-
scale = 0.5 + 0.5 * math.sin(2 * math.pi * t)
|
| 66 |
-
radius = 100 * scale
|
| 67 |
-
draw.ellipse((center_x-radius, center_y-radius, center_x+radius, center_y+radius),
|
| 68 |
-
outline=(0, 0, 0), fill=(0, 255, 0))
|
| 69 |
-
elif animation_type == 'swing':
|
| 70 |
-
angle_offset = 30 * math.sin(2 * math.pi * t)
|
| 71 |
-
points = [
|
| 72 |
-
(center_x + 100 * math.cos(math.radians(angle_offset)), center_y - 80),
|
| 73 |
-
(center_x + 100 * math.cos(math.radians(120 + angle_offset)), center_y + 40),
|
| 74 |
-
(center_x + 100 * math.cos(math.radians(240 + angle_offset)), center_y + 40)
|
| 75 |
-
]
|
| 76 |
-
draw.polygon(points, outline=(0, 0, 0), fill=(255, 165, 0))
|
| 77 |
-
|
| 78 |
-
frames.append(img)
|
| 79 |
-
|
| 80 |
-
frames[0].save(
|
| 81 |
-
output_path,
|
| 82 |
-
save_all=True,
|
| 83 |
-
append_images=frames[1:],
|
| 84 |
-
optimize=False,
|
| 85 |
-
duration=int(1000 / fps),
|
| 86 |
-
loop=0
|
| 87 |
-
)
|
| 88 |
-
print(f"Created textual animation GIF at {output_path}")
|
| 89 |
-
return output_path
|
| 90 |
-
except Exception as e:
|
| 91 |
-
print(f"Error creating textual animation: {str(e)}")
|
| 92 |
-
return None
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
try:
|
| 97 |
-
# ๊ฐ๋จํ ๋ฐฉ๋ฒ: ์๋ณธ ํ์ผ์ ๊ทธ๋๋ก ๋ก๋ํ์ฌ ๋จ์ผ ๋ณํ ์ ์ฉ ํ ์ ์ฅ
|
| 98 |
-
# ์ฌ๋ฌ ๋จ๊ณ๋ก ๋๋์ด ์ฒ๋ฆฌํ์ฌ ๊ฐ ๋จ๊ณ์์ ์ค๋ฅ๋ฅผ ํ์ธํ ์ ์๋๋ก ํจ
|
| 99 |
-
|
| 100 |
-
print(f"Step 1: Loading GLB file '{input_glb_path}'")
|
| 101 |
scene = trimesh.load(input_glb_path)
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
#
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
# Y์ถ ๊ธฐ์ค 45๋ ํ์
|
| 110 |
-
transform = tf.rotation_matrix(math.pi/4, [0, 1, 0])
|
| 111 |
-
elif animation_type == 'float':
|
| 112 |
-
# ์๋ก ์ด๋
|
| 113 |
transform = tf.translation_matrix([0, 0.5, 0])
|
| 114 |
-
elif animation_type ==
|
| 115 |
-
# ํฌ๊ธฐ ํ๋
|
| 116 |
transform = tf.scale_matrix(1.2)
|
| 117 |
-
elif animation_type ==
|
| 118 |
-
# X์ถ ๋ฐฉํฅ์ผ๋ก ์ฝ๊ฐ ์ด๋
|
| 119 |
transform = tf.translation_matrix([0.5, 0, 0])
|
| 120 |
-
elif animation_type ==
|
| 121 |
-
# X์ถ ๋ฐฉํฅ์ผ๋ก ์ฝ๊ฐ ์ด๋ (๋ฐ๋ ๋ฐฉํฅ)
|
| 122 |
transform = tf.translation_matrix([-0.5, 0, 0])
|
| 123 |
-
elif animation_type ==
|
| 124 |
-
|
| 125 |
-
transform = tf.rotation_matrix(math.pi/8, [0, 0, 1])
|
| 126 |
-
|
| 127 |
-
print(f"Step 3: Applying transformation to scene")
|
| 128 |
-
if isinstance(scene, trimesh.Scene):
|
| 129 |
-
# Scene์ธ ๊ฒฝ์ฐ ์ง์ ๋ณํ ์ ์ฉ
|
| 130 |
-
scene.apply_transform(transform)
|
| 131 |
-
print(f"Applied transform to scene")
|
| 132 |
else:
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
scene.export(output_glb_path)
|
| 137 |
-
print(f"
|
| 138 |
-
|
| 139 |
return output_glb_path
|
| 140 |
-
|
| 141 |
except Exception as e:
|
| 142 |
-
print(
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
return output_glb_path
|
| 151 |
-
except Exception as copy_error:
|
| 152 |
-
print(f"Error copying GLB: {copy_error}")
|
| 153 |
-
return input_glb_path # ์๋ณธ ํ์ผ ๊ฒฝ๋ก ๋ฐํ
|
| 154 |
|
|
|
|
|
|
|
|
|
|
| 155 |
@spaces.GPU
|
| 156 |
def process_3d_model(input_3d, animation_type, animation_duration, fps):
|
| 157 |
-
"""
|
| 158 |
-
|
| 159 |
-
|
| 160 |
try:
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
animated_gif = create_textual_animation_gif(
|
| 174 |
-
animated_gif_path,
|
| 175 |
-
os.path.basename(input_3d),
|
| 176 |
animation_type,
|
| 177 |
animation_duration,
|
| 178 |
-
fps
|
| 179 |
)
|
| 180 |
-
|
| 181 |
-
# 3
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
"
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
with open(json_path, 'w') as f:
|
| 192 |
json.dump(metadata, f, indent=4)
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
except Exception as e:
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
create_textual_animation_gif(
|
| 204 |
-
animated_gif_path,
|
| 205 |
-
os.path.basename(input_3d),
|
| 206 |
-
animation_type,
|
| 207 |
-
animation_duration,
|
| 208 |
-
fps
|
| 209 |
-
)
|
| 210 |
-
return input_3d, animated_gif_path, None
|
| 211 |
-
except:
|
| 212 |
-
return f"Error processing file: {str(e)}", None, None
|
| 213 |
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
| 215 |
with gr.Blocks(title="GLB ์ ๋๋ฉ์ด์
์์ฑ๊ธฐ") as demo:
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
- ์ด ๋ฐ๋ชจ๋ ์
๋ก๋๋ GLB ํ์ผ์ ์ ๋๋ฉ์ด์
์ ์ ์ฉํฉ๋๋ค.
|
| 224 |
-
- ๋ค์ํ ์ ๋๋ฉ์ด์
์คํ์ผ ์ค์์ ์ ํํ์ธ์: ํ์ , ๋ถ์ , ํญ๋ฐ, ์กฐ๋ฆฝ, ํ์ค, ์ค์.
|
| 225 |
-
- ๊ฒฐ๊ณผ๋ ์ ๋๋ฉ์ด์
๋ GLB ํ์ผ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์ฉ GIF ํ์ผ๋ก ์ ๊ณต๋ฉ๋๋ค.
|
| 226 |
-
""")
|
| 227 |
-
|
| 228 |
with gr.Row():
|
| 229 |
with gr.Column():
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
label="์ ๋๋ฉ์ด์
๊ธธ์ด (์ด)",
|
| 243 |
-
minimum=1.0,
|
| 244 |
-
maximum=10.0,
|
| 245 |
-
value=3.0,
|
| 246 |
-
step=0.5
|
| 247 |
-
)
|
| 248 |
-
fps = gr.Slider(
|
| 249 |
-
label="์ด๋น ํ๋ ์ ์",
|
| 250 |
-
minimum=15,
|
| 251 |
-
maximum=60,
|
| 252 |
-
value=30,
|
| 253 |
-
step=1
|
| 254 |
-
)
|
| 255 |
-
|
| 256 |
-
submit_btn = gr.Button("๋ชจ๋ธ ์ฒ๋ฆฌ ๋ฐ ์ ๋๋ฉ์ด์
์์ฑ")
|
| 257 |
-
|
| 258 |
with gr.Column():
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
# ์ ๋๋ฉ์ด์
์ ํ ์ค๋ช
|
| 265 |
-
gr.Markdown("""
|
| 266 |
-
### ์ ๋๋ฉ์ด์
์ ํ ์ค๋ช
|
| 267 |
-
- **ํ์ (rotate)**: ๋ชจ๋ธ์ด Y์ถ์ ์ค์ฌ์ผ๋ก ํ์ ํฉ๋๋ค.
|
| 268 |
-
- **๋ถ์ (float)**: ๋ชจ๋ธ์ด ์์๋๋ก ๋ถ๋๋ฝ๊ฒ ๋ ๋ค๋๋๋ค.
|
| 269 |
-
- **ํญ๋ฐ(explode)**: ๋ชจ๋ธ์ ๊ฐ ๋ถ๋ถ์ด ์ค์ฌ์์ ๋ฐ๊นฅ์ชฝ์ผ๋ก ํผ์ ธ๋๊ฐ๋๋ค.
|
| 270 |
-
- **์กฐ๋ฆฝ(assemble)**: ํญ๋ฐ ์ ๋๋ฉ์ด์
์ ๋ฐ๋ - ๋ถํ๋ค์ด ํจ๊ป ๋ชจ์
๋๋ค.
|
| 271 |
-
- **ํ์ค(pulse)**: ๋ชจ๋ธ์ด ํฌ๊ธฐ๊ฐ ์ปค์ก๋ค ์์์ก๋ค๋ฅผ ๋ฐ๋ณตํฉ๋๋ค.
|
| 272 |
-
- **์ค์(swing)**: ๋ชจ๋ธ์ด ์ข์ฐ๋ก ๋ถ๋๋ฝ๊ฒ ํ๋ค๋ฆฝ๋๋ค.
|
| 273 |
-
|
| 274 |
-
### ํ
|
| 275 |
-
- ์ ๋๋ฉ์ด์
๊ธธ์ด์ FPS๋ฅผ ์กฐ์ ํ์ฌ ์์ง์์ ์๋์ ๋ถ๋๋ฌ์์ ์กฐ์ ํ ์ ์์ต๋๋ค.
|
| 276 |
-
- ๋ณต์กํ ๋ชจ๋ธ์ ์ฒ๋ฆฌ ์๊ฐ์ด ๋ ์ค๋ ๊ฑธ๋ฆด ์ ์์ต๋๋ค.
|
| 277 |
-
- GIF ๋ฏธ๋ฆฌ๋ณด๊ธฐ๋ ๋น ๋ฅธ ์ฐธ์กฐ์ฉ์ด๋ฉฐ, ๊ณ ํ์ง ๊ฒฐ๊ณผ๋ฅผ ์ํด์๋ ์ ๋๋ฉ์ด์
๋ GLB ํ์ผ์ ๋ค์ด๋ก๋ํ์ธ์.
|
| 278 |
-
""")
|
| 279 |
-
|
| 280 |
-
# ๋ฒํผ ๋์ ์ค์
|
| 281 |
submit_btn.click(
|
| 282 |
fn=process_3d_model,
|
| 283 |
inputs=[input_3d, animation_type, animation_duration, fps],
|
| 284 |
-
outputs=[output_3d, output_gif, output_json]
|
| 285 |
)
|
| 286 |
-
|
| 287 |
-
# ์์ ์ค๋น
|
| 288 |
-
example_files = [[f] for f in glob.glob('./data/demo_glb/*.glb')]
|
| 289 |
-
if example_files:
|
| 290 |
-
gr.Examples(
|
| 291 |
-
examples=example_files,
|
| 292 |
-
inputs=[input_3d],
|
| 293 |
-
examples_per_page=10,
|
| 294 |
-
)
|
| 295 |
|
| 296 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
if __name__ == "__main__":
|
| 298 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
GLB ์ ๋๋ฉ์ด์
์์ฑ๊ธฐ โ ์
๋ก๋ํ 3D ๋ชจ๋ธ์ ์ง์ ๋ ๋๋งํด GIFยทGLB๋ก ๋ด๋ณด๋
๋๋ค.
|
| 3 |
+
(Gradio + Trimesh ์คํ์คํฌ๋ฆฐ ๋ ๋๋ง)
|
| 4 |
+
|
| 5 |
+
โป ์ฃผ์ ๋ณ๊ฒฝ
|
| 6 |
+
1. create_model_animation_gif : ๋ชจ๋ธ์ ์ค์ ๋ ๋๋งํด ํ๋ ์ ์์ฑ
|
| 7 |
+
2. process_3d_model ๋ด๋ถ์์ ์ GIF ํจ์ ์ฌ์ฉ
|
| 8 |
+
3. ์์ธ ์ฒ๋ฆฌยท์ฃผ์ ์ผ๋ถ ๋ณด๊ฐ
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import os, io, time, glob, json, math
|
| 12 |
import numpy as np
|
| 13 |
import trimesh
|
| 14 |
+
from PIL import Image
|
|
|
|
| 15 |
import trimesh.transformations as tf
|
| 16 |
|
| 17 |
+
os.environ["PYOPENGL_PLATFORM"] = "egl" # ๋ชจ๋ํฐ ์์ด ๋ ๋๋ง
|
| 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 |
+
# 1) ์ค์ GLB ๋ชจ๋ธ์ ๋ ๋๋งํด GIF ์์ฑ
|
| 29 |
+
# ------------------------------------------------------------------
|
| 30 |
+
def create_model_animation_gif(
|
| 31 |
+
output_path: str,
|
| 32 |
+
input_glb_path: str,
|
| 33 |
+
animation_type: str,
|
| 34 |
+
duration: float = 3.0,
|
| 35 |
+
fps: int = 30,
|
| 36 |
+
resolution=(640, 480),
|
| 37 |
+
):
|
| 38 |
+
"""
|
| 39 |
+
์
๋ก๋๋ GLB ๋ชจ๋ธ์ ์คํ์คํฌ๋ฆฐ์ผ๋ก ๋ ๋๋งํด ์ ๋๋ฉ์ด์
GIF ์์ฑ
|
| 40 |
+
"""
|
| 41 |
+
# (1) ๋ชจ๋ธ ๋ก๋
|
| 42 |
+
scene = trimesh.load(input_glb_path)
|
| 43 |
+
if isinstance(scene, trimesh.Trimesh):
|
| 44 |
+
scene = trimesh.Scene(scene)
|
| 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 |
+
# (2) ํ๋ ์๋ง๋ค ๋ณํ ์ ์ฉ
|
| 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":
|
| 55 |
+
M = tf.translation_matrix([0, 0.5 * math.sin(2 * math.pi * t), 0])
|
| 56 |
+
elif animation_type == "pulse":
|
| 57 |
+
M = tf.scale_matrix(0.8 + 0.4 * math.sin(2 * math.pi * t))
|
| 58 |
+
elif animation_type == "explode":
|
| 59 |
+
M = tf.translation_matrix([0.5 * t, 0, 0])
|
| 60 |
+
elif animation_type == "assemble":
|
| 61 |
+
M = tf.translation_matrix([0.5 * (1 - t), 0, 0])
|
| 62 |
+
elif animation_type == "swing":
|
| 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 |
+
scene_i.apply_transform(M)
|
| 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(f"Created model animation GIF at {output_path}")
|
| 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 |
scene = trimesh.load(input_glb_path)
|
| 96 |
+
if not isinstance(scene, trimesh.Scene):
|
| 97 |
+
scene = trimesh.Scene(scene)
|
| 98 |
+
|
| 99 |
+
# ๋ณํ ๋งคํธ๋ฆญ์ค ๊ฒฐ์
|
| 100 |
+
if animation_type == "rotate":
|
| 101 |
+
transform = tf.rotation_matrix(math.pi / 4, [0, 1, 0])
|
| 102 |
+
elif animation_type == "float":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
transform = tf.translation_matrix([0, 0.5, 0])
|
| 104 |
+
elif animation_type == "pulse":
|
|
|
|
| 105 |
transform = tf.scale_matrix(1.2)
|
| 106 |
+
elif animation_type == "explode":
|
|
|
|
| 107 |
transform = tf.translation_matrix([0.5, 0, 0])
|
| 108 |
+
elif animation_type == "assemble":
|
|
|
|
| 109 |
transform = tf.translation_matrix([-0.5, 0, 0])
|
| 110 |
+
elif animation_type == "swing":
|
| 111 |
+
transform = tf.rotation_matrix(math.pi / 8, [0, 0, 1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
else:
|
| 113 |
+
transform = np.eye(4)
|
| 114 |
+
|
| 115 |
+
scene.apply_transform(transform)
|
| 116 |
scene.export(output_glb_path)
|
| 117 |
+
print(f"Exported modified GLB to {output_glb_path}")
|
|
|
|
| 118 |
return output_glb_path
|
| 119 |
+
|
| 120 |
except Exception as e:
|
| 121 |
+
print("Error modifying GLB:", e)
|
| 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 |
+
animated_glb = os.path.join(LOG_PATH, f"animated_{base}.glb")
|
| 141 |
+
animated_gif = os.path.join(LOG_PATH, f"preview_{base}.gif")
|
| 142 |
+
json_path = os.path.join(LOG_PATH, f"metadata_{base}.json")
|
| 143 |
+
|
| 144 |
+
# 1) GLB ๋ณํ
|
| 145 |
+
modify_glb_file(input_3d, animated_glb, animation_type)
|
| 146 |
+
|
| 147 |
+
# 2) ์ค์ ๋ชจ๋ธ ๊ธฐ๋ฐ GIF
|
| 148 |
+
create_model_animation_gif(
|
| 149 |
+
animated_gif,
|
| 150 |
+
input_3d,
|
|
|
|
|
|
|
|
|
|
| 151 |
animation_type,
|
| 152 |
animation_duration,
|
| 153 |
+
fps,
|
| 154 |
)
|
| 155 |
+
|
| 156 |
+
# 3) ๋ฉํ๋ฐ์ดํฐ ๊ธฐ๋ก
|
| 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(json_path, "w") as f:
|
|
|
|
|
|
|
| 165 |
json.dump(metadata, f, indent=4)
|
| 166 |
+
|
| 167 |
+
return animated_glb, animated_gif, json_path
|
| 168 |
+
|
|
|
|
| 169 |
except Exception as e:
|
| 170 |
+
# ์น๋ช
์ ์ค๋ฅ ์: ์๋ณธ ๋ชจ๋ธ + ๋น GIF
|
| 171 |
+
print("Error in process_3d_model:", e)
|
| 172 |
+
fallback_gif = os.path.join(LOG_PATH, "error.gif")
|
| 173 |
+
Image.new("RGB", (640, 480), (255, 0, 0)).save(fallback_gif)
|
| 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 |
+
์ ์ ์ธ 3D ๋ชจ๋ธ(GLB)์ ํ์ ยท๋ถ์ ยทํญ๋ฐ ๋ฑ ์ ๋๋ฉ์ด์
์ ์ ์ฉํฉ๋๋ค.
|
| 185 |
+
"""
|
| 186 |
+
)
|
| 187 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
with gr.Row():
|
| 189 |
with gr.Column():
|
| 190 |
+
input_3d = gr.Model3D(label="3D ๋ชจ๋ธ ์
๋ก๋ (GLB)")
|
| 191 |
+
animation_type = gr.Dropdown(
|
| 192 |
+
label="์ ๋๋ฉ์ด์
์ ํ",
|
| 193 |
+
choices=["rotate", "float", "explode", "assemble", "pulse", "swing"],
|
| 194 |
+
value="rotate",
|
| 195 |
+
)
|
| 196 |
+
animation_duration = gr.Slider(
|
| 197 |
+
label="์ ๋๋ฉ์ด์
๊ธธ์ด (์ด)", minimum=1.0, maximum=10.0, value=3.0, step=0.5
|
| 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 |
+
output_3d = gr.Model3D(label="์ ๋๋ฉ์ด์
๋ GLB")
|
| 204 |
+
output_gif = gr.Image(label="๋ฏธ๋ฆฌ๋ณด๊ธฐ GIF")
|
| 205 |
+
output_json = gr.File(label="๋ฉํ๋ฐ์ดํฐ JSON")
|
| 206 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
submit_btn.click(
|
| 208 |
fn=process_3d_model,
|
| 209 |
inputs=[input_3d, animation_type, animation_duration, fps],
|
| 210 |
+
outputs=[output_3d, output_gif, output_json],
|
| 211 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
+
# ์์ GLB ํด๋๊ฐ ์๋ค๋ฉด ์๋ ๋ฑ๋ก
|
| 214 |
+
example_glbs = [[f] for f in glob.glob("./data/demo_glb/*.glb")]
|
| 215 |
+
if example_glbs:
|
| 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)
|