File size: 7,385 Bytes
6d69218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import time
from pathlib import Path

import cv2
import gradio as gr
from PIL import Image

from .omni_processor import OmniVideoProcessor


class OmniConverterUI:
    def __init__(self):
        self.processor = OmniVideoProcessor()
        self.default_params = self.processor.params.copy()
        self.max_gallery_items = 20

    def create_interface(self):
        """Create Gradio interface"""
        with gr.Blocks(title="Omnidirectional Video to Pinhole Converter") as demo:
            gr.Markdown("## Omnidirectional Video to Pinhole Converter")

            with gr.Row():
                with gr.Column():
                    # Video input
                    video_input = gr.File(label="Upload Video", type="filepath")

                    # Submit button
                    submit_btn = gr.Button("Convert", variant="primary")

                    # Frame extraction settings
                    with gr.Accordion("Frame Extraction", open=True):
                        frame_interval = gr.Slider(
                            1,
                            100,
                            value=self.default_params["frame_interval"],
                            label="Frame Interval",
                            interactive=True,
                        )

                    # Pinhole camera settings
                    with gr.Accordion("Pinhole Parameters", open=True):
                        with gr.Row():
                            image_width = gr.Slider(
                                100,
                                2000,
                                value=self.default_params["width"],
                                label="Image Width",
                                interactive=True,
                            )
                            image_height = gr.Slider(
                                100,
                                2000,
                                value=self.default_params["height"],
                                label="Image Height",
                                interactive=True,
                            )
                        with gr.Row():
                            cx = gr.Slider(
                                50,
                                1000,
                                value=self.default_params["cx"],
                                label="Principal Point X",
                                interactive=True,
                            )
                            cy = gr.Slider(
                                50,
                                1000,
                                value=self.default_params["cy"],
                                label="Principal Point Y",
                                interactive=True,
                            )
                        with gr.Row():
                            fov_h = gr.Slider(
                                30,
                                180,
                                value=self.default_params["fov_h"],
                                label="Horizontal FOV (deg)",
                                interactive=True,
                            )
                            fov_v = gr.Slider(
                                30,
                                180,
                                value=self.default_params["fov_v"],
                                label="Vertical FOV (deg)",
                                interactive=True,
                            )
                        with gr.Row():
                            fx = gr.Slider(
                                50,
                                1000,
                                value=self.default_params["fx"],
                                label="Focal Length X",
                                interactive=True,
                            )
                            fy = gr.Slider(
                                50,
                                1000,
                                value=self.default_params["fy"],
                                label="Focal Length Y",
                                interactive=True,
                            )

                    # View selection
                    with gr.Accordion("Custom View editions", open=False):
                        with gr.Row():
                            custom_pitch = gr.Slider(-90, 90, value=0, label="Custom Pitch")
                            custom_yaw = gr.Slider(-180, 180, value=0, label="Custom Yaw")
                        add_custom = gr.Button("Add Custom View")

                with gr.Column():
                    # Results display
                    output_gallery = gr.Gallery(
                        label="Generated Pinhole Images",
                        columns=len(self.default_params["views"]),  # Use initial value
                        object_fit="contain",
                        height="auto",
                    )
                    view_state_display = gr.JSON(
                        label="Current Views",
                        value=self.default_params["views"].copy(),
                    )

            # Initialize views state
            views_state = gr.State(self.default_params["views"].copy())

            # Event handlers
            add_custom.click(
                fn=self._update_views,
                inputs=[custom_pitch, custom_yaw, views_state],
                outputs=[views_state, view_state_display],
            )

            submit_btn.click(
                fn=self._run_conversion,
                inputs=[
                    video_input,
                    frame_interval,
                    fx,
                    fy,
                    cx,
                    cy,
                    image_width,
                    image_height,
                    fov_h,
                    fov_v,
                    views_state,
                ],
                outputs=output_gallery,
            )

        return demo

    def _update_views(self, pitch, yaw, current_views):
        """Update views state with new custom view"""
        new_views = {**current_views, f"pitch_{pitch}_yaw_{yaw}": (pitch, yaw)}
        return new_views, new_views

    def _run_conversion(self, video_file, *params):
        """Run conversion with progress tracking"""
        param_names = [
            "frame_interval",
            "fx",
            "fy",
            "cx",
            "cy",
            "width",
            "height",
            "fov_h",
            "fov_v",
            "views",
        ]
        params_dict = dict(zip(param_names, params))

        self.processor.set_params(params_dict)

        output_dir = Path.cwd() / "outputs" / time.strftime("%Y%m%d%H%M%S")
        output_dir.mkdir(parents=True, exist_ok=True)
        pano_images, pinhole_images_data = self.processor.process_video(video_file.name, output_dir)
        image_list_for_gallery = [
            (
                Image.fromarray(cv2.cvtColor(img_info["image"], cv2.COLOR_BGR2RGB)),
                "Frame {}, View: {}".format(img_info["pano_index"], img_info["view_name"]),
            )
            for img_info in pinhole_images_data
        ][: self.max_gallery_items]
        if not image_list_for_gallery:
            return gr.update(value=[], visible=False)
        return gr.update(columns=len(params_dict["views"]), value=image_list_for_gallery)