File size: 3,282 Bytes
c67582c
 
 
c0cb016
c67582c
 
 
58b4ac9
c67582c
 
 
bdcadd4
c67582c
 
f10a28a
c67582c
 
f10a28a
 
 
c67582c
 
c0cb016
c67582c
c0cb016
f10a28a
c0cb016
 
c67582c
c0cb016
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c67582c
bdcadd4
 
c67582c
 
bdcadd4
 
 
c0cb016
 
 
 
 
 
 
bdcadd4
c67582c
 
 
 
 
c0cb016
 
 
 
 
c67582c
c0cb016
 
 
f10a28a
c67582c
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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)