bhacquin commited on
Commit
78abc8f
·
verified ·
1 Parent(s): 8222224

Update demo.py

Browse files

Modify code for showing only two videos, from the new folders.

Files changed (1) hide show
  1. demo.py +25 -72
demo.py CHANGED
@@ -13,26 +13,10 @@ from firebase_admin import db, credentials
13
 
14
  # Constants for video display
15
  VIDEO_FOLDERS = {
16
- "ours_walk_1": "./videos/samples_save_myomni_walk_ours/",
17
- "ours_walk_2": "./videos/samples_save_myomni_walk_ours_2/",
18
- "ours_walk_3": "./videos/samples_save_myomni_walk_ours_3/",
19
- "ours_run_1": "./videos/samples_save_myomni_run_ours/",
20
- "ours_run_2": "./videos/samples_save_myomni_run_ours_2/",
21
- "ours_run_3": "./videos/samples_save_myomni_run_ours_3/",
22
- "sdf_walk_1": "./videos/samples_save_myomni_walk_sdf/",
23
- "sdf_walk_2": "./videos/samples_save_myomni_walk_sdf_2/",
24
- "sdf_walk_3": "./videos/samples_save_myomni_walk_sdf_3/", # Fixed duplicate key
25
- "sdf_run_1": "./videos/samples_save_myomni_run_sdf/",
26
- "sdf_run_2": "./videos/samples_save_myomni_run_sdf_2/",
27
- "sdf_run_3": "./videos/samples_save_myomni_run_sdf_3/",
28
- "sum_walk_1": "./videos/samples_save_myomni_walk_sum/",
29
- "sum_walk_2": "./videos/samples_save_myomni_walk_sum_2/",
30
- "sum_walk_3": "./videos/samples_save_myomni_walk_sum_3/",
31
- "sum_run_1": "./videos/samples_save_myomni_run_sum/",
32
- "sum_run_2": "./videos/samples_save_myomni_run_sum_2/",
33
- "sum_run_3": "./videos/samples_save_myomni_run_sum_3/",
34
  }
35
- NUMBER_OF_EXPERIMENTS = 20 # Adjusted to match the user's comment
36
 
37
  #################################################################################################################################################
38
  # Authentication
@@ -61,11 +45,10 @@ class Experiment(dict):
61
  """
62
  Represents an experiment consisting of three videos and the user's selection.
63
  """
64
- def __init__(self, dataset, video_left, video_middle, video_right, selected_video=None):
65
  super().__init__(
66
  dataset=dataset,
67
  video_left=video_left,
68
- video_middle=video_middle,
69
  video_right=video_right,
70
  selected_video=selected_video,
71
  )
@@ -78,7 +61,6 @@ def experiment_to_dict(experiment, skip=False):
78
  info = {
79
  "dataset": experiment["dataset"],
80
  "video_left": experiment["video_left"],
81
- "video_middle": experiment["video_middle"],
82
  "video_right": experiment["video_right"],
83
  }
84
 
@@ -91,8 +73,6 @@ def experiment_to_dict(experiment, skip=False):
91
  # Determine "Control" based on which video was selected
92
  if experiment["selected_video"] == "left":
93
  selected_video_path = Path(experiment["video_left"])
94
- elif experiment["selected_video"] == "middle":
95
- selected_video_path = Path(experiment["video_middle"])
96
  elif experiment["selected_video"] == "right":
97
  selected_video_path = Path(experiment["video_right"])
98
  else:
@@ -100,12 +80,10 @@ def experiment_to_dict(experiment, skip=False):
100
 
101
  if selected_video_path:
102
  # Check the parent folder name to determine the source
103
- if "sdf" in selected_video_path.parts:
104
- info["Control"] = "SDF"
105
- elif "sum" in selected_video_path.parts:
106
- info["Control"] = "SUM"
107
- elif "ours" in selected_video_path.parts:
108
- info["Control"] = "Ours" # Corrected from "SUM" to "Ours"
109
  else:
110
  info["Control"] = "Unknown"
111
  else:
@@ -121,40 +99,39 @@ def generate_new_experiment() -> Experiment:
121
  The positions of the videos (left/middle/right) are randomized.
122
  """
123
  # Randomly select an action and folder
124
- random_action = random.choice(["walk", "run"])
125
- random_folder = random.randint(1, 3) # Adjusted to match folder numbering
126
 
127
  # Generate a two-digit sample number as a string
128
- if random_folder > 1:
129
- random_int = random.randint(0, NUMBER_OF_EXPERIMENTS - 1)
130
- else:
131
- random_int = random.randint(0, 10)
132
- video_name = f"sample00_rep{random_int:02d}.mp4"
133
 
134
  # Paths to the three videos
135
  try:
136
- video_ours_path = Path(VIDEO_FOLDERS[f"ours_{random_action}_{random_folder}"]) / video_name
137
- video_sdf_path = Path(VIDEO_FOLDERS[f"sdf_{random_action}_{random_folder}"]) / video_name
138
- video_sum_path = Path(VIDEO_FOLDERS[f"sum_{random_action}_{random_folder}"]) / video_name
139
  except KeyError as e:
140
  raise KeyError(f"Invalid folder key: {e}")
141
 
142
- videos = [video_ours_path, video_sdf_path, video_sum_path]
143
 
144
  # Ensure that all videos exist
145
  missing_videos = [str(v) for v in videos if not v.exists()]
146
  if missing_videos:
147
  raise FileNotFoundError(f"The following video files were not found: {', '.join(missing_videos)}")
148
 
149
- # Shuffle the videos to assign to left, middle, right
150
  random.shuffle(videos)
151
- video_left, video_middle, video_right = videos # Correct assignment after shuffling
152
 
153
- print(f"Generated new experiment with left video: {video_left}, middle video: {video_middle}, right video: {video_right}")
154
  return Experiment(
155
  DATASET,
156
  str(video_left),
157
- str(video_middle),
158
  str(video_right),
159
  )
160
 
@@ -168,7 +145,6 @@ def load_initial():
168
  return [
169
  new_experiment,
170
  gr.update(value=new_experiment["video_left"]),
171
- gr.update(value=new_experiment["video_middle"]),
172
  gr.update(value=new_experiment["video_right"]),
173
  "" # Empty message
174
  ]
@@ -185,8 +161,6 @@ def select_and_load(selected_label, experiment, message_component):
185
  # Update the experiment's selected video
186
  if selected_label.lower() == "left":
187
  experiment["selected_video"] = "left"
188
- elif selected_label.lower() == "middle":
189
- experiment["selected_video"] = "middle"
190
  elif selected_label.lower() == "right":
191
  experiment["selected_video"] = "right"
192
  else:
@@ -215,7 +189,6 @@ def select_and_load(selected_label, experiment, message_component):
215
  return [
216
  new_experiment,
217
  gr.update(value=new_experiment["video_left"]),
218
- gr.update(value=new_experiment["video_middle"]),
219
  gr.update(value=new_experiment["video_right"]),
220
  new_message
221
  ]
@@ -227,7 +200,6 @@ def select_and_load(selected_label, experiment, message_component):
227
  return [
228
  experiment,
229
  gr.update(value=experiment["video_left"]),
230
- gr.update(value=experiment["video_middle"]),
231
  gr.update(value=experiment["video_right"]),
232
  new_message
233
  ]
@@ -264,7 +236,6 @@ def skip_experiment(experiment, message_component):
264
  return [
265
  new_experiment,
266
  gr.update(value=new_experiment["video_left"]),
267
- gr.update(value=new_experiment["video_middle"]),
268
  gr.update(value=new_experiment["video_right"]),
269
  new_message
270
  ]
@@ -276,7 +247,6 @@ def skip_experiment(experiment, message_component):
276
  return [
277
  experiment,
278
  gr.update(value=experiment["video_left"]),
279
- gr.update(value=experiment["video_middle"]),
280
  gr.update(value=experiment["video_right"]),
281
  new_message
282
  ]
@@ -331,15 +301,6 @@ with gr.Blocks(title="Unsupervised Video Editing", css=css) as demo:
331
  show_share_button=False,
332
  interactive=True
333
  )
334
- with gr.Column(scale=1):
335
- video_middle_component = gr.Video(
336
- label="middle",
337
- elem_id="video-middle",
338
- show_label=False,
339
- show_download_button=False,
340
- show_share_button=False,
341
- interactive=True
342
- )
343
  with gr.Column(scale=1):
344
  video_right_component = gr.Video(
345
  label="right",
@@ -354,8 +315,6 @@ with gr.Blocks(title="Unsupervised Video Editing", css=css) as demo:
354
  # Add Select buttons below each video
355
  with gr.Column(scale=1):
356
  select_left = gr.Button("Select Left Video", elem_id="select-button-left", variant="primary")
357
- with gr.Column(scale=1):
358
- select_middle = gr.Button("Select Middle Video", elem_id="select-button-middle", variant="primary")
359
  with gr.Column(scale=1):
360
  select_right = gr.Button("Select Right Video", elem_id="select-button-right", variant="primary")
361
 
@@ -363,7 +322,7 @@ with gr.Blocks(title="Unsupervised Video Editing", css=css) as demo:
363
  btn_skip.click(
364
  fn=skip_experiment,
365
  inputs=[experiment, message_component],
366
- outputs=[experiment, video_left_component, video_middle_component, video_right_component, message_component],
367
  queue=True,
368
  show_progress=True
369
  )
@@ -376,13 +335,7 @@ with gr.Blocks(title="Unsupervised Video Editing", css=css) as demo:
376
  queue=True,
377
  show_progress=True
378
  )
379
- select_middle.click(
380
- fn=lambda exp, msg: select_and_load("middle", exp, msg), # Corrected to "middle"
381
- inputs=[experiment, message_component],
382
- outputs=[experiment, video_left_component, video_middle_component, video_right_component, message_component],
383
- queue=True,
384
- show_progress=True
385
- )
386
  select_right.click(
387
  fn=lambda exp, msg: select_and_load("right", exp, msg),
388
  inputs=[experiment, message_component],
@@ -395,7 +348,7 @@ with gr.Blocks(title="Unsupervised Video Editing", css=css) as demo:
395
  demo.load(
396
  fn=load_initial,
397
  inputs=None,
398
- outputs=[experiment, video_left_component, video_middle_component, video_right_component, message_component]
399
  )
400
 
401
  # Launch the app without share=True since it's not supported on Hugging Face Spaces
 
13
 
14
  # Constants for video display
15
  VIDEO_FOLDERS = {
16
+ "ours": "./ours/",
17
+ "theirs": "./3d/",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  }
19
+ NUMBER_OF_EXPERIMENTS = 16 # Adjusted to match the user's comment
20
 
21
  #################################################################################################################################################
22
  # Authentication
 
45
  """
46
  Represents an experiment consisting of three videos and the user's selection.
47
  """
48
+ def __init__(self, dataset, video_left, video_right, selected_video=None):
49
  super().__init__(
50
  dataset=dataset,
51
  video_left=video_left,
 
52
  video_right=video_right,
53
  selected_video=selected_video,
54
  )
 
61
  info = {
62
  "dataset": experiment["dataset"],
63
  "video_left": experiment["video_left"],
 
64
  "video_right": experiment["video_right"],
65
  }
66
 
 
73
  # Determine "Control" based on which video was selected
74
  if experiment["selected_video"] == "left":
75
  selected_video_path = Path(experiment["video_left"])
 
 
76
  elif experiment["selected_video"] == "right":
77
  selected_video_path = Path(experiment["video_right"])
78
  else:
 
80
 
81
  if selected_video_path:
82
  # Check the parent folder name to determine the source
83
+ if "ours" in selected_video_path.parts:
84
+ info["Control"] = "Ours"
85
+ elif "3d" in selected_video_path.parts:
86
+ info["Control"] = "Theirs"
 
 
87
  else:
88
  info["Control"] = "Unknown"
89
  else:
 
99
  The positions of the videos (left/middle/right) are randomized.
100
  """
101
  # Randomly select an action and folder
102
+
103
+ random_videos = random.randint(1, 16) # Adjusted to match folder numbering
104
 
105
  # Generate a two-digit sample number as a string
106
+ # if random_folder > 1:
107
+ # random_int = random.randint(0, NUMBER_OF_EXPERIMENTS - 1)
108
+ # else:
109
+ # random_int = random.randint(0, 10)
110
+ video_name = f"{random_videos:02d}.mov"
111
 
112
  # Paths to the three videos
113
  try:
114
+ video_ours_path = Path(VIDEO_FOLDERS["ours"]) / video_name
115
+ video_theirs_path = Path(VIDEO_FOLDERS["theirs"]) / video_name
116
+ # video_sum_path = Path(VIDEO_FOLDERS[f"sum_{random_action}_{random_folder}"]) / video_name
117
  except KeyError as e:
118
  raise KeyError(f"Invalid folder key: {e}")
119
 
120
+ videos = [video_ours_path, video_theirs_path]
121
 
122
  # Ensure that all videos exist
123
  missing_videos = [str(v) for v in videos if not v.exists()]
124
  if missing_videos:
125
  raise FileNotFoundError(f"The following video files were not found: {', '.join(missing_videos)}")
126
 
127
+ # Shuffle the videos to assign to left, right
128
  random.shuffle(videos)
129
+ video_left, video_right = videos # Correct assignment after shuffling
130
 
131
+ print(f"Generated new experiment with left video: {video_left}, right video: {video_right}")
132
  return Experiment(
133
  DATASET,
134
  str(video_left),
 
135
  str(video_right),
136
  )
137
 
 
145
  return [
146
  new_experiment,
147
  gr.update(value=new_experiment["video_left"]),
 
148
  gr.update(value=new_experiment["video_right"]),
149
  "" # Empty message
150
  ]
 
161
  # Update the experiment's selected video
162
  if selected_label.lower() == "left":
163
  experiment["selected_video"] = "left"
 
 
164
  elif selected_label.lower() == "right":
165
  experiment["selected_video"] = "right"
166
  else:
 
189
  return [
190
  new_experiment,
191
  gr.update(value=new_experiment["video_left"]),
 
192
  gr.update(value=new_experiment["video_right"]),
193
  new_message
194
  ]
 
200
  return [
201
  experiment,
202
  gr.update(value=experiment["video_left"]),
 
203
  gr.update(value=experiment["video_right"]),
204
  new_message
205
  ]
 
236
  return [
237
  new_experiment,
238
  gr.update(value=new_experiment["video_left"]),
 
239
  gr.update(value=new_experiment["video_right"]),
240
  new_message
241
  ]
 
247
  return [
248
  experiment,
249
  gr.update(value=experiment["video_left"]),
 
250
  gr.update(value=experiment["video_right"]),
251
  new_message
252
  ]
 
301
  show_share_button=False,
302
  interactive=True
303
  )
 
 
 
 
 
 
 
 
 
304
  with gr.Column(scale=1):
305
  video_right_component = gr.Video(
306
  label="right",
 
315
  # Add Select buttons below each video
316
  with gr.Column(scale=1):
317
  select_left = gr.Button("Select Left Video", elem_id="select-button-left", variant="primary")
 
 
318
  with gr.Column(scale=1):
319
  select_right = gr.Button("Select Right Video", elem_id="select-button-right", variant="primary")
320
 
 
322
  btn_skip.click(
323
  fn=skip_experiment,
324
  inputs=[experiment, message_component],
325
+ outputs=[experiment, video_left_component, video_right_component, message_component],
326
  queue=True,
327
  show_progress=True
328
  )
 
335
  queue=True,
336
  show_progress=True
337
  )
338
+
 
 
 
 
 
 
339
  select_right.click(
340
  fn=lambda exp, msg: select_and_load("right", exp, msg),
341
  inputs=[experiment, message_component],
 
348
  demo.load(
349
  fn=load_initial,
350
  inputs=None,
351
+ outputs=[experiment, video_left_component, video_right_component, message_component]
352
  )
353
 
354
  # Launch the app without share=True since it's not supported on Hugging Face Spaces