Update app.py
Browse files
app.py
CHANGED
|
@@ -19,6 +19,62 @@ import spaces
|
|
| 19 |
LOG_PATH = './results/demo'
|
| 20 |
os.makedirs(LOG_PATH, exist_ok=True)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def create_textual_animation_gif(output_path, model_name, animation_type, duration=3.0, fps=30):
|
| 23 |
"""ν
μ€νΈ κΈ°λ°μ κ°λ¨ν μ λλ©μ΄μ
GIF μμ± - λ λλ§ μ€ν¨ μ λ체μ©"""
|
| 24 |
try:
|
|
@@ -107,13 +163,152 @@ def create_textual_animation_gif(output_path, model_name, animation_type, durati
|
|
| 107 |
print(f"Error creating textual animation: {str(e)}")
|
| 108 |
return None
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
@spaces.GPU
|
| 111 |
def process_3d_model(input_3d, animation_type, animation_duration, fps):
|
| 112 |
"""Process a 3D model and apply animation"""
|
| 113 |
print(f"Processing: {input_3d} with animation type: {animation_type}")
|
| 114 |
|
| 115 |
try:
|
| 116 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
base_filename = os.path.basename(input_3d).rsplit('.', 1)[0]
|
| 118 |
text_gif_path = os.path.join(LOG_PATH, f'text_animated_{base_filename}.gif')
|
| 119 |
animated_gif_path = create_textual_animation_gif(
|
|
@@ -124,18 +319,7 @@ def process_3d_model(input_3d, animation_type, animation_duration, fps):
|
|
| 124 |
fps
|
| 125 |
)
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
copy_glb_path = os.path.join(LOG_PATH, f'copy_{base_filename}.glb')
|
| 129 |
-
import shutil
|
| 130 |
-
try:
|
| 131 |
-
shutil.copy(input_3d, copy_glb_path)
|
| 132 |
-
animated_glb_path = copy_glb_path
|
| 133 |
-
print(f"Copied original GLB to {copy_glb_path}")
|
| 134 |
-
except Exception as e:
|
| 135 |
-
print(f"Error copying GLB: {e}")
|
| 136 |
-
animated_glb_path = input_3d
|
| 137 |
-
|
| 138 |
-
# λ©νλ°μ΄ν° μμ±
|
| 139 |
metadata = {
|
| 140 |
"animation_type": animation_type,
|
| 141 |
"duration": animation_duration,
|
|
|
|
| 19 |
LOG_PATH = './results/demo'
|
| 20 |
os.makedirs(LOG_PATH, exist_ok=True)
|
| 21 |
|
| 22 |
+
def create_simple_rotation_animation(input_glb_path, output_glb_path, num_frames=30):
|
| 23 |
+
"""
|
| 24 |
+
μλ³Έ GLB νμΌμ νμ μ λλ©μ΄μ
μ μ μ©ν μλ‘μ΄ GLB νμΌ μμ±
|
| 25 |
+
"""
|
| 26 |
+
try:
|
| 27 |
+
# GLB νμΌ λ‘λ
|
| 28 |
+
scene = trimesh.load(input_glb_path)
|
| 29 |
+
|
| 30 |
+
if isinstance(scene, trimesh.Scene):
|
| 31 |
+
# μ λλ©μ΄μ
μ μ© (첫 λ²μ§Έ νλ μλ§ μ¬μ©)
|
| 32 |
+
angle = math.pi / 4 # 45λ νμ
|
| 33 |
+
|
| 34 |
+
# μ¬μ λͺ¨λ λ©μμ νμ μ μ©
|
| 35 |
+
for node_name, transform, geometry_name in scene.graph.nodes_geometry:
|
| 36 |
+
# μλ³Έ μμΉ λ°±μ
|
| 37 |
+
original_transform = scene.graph[node_name][0]
|
| 38 |
+
|
| 39 |
+
# νμ λ³ν κ³μ°
|
| 40 |
+
rotation = tf.rotation_matrix(angle, [0, 1, 0])
|
| 41 |
+
|
| 42 |
+
# μ λ³ν = μλ³Έ λ³ν * νμ λ³ν
|
| 43 |
+
new_transform = np.dot(original_transform, rotation)
|
| 44 |
+
|
| 45 |
+
# λ³ν μ μ©
|
| 46 |
+
scene.graph[node_name] = new_transform
|
| 47 |
+
|
| 48 |
+
# νμ λ GLB μ μ₯
|
| 49 |
+
scene.export(output_glb_path)
|
| 50 |
+
print(f"Saved animated GLB to {output_glb_path}")
|
| 51 |
+
return output_glb_path
|
| 52 |
+
|
| 53 |
+
elif isinstance(scene, trimesh.Trimesh):
|
| 54 |
+
# λ¨μΌ λ©μμΈ κ²½μ°
|
| 55 |
+
new_scene = trimesh.Scene()
|
| 56 |
+
|
| 57 |
+
# λ©μμ νμ μ μ©
|
| 58 |
+
angle = math.pi / 4 # 45λ νμ
|
| 59 |
+
rotation = tf.rotation_matrix(angle, [0, 1, 0])
|
| 60 |
+
scene.apply_transform(rotation)
|
| 61 |
+
|
| 62 |
+
# νμ λ λ©μλ₯Ό μ¬μ μΆκ°
|
| 63 |
+
new_scene.add_geometry(scene)
|
| 64 |
+
|
| 65 |
+
# μ¬μ GLBλ‘ μ μ₯
|
| 66 |
+
new_scene.export(output_glb_path)
|
| 67 |
+
print(f"Saved animated GLB to {output_glb_path}")
|
| 68 |
+
return output_glb_path
|
| 69 |
+
|
| 70 |
+
else:
|
| 71 |
+
print(f"Unsupported format: {type(scene)}")
|
| 72 |
+
return None
|
| 73 |
+
|
| 74 |
+
except Exception as e:
|
| 75 |
+
print(f"Error creating animation: {str(e)}")
|
| 76 |
+
return None
|
| 77 |
+
|
| 78 |
def create_textual_animation_gif(output_path, model_name, animation_type, duration=3.0, fps=30):
|
| 79 |
"""ν
μ€νΈ κΈ°λ°μ κ°λ¨ν μ λλ©μ΄μ
GIF μμ± - λ λλ§ μ€ν¨ μ λ체μ©"""
|
| 80 |
try:
|
|
|
|
| 163 |
print(f"Error creating textual animation: {str(e)}")
|
| 164 |
return None
|
| 165 |
|
| 166 |
+
def create_glb_with_animation(input_glb_path, animation_type, duration=3.0, fps=30):
|
| 167 |
+
"""
|
| 168 |
+
μ
λ‘λλ GLB νμΌμ μ λλ©μ΄μ
μ μ μ©ν μλ‘μ΄ GLB νμΌ μμ±
|
| 169 |
+
"""
|
| 170 |
+
try:
|
| 171 |
+
base_filename = os.path.basename(input_glb_path).rsplit('.', 1)[0]
|
| 172 |
+
output_glb_path = os.path.join(LOG_PATH, f"animated_{base_filename}.glb")
|
| 173 |
+
|
| 174 |
+
# μλ³Έ λͺ¨λΈ λ‘λ
|
| 175 |
+
scene = trimesh.load(input_glb_path)
|
| 176 |
+
print(f"Loaded GLB: {type(scene)}")
|
| 177 |
+
|
| 178 |
+
# μ λλ©μ΄μ
μ νμ λ°λΌ μ²λ¦¬
|
| 179 |
+
if animation_type == 'rotate':
|
| 180 |
+
# μκ³λ°©ν₯ 45λ νμ
|
| 181 |
+
angle = math.pi / 4
|
| 182 |
+
axis = [0, 1, 0] # YμΆ (μμ§μΆ)
|
| 183 |
+
elif animation_type == 'float':
|
| 184 |
+
# μμ§μΌλ‘ μμ§μ
|
| 185 |
+
angle = 0
|
| 186 |
+
axis = [0, 1, 0]
|
| 187 |
+
# YμΆ λ°©ν₯μΌλ‘ μ΄λ
|
| 188 |
+
if isinstance(scene, trimesh.Scene):
|
| 189 |
+
for node_name, transform, geometry_name in scene.graph.nodes_geometry:
|
| 190 |
+
# κ°λ³κ² μλ‘ μ΄λ
|
| 191 |
+
translation = tf.translation_matrix([0, 0.2, 0])
|
| 192 |
+
original_transform = scene.graph[node_name][0]
|
| 193 |
+
new_transform = np.dot(original_transform, translation)
|
| 194 |
+
scene.graph[node_name] = new_transform
|
| 195 |
+
elif isinstance(scene, trimesh.Trimesh):
|
| 196 |
+
translation = tf.translation_matrix([0, 0.2, 0])
|
| 197 |
+
scene.apply_transform(translation)
|
| 198 |
+
elif animation_type == 'explode':
|
| 199 |
+
# κ° λΆλΆμ μ€μ¬μμ μ½κ° λ©μ΄μ§κ²
|
| 200 |
+
angle = 0
|
| 201 |
+
axis = [0, 1, 0]
|
| 202 |
+
# μ€λΈμ νΈκ° μ¬λ¬ κ°μΌ κ²½μ° μ€μ¬μμ λ°κΉ₯μͺ½μΌλ‘ μ΄λ
|
| 203 |
+
if isinstance(scene, trimesh.Scene):
|
| 204 |
+
# μ¬μ μ€μ¬μ κ³μ°
|
| 205 |
+
all_vertices = []
|
| 206 |
+
for geometry_name, geometry in scene.geometry.items():
|
| 207 |
+
if hasattr(geometry, 'vertices') and len(geometry.vertices) > 0:
|
| 208 |
+
all_vertices.append(geometry.vertices)
|
| 209 |
+
|
| 210 |
+
if all_vertices:
|
| 211 |
+
all_points = np.vstack(all_vertices)
|
| 212 |
+
center = np.mean(all_points, axis=0)
|
| 213 |
+
|
| 214 |
+
for node_name, transform, geometry_name in scene.graph.nodes_geometry:
|
| 215 |
+
# κ° λΆλΆμ μ€μ¬μ κ³μ°
|
| 216 |
+
geometry = scene.geometry[geometry_name]
|
| 217 |
+
if hasattr(geometry, 'centroid'):
|
| 218 |
+
part_center = geometry.centroid
|
| 219 |
+
# μ€μ¬μμ κ°μ²΄ λ°©ν₯ κ³μ°
|
| 220 |
+
direction = part_center - center
|
| 221 |
+
if np.linalg.norm(direction) > 0.001:
|
| 222 |
+
direction = direction / np.linalg.norm(direction)
|
| 223 |
+
# λ°©ν₯μΌλ‘ μ΄λ
|
| 224 |
+
translation = tf.translation_matrix(direction * 0.2)
|
| 225 |
+
original_transform = scene.graph[node_name][0]
|
| 226 |
+
new_transform = np.dot(original_transform, translation)
|
| 227 |
+
scene.graph[node_name] = new_transform
|
| 228 |
+
elif animation_type == 'pulse':
|
| 229 |
+
# λͺ¨λΈμ μ½κ° ν€μ
|
| 230 |
+
scale_factor = 1.2
|
| 231 |
+
|
| 232 |
+
if isinstance(scene, trimesh.Scene):
|
| 233 |
+
# μ¬μ μ€μ¬μ κ³μ°
|
| 234 |
+
all_vertices = []
|
| 235 |
+
for geometry_name, geometry in scene.geometry.items():
|
| 236 |
+
if hasattr(geometry, 'vertices') and len(geometry.vertices) > 0:
|
| 237 |
+
all_vertices.append(geometry.vertices)
|
| 238 |
+
|
| 239 |
+
if all_vertices:
|
| 240 |
+
all_points = np.vstack(all_vertices)
|
| 241 |
+
center = np.mean(all_points, axis=0)
|
| 242 |
+
|
| 243 |
+
for node_name, transform, geometry_name in scene.graph.nodes_geometry:
|
| 244 |
+
# μ€μ¬ κΈ°μ€ μ€μΌμΌλ§
|
| 245 |
+
translate_to_center = tf.translation_matrix(-center)
|
| 246 |
+
scale = np.eye(4)
|
| 247 |
+
scale[:3, :3] *= scale_factor
|
| 248 |
+
translate_back = tf.translation_matrix(center)
|
| 249 |
+
|
| 250 |
+
# λ³ν μ μ©
|
| 251 |
+
original_transform = scene.graph[node_name][0]
|
| 252 |
+
new_transform = np.dot(translate_back, np.dot(scale, np.dot(translate_to_center, original_transform)))
|
| 253 |
+
scene.graph[node_name] = new_transform
|
| 254 |
+
elif isinstance(scene, trimesh.Trimesh):
|
| 255 |
+
# λ©μ μ€μ¬ κΈ°μ€ μ€μΌμΌλ§
|
| 256 |
+
center = scene.centroid
|
| 257 |
+
translate_to_center = tf.translation_matrix(-center)
|
| 258 |
+
scale = tf.scale_matrix(scale_factor)
|
| 259 |
+
translate_back = tf.translation_matrix(center)
|
| 260 |
+
scene.apply_transform(np.dot(translate_back, np.dot(scale, translate_to_center)))
|
| 261 |
+
else:
|
| 262 |
+
# κΈ°λ³Έμ μΌλ‘ νμ μ μ©
|
| 263 |
+
angle = math.pi / 4
|
| 264 |
+
axis = [0, 1, 0]
|
| 265 |
+
|
| 266 |
+
# νμ μ μ© (float, explode, pulse μλ κ²½μ°)
|
| 267 |
+
if angle != 0:
|
| 268 |
+
if isinstance(scene, trimesh.Scene):
|
| 269 |
+
# μ¬μ λͺ¨λ λΆλΆμ νμ μ μ©
|
| 270 |
+
for node_name, transform, geometry_name in scene.graph.nodes_geometry:
|
| 271 |
+
rotation = tf.rotation_matrix(angle, axis)
|
| 272 |
+
original_transform = scene.graph[node_name][0]
|
| 273 |
+
new_transform = np.dot(original_transform, rotation)
|
| 274 |
+
scene.graph[node_name] = new_transform
|
| 275 |
+
elif isinstance(scene, trimesh.Trimesh):
|
| 276 |
+
# λ¨μΌ λ©μμ νμ μ μ©
|
| 277 |
+
rotation = tf.rotation_matrix(angle, axis)
|
| 278 |
+
scene.apply_transform(rotation)
|
| 279 |
+
|
| 280 |
+
# κ²°κ³Ό μ μ₯
|
| 281 |
+
scene.export(output_glb_path)
|
| 282 |
+
print(f"Saved animated GLB to {output_glb_path}")
|
| 283 |
+
return output_glb_path
|
| 284 |
+
except Exception as e:
|
| 285 |
+
print(f"Error creating animated GLB: {str(e)}")
|
| 286 |
+
# μ€λ₯ λ°μ μ μλ³Έ νμΌ λ³΅μ¬
|
| 287 |
+
try:
|
| 288 |
+
import shutil
|
| 289 |
+
output_path = os.path.join(LOG_PATH, f"copy_{os.path.basename(input_glb_path)}")
|
| 290 |
+
shutil.copy(input_glb_path, output_path)
|
| 291 |
+
print(f"Copied original GLB to {output_path}")
|
| 292 |
+
return output_path
|
| 293 |
+
except Exception as copy_error:
|
| 294 |
+
print(f"Error copying GLB: {copy_error}")
|
| 295 |
+
return input_glb_path # μλ³Έ κ²½λ‘ λ°ν
|
| 296 |
+
|
| 297 |
@spaces.GPU
|
| 298 |
def process_3d_model(input_3d, animation_type, animation_duration, fps):
|
| 299 |
"""Process a 3D model and apply animation"""
|
| 300 |
print(f"Processing: {input_3d} with animation type: {animation_type}")
|
| 301 |
|
| 302 |
try:
|
| 303 |
+
# 1. μ
λ‘λλ GLB νμΌμ μ€μ μ λλ©μ΄μ
μ μ©
|
| 304 |
+
animated_glb_path = create_glb_with_animation(
|
| 305 |
+
input_3d,
|
| 306 |
+
animation_type,
|
| 307 |
+
animation_duration,
|
| 308 |
+
fps
|
| 309 |
+
)
|
| 310 |
+
|
| 311 |
+
# 2. ν
μ€νΈ κΈ°λ° μ λλ©μ΄μ
GIF μμ± (λ°±μ
μ©)
|
| 312 |
base_filename = os.path.basename(input_3d).rsplit('.', 1)[0]
|
| 313 |
text_gif_path = os.path.join(LOG_PATH, f'text_animated_{base_filename}.gif')
|
| 314 |
animated_gif_path = create_textual_animation_gif(
|
|
|
|
| 319 |
fps
|
| 320 |
)
|
| 321 |
|
| 322 |
+
# 3. λ©νλ°μ΄ν° μμ±
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
metadata = {
|
| 324 |
"animation_type": animation_type,
|
| 325 |
"duration": animation_duration,
|