File size: 11,095 Bytes
6495ab5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import gradio as gr
import os

# 配置视频文件路径和场景信息
SCENES = {
    "basketball": {
        "ego_query": "videos/basketball/basketball_ego_original.mp4",
        "exo_target": "videos/basketball/basketball_exo_original.mp4",
        "objects": ["ball", "hoop"]
    },
    "cooking": {
        "ego_query": "videos/cooking/kitchen_ego_original.mp4",
        "exo_target": "videos/cooking/kitchen_exo_original.mp4",
        "objects": ["food", "knife_block"]
    },
    "health": {
        "ego_query": "videos/health/health_ego_original.mp4",
        "exo_target": "videos/health/health_exo_original.mp4",
        "objects": ["instruction_manual", "transport_box", "test_strip"]
    }
}

def update_scene(scene):
    """更新场景时的回调函数"""
    ego_video = SCENES[scene]["ego_query"]
    exo_video = SCENES[scene]["exo_target"]
    objects = SCENES[scene]["objects"]
    
    # 更新物体选择下拉菜单
    object_dropdown = gr.Dropdown(
        choices=objects,
        value=None,
        label="Select Object",
        interactive=True
    )
    
    return ego_video, exo_video, object_dropdown

def update_videos_with_object(scene, selected_object):
    """选择物体时更新视频"""
    if selected_object is None:
        # 如果没有选择物体,返回原始视频
        ego_video = SCENES[scene]["ego_query"]
        exo_video = SCENES[scene]["exo_target"]
    else:
        # 这里假设有mask视频的命名规则
        # 例如:ball_ego_query_mask.mp4, ball_exo_target_mask.mp4
        ego_video = f"videos/{scene}/{selected_object}_ego_query_mask.mp4"
        exo_video = f"videos/{scene}/{selected_object}_exo_target_mask.mp4"
        
        # 如果mask视频不存在,回退到原始视频
        if not os.path.exists(ego_video):
            ego_video = SCENES[scene]["ego_query"]
        if not os.path.exists(exo_video):
            exo_video = SCENES[scene]["exo_target"]
    
    # 返回视频路径
    return ego_video, exo_video

def sync_video_playback():
    """JavaScript代码:检测两个视频都加载完毕后同步播放"""
    return """
    () => {
        const egoVideo = document.querySelector('#ego_video video');
        const exoVideo = document.querySelector('#exo_video video');
        
        if (!egoVideo || !exoVideo) {
            setTimeout(() => {
                const egoVideo = document.querySelector('#ego_video video');
                const exoVideo = document.querySelector('#exo_video video');
                if (egoVideo && exoVideo) {
                    let egoLoaded = false;
                    let exoLoaded = false;
                    
                    const checkBothLoaded = () => {
                        if (egoLoaded && exoLoaded) {
                            egoVideo.currentTime = 0;
                            exoVideo.currentTime = 0;
                            Promise.all([egoVideo.play(), exoVideo.play()]).then(() => {
                                console.log('Both videos started playing synchronously');
                            }).catch(error => console.error('Error playing videos:', error));
                        }
                    };
                    
                    const onEgoLoaded = () => { egoLoaded = true; checkBothLoaded(); };
                    const onExoLoaded = () => { exoLoaded = true; checkBothLoaded(); };
                    
                    egoVideo.addEventListener('loadeddata', onEgoLoaded, { once: true });
                    exoVideo.addEventListener('loadeddata', onExoLoaded, { once: true });
                    
                    if (egoVideo.readyState >= 2) onEgoLoaded();
                    if (exoVideo.readyState >= 2) onExoLoaded();
                }
            }, 100);
            return;
        }
        
        let egoLoaded = false;
        let exoLoaded = false;
        
        const checkBothLoaded = () => {
            if (egoLoaded && exoLoaded) {
                egoVideo.currentTime = 0;
                exoVideo.currentTime = 0;
                Promise.all([egoVideo.play(), exoVideo.play()]).then(() => {
                    console.log('Both videos started playing synchronously');
                }).catch(error => console.error('Error playing videos:', error));
            }
        };
        
        const onEgoLoaded = () => { egoLoaded = true; checkBothLoaded(); };
        const onExoLoaded = () => { exoLoaded = true; checkBothLoaded(); };
        
        egoVideo.addEventListener('loadeddata', onEgoLoaded, { once: true });
        exoVideo.addEventListener('loadeddata', onExoLoaded, { once: true });
        
        if (egoVideo.readyState >= 2) onEgoLoaded();
        if (exoVideo.readyState >= 2) onExoLoaded();
    }
    """

def clear_loading_message():
    """清除加载消息"""
    return ""

# 创建 Gradio 界面
with gr.Blocks(title="ObjectRelator", theme=gr.themes.Soft()) as demo:
    # 标题
    gr.Markdown("""
    <div style="text-align: center; margin-bottom: 30px;">
        <h1 style="font-size: 3em; color: #2c3e50; margin: 0; text-shadow: 2px 2px 4px rgba(0,0,0,0.1);">
            ✨ ObjectRelator ✨
        </h1>
        <h2 style="font-size: 1.2em; color: #7f8c8d; margin: 10px 0; font-weight: 300;">
            👤 Enabling Cross-View Object Relation Understanding Across Ego-Centric and Exo-Centric Perspectives 📹
        </h2>
    </div>
    """, elem_id="title")
    

    # 控制面板
    with gr.Row(elem_id="control_panel"):
        with gr.Column(scale=1):
            scene_dropdown = gr.Dropdown(
                choices=list(SCENES.keys()),
                value="basketball",
                label="Select Scene",
                interactive=True,
                elem_id="scene_dropdown"
            )
        
        with gr.Column(scale=1):
            object_dropdown = gr.Dropdown(
                choices=SCENES["basketball"]["objects"],
                value=None,
                label="Select Object",
                interactive=True,
                elem_id="object_dropdown"
            )
    
    # 视频显示区域
    with gr.Row(elem_id="video_container"):
        with gr.Column():
            gr.Markdown("""
            <div style="text-align: center; margin-bottom: 15px;">
                <h3 style="color: #34495e; margin: 0; display: flex; align-items: center; justify-content: center;">
                    <span style="margin-right: 8px;">👤</span>
                    Ego View
                    <span style="margin-left: 8px;"></span>
                </h3>
            </div>
            """)
            ego_video = gr.Video(
                value=SCENES["basketball"]["ego_query"],
                autoplay=False,
                loop=True,
                show_download_button=False,
                show_share_button=False,
                elem_id="ego_video"
            )
        
        with gr.Column():
            gr.Markdown("""
            <div style="text-align: center; margin-bottom: 15px;">
                <h3 style="color: #34495e; margin: 0; display: flex; align-items: center; justify-content: center;">
                    <span style="margin-right: 8px;">📹</span>
                    Exo View
                    <span style="margin-left: 8px;"></span>
                </h3>
            </div>
            """)
            exo_video = gr.Video(
                value=SCENES["basketball"]["exo_target"],
                autoplay=False,
                loop=True,
                show_download_button=False,
                show_share_button=False,
                elem_id="exo_video"
            )
    
    # 场景选择事件处理
    scene_dropdown.change(
        fn=update_scene,
        inputs=[scene_dropdown],
        outputs=[ego_video, exo_video, object_dropdown]
    ).then(
        fn=None,
        js=sync_video_playback()
    )
    
    # 物体选择事件处理
    object_dropdown.change(
        fn=update_videos_with_object,
        inputs=[scene_dropdown, object_dropdown],
        outputs=[ego_video, exo_video]
    ).then(
        fn=None,
        js=sync_video_playback()
    )
    
    # 页面加载完成后执行同步播放
    demo.load(fn=None, js=sync_video_playback())

# 自定义CSS样式和初始JavaScript
demo.css = """
/* 全局样式 */
.gradio-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 20px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
}

/* 主内容区域 */
.contain {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    box-shadow: 0 20px 40px rgba(0,0,0,0.1);
    padding: 30px;
    backdrop-filter: blur(10px);
}

/* 标题样式 */
#title {
    margin-bottom: 30px;
}

/* 控制面板样式 */
#control_panel {
    margin-bottom: 25px;
    padding: 20px;
    background: linear-gradient(145deg, #f8f9fa, #e9ecef);
    border-radius: 15px;
    box-shadow: inset 5px 5px 10px #d1d5db, inset -5px -5px 10px #ffffff;
}

/* 下拉菜单样式 */
#scene_dropdown, #object_dropdown {
    margin: 10px;
}

.gr-dropdown {
    border-radius: 10px !important;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1) !important;
    transition: all 0.3s ease !important;
}

.gr-dropdown:hover {
    transform: translateY(-2px) !important;
    box-shadow: 0 6px 12px rgba(0,0,0,0.15) !important;
}

/* 视频容器样式 */
#video_container {
    gap: 30px;
    margin-top: 20px;
}

/* 视频样式 */
video {
    border-radius: 15px !important;
    box-shadow: 0 10px 30px rgba(0,0,0,0.2) !important;
    transition: all 0.3s ease !important;
    border: 3px solid transparent !important;
    background: linear-gradient(white, white) padding-box,
                linear-gradient(45deg, #667eea, #764ba2) border-box !important;
}

video:hover {
    transform: scale(1.02) !important;
    box-shadow: 0 15px 40px rgba(0,0,0,0.3) !important;
}

/* 视频标题样式 */
.gr-markdown h3 {
    background: linear-gradient(45deg, #667eea, #764ba2);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    font-weight: bold;
    text-shadow: none;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .gradio-container {
        padding: 10px;
    }
    
    .contain {
        padding: 20px;
    }
    
    #video_container {
        flex-direction: column;
    }
    
    video {
        width: 100% !important;
    }
}

/* 加载动画 */
@keyframes pulse {
    0% { opacity: 1; }
    50% { opacity: 0.6; }
    100% { opacity: 1; }
}

.loading {
    animation: pulse 1.5s infinite;
}

/* 按钮和交互元素的美化 */
.gr-button {
    border-radius: 10px !important;
    background: linear-gradient(45deg, #667eea, #764ba2) !important;
    border: none !important;
    color: white !important;
    font-weight: 600 !important;
    transition: all 0.3s ease !important;
}

.gr-button:hover {
    transform: translateY(-2px) !important;
    box-shadow: 0 8px 15px rgba(102, 126, 234, 0.3) !important;
}
"""

if __name__ == "__main__":
    demo.launch(share=True)