shin0624's picture
Update app.py
bdcadd4 verified
Raw History Blame Contribute Delete
3.28 kB
import gradio as gr
import tempfile
import os
import numpy as np
from detector.frame_sampler import extract_keyframes
from detector.yolo_pipeline import YOLO3DReconstructor
from exporters.webgl_exporter import export_to_webgl
from utils.visualization import FactoryVisualizer
def process_video(video):
with tempfile.TemporaryDirectory() as tmpdir:
print(f"์ž„์‹œ ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ: {tmpdir}") # ๊ฒฝ๋กœ ํ™•์ธ
# 1. ํ‚คํ”„๋ ˆ์ž„ ์ถ”์ถœ
frames_dir = os.path.join(tmpdir, "frames")
os.makedirs(frames_dir, exist_ok=True)
frame_paths = extract_keyframes(video, frames_dir)
if not frame_paths:
raise gr.Error("๋น„๋””์˜ค ํŒŒ์ผ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค!")
# 2. 3D ํฌ์ธํŠธ ํด๋ผ์šฐ๋“œ ์ƒ์„ฑ
reconstructor = YOLO3DReconstructor()
result = reconstructor.process_frames(frame_paths)
if result is None:
raise gr.Error("์˜์ƒ์—์„œ ๊ฐ์ง€ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค!")
pointcloud, detections = result
# 3. ์‹œ๊ฐํ™” ๊ฒฐ๊ณผ ์ƒ์„ฑ
vis_dir = os.path.join(tmpdir, "visualizations")
os.makedirs(vis_dir, exist_ok=True)
# 3D ํฌ์ธํŠธ ํด๋ผ์šฐ๋“œ ์‹œ๊ฐํ™”
pc_points = np.array([point[:3] for point in pointcloud.vertices])
FactoryVisualizer.plot_3d_pointcloud(
pc_points,
save_path=os.path.join(vis_dir, "3d_view.png")
)
# ํƒ‘๋ทฐ ์‹œ๊ฐํ™”
FactoryVisualizer.create_top_down_view(
pc_points,
save_path=os.path.join(vis_dir, "top_view.png")
)
# ํด๋ž˜์Šค ๋ถ„ํฌ ์‹œ๊ฐํ™”
FactoryVisualizer.plot_class_distribution(
detections,
save_path=os.path.join(vis_dir, "distribution.png")
)
# 4. ๋ชจ๋ธ ๋ฐ ๊ฒฐ๊ณผ ํŒŒ์ผ ์ €์žฅ
obj_path = os.path.join(tmpdir, "factory.obj")
if len(pc_points) < 3: # ์ตœ์†Œ 3๊ฐœ ํฌ์ธํŠธ ํ•„์š”
raise gr.Error("ํฌ์ธํŠธ ํด๋ผ์šฐ๋“œ ์ƒ์„ฑ ์‹คํŒจ: ์ถฉ๋ถ„ํ•œ ๋ฐ์ดํ„ฐ ์—†์Œ")
pointcloud.export(obj_path)
html_path = export_to_webgl(obj_path, tmpdir)
print("์ƒ์„ฑ๋œ ํŒŒ์ผ ๋ชฉ๋ก:", os.listdir(tmpdir)) # ํŒŒ์ผ ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ
return (
obj_path,
html_path,
os.path.join(vis_dir, "3d_view.png"),
os.path.join(vis_dir, "top_view.png"),
os.path.join(vis_dir, "distribution.png")
)
iface = gr.Interface(
fn=process_video,
inputs=gr.Video(label="๊ณต์žฅ ์˜์ƒ ์—…๋กœ๋“œ (MP4/AVI)"),
outputs=[
gr.Model3D(label="3D ํฌ์ธํŠธ ํด๋ผ์šฐ๋“œ ๋ชจ๋ธ"),
gr.File(label="WebGL ์ธํ„ฐ๋ž™ํ‹ฐ๋ธŒ ๋ทฐ์–ด"),
gr.Image(label="3D ๊ณต์žฅ ๊ตฌ์กฐ", type="filepath"),
gr.Image(label="ํƒ‘๋ทฐ ๊ตฌ์กฐ๋งต", type="filepath"),
gr.Image(label="๊ฐ์ฒด ๋ถ„ํฌ ๋ถ„์„", type="filepath")
],
title="๐Ÿญ AI ๊ณต์žฅ ๊ตฌ์กฐ ๋ถ„์„ ์‹œ์Šคํ…œ",
description="์—…๋กœ๋“œ๋œ ๊ณต์žฅ ์˜์ƒ์—์„œ 3D ๊ตฌ์กฐ๋งต๊ณผ ๋ถ„์„ ๋ฆฌํฌํŠธ๋ฅผ ์ž๋™ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค",
allow_flagging="never",
examples=[], # ์˜ˆ์‹œ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
)
if __name__ == "__main__":
iface.launch(debug=True)