Spaces:
Sleeping
Sleeping
Download app.py from shin0624/Factory_Layout_Simulator: direct link, hf CLI and curl.
- Browser
- Download file 3.28 kB
-
https://huggingface.co/spaces/shin0624/Factory_Layout_Simulator/resolve/main/app.py
- Command line
-
hf download hf://spaces/shin0624/Factory_Layout_Simulator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/shin0624/Factory_Layout_Simulator/resolve/main/app.py
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) |