bhacquin commited on
Commit
1989e82
·
verified ·
1 Parent(s): b62db4d

Update demo.py

Browse files
Files changed (1) hide show
  1. demo.py +47 -47
demo.py CHANGED
@@ -21,7 +21,7 @@ VIDEO_FOLDERS = {
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_2": "./videos/samples_save_myomni_walk_sdf_3/",
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/",
@@ -32,7 +32,7 @@ VIDEO_FOLDERS = {
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 # Assuming sample0.mp4 to sample199.mp4
36
 
37
  #################################################################################################################################################
38
  # Authentication
@@ -59,22 +59,21 @@ except Exception as e:
59
 
60
  class Experiment(dict):
61
  """
62
- Represents an experiment consisting of two 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
-
71
  selected_video=selected_video,
72
  )
73
 
74
  def experiment_to_dict(experiment, skip=False):
75
  """
76
  Converts the Experiment object to a dictionary for Firebase storage.
77
- Includes whether the selected video is from 'omni' or 'ours' folder.
78
  """
79
  info = {
80
  "dataset": experiment["dataset"],
@@ -92,10 +91,10 @@ def experiment_to_dict(experiment, skip=False):
92
  # Determine "Control" based on which video was selected
93
  if experiment["selected_video"] == "left":
94
  selected_video_path = Path(experiment["video_left"])
95
- elif experiment["selected_video"] == "right":
96
- selected_video_path = Path(experiment["video_right"])
97
  elif experiment["selected_video"] == "middle":
98
  selected_video_path = Path(experiment["video_middle"])
 
 
99
  else:
100
  selected_video_path = None
101
 
@@ -106,7 +105,7 @@ def experiment_to_dict(experiment, skip=False):
106
  elif "sum" in selected_video_path.parts:
107
  info["Control"] = "SUM"
108
  elif "ours" in selected_video_path.parts:
109
- info["Control"] = "SUM"
110
  else:
111
  info["Control"] = "Unknown"
112
  else:
@@ -118,35 +117,37 @@ def experiment_to_dict(experiment, skip=False):
118
 
119
  def generate_new_experiment() -> Experiment:
120
  """
121
- Generates a new experiment by randomly selecting a sample number and fetching corresponding videos from both folders.
122
- The positions of the videos (left/right) are randomized.
123
  """
124
- # Randomly select an integer between 0 and 199 inclusive
125
-
126
  random_action = random.choice(["walk", "run"])
127
- random_folder = random.randint(1,4)
128
- if random_folder == 1:
129
- random_int = random.randint(0, 10)
130
- else:
131
- random_int = random.randint(0, NUMBER_OF_EXPERIMENTS - 1)
132
  video_name = f"sample00_rep{random_int:02d}.mp4"
133
 
134
- # Paths to the two videos
135
- video_ours_path = Path(VIDEO_FOLDERS[f"ours_{random_action}_{random_folder}"]) / video_name
136
- video_sdf_path = Path(VIDEO_FOLDERS[f"sdf_{random_action}_{random_folder}"]) / video_name
137
- video_sum_path = Path(VIDEO_FOLDERS[f"sum_{random_action}_{random_folder}"]) / video_name
 
 
 
 
138
  videos = [video_ours_path, video_sdf_path, video_sum_path]
139
- # Ensure that both videos exist
140
- if not video_ours_path.exists() or not video_sdf_path.exists() or not video_sum_path.exists():
141
- raise FileNotFoundError(f"One or both video files not found: {video_ours_path}, {video_sdf_path}, {video_sum_path}")
142
-
143
- # Randomly decide the order (left/right/middle)
144
- # if random.choice([True, False]):
145
- # video_left, video_right = video_ours_path, video_omni_path
146
- # else:
147
- # video_left, video_right = video_omni_path, video_ours_path
148
- video_left, video_middle, video_right = random.shuffle(videos)
149
- print(f"Generated new experiment with left video: {video_left}, right video: {video_right}")
150
  return Experiment(
151
  DATASET,
152
  str(video_left),
@@ -317,7 +318,7 @@ with gr.Blocks(title="Unsupervised Video Editing", css=css) as demo:
317
  message_component = gr.Markdown("") # For displaying messages
318
 
319
  with gr.Row():
320
- # Define both video components first
321
  with gr.Column(scale=1):
322
  video_left_component = gr.Video(
323
  label="left",
@@ -355,15 +356,14 @@ with gr.Blocks(title="Unsupervised Video Editing", css=css) as demo:
355
  with gr.Column(scale=1):
356
  select_right = gr.Button("Select Right Video", elem_id="select-button-right", variant="primary")
357
 
358
- with gr.Row():
359
- # Configure the skip button
360
- btn_skip.click(
361
- fn=skip_experiment,
362
- inputs=[experiment, message_component],
363
- outputs=[experiment, video_left_component, video_middle_component, video_right_component, message_component],
364
- queue=True,
365
- show_progress=True
366
- )
367
 
368
  # Configure the select buttons to handle selection and load next experiment
369
  select_left.click(
@@ -374,7 +374,7 @@ with gr.Blocks(title="Unsupervised Video Editing", css=css) as demo:
374
  show_progress=True
375
  )
376
  select_middle.click(
377
- fn=lambda exp, msg: select_and_load("left", exp, msg),
378
  inputs=[experiment, message_component],
379
  outputs=[experiment, video_left_component, video_middle_component, video_right_component, message_component],
380
  queue=True,
@@ -392,8 +392,8 @@ with gr.Blocks(title="Unsupervised Video Editing", css=css) as demo:
392
  demo.load(
393
  fn=load_initial,
394
  inputs=None,
395
- outputs=[experiment, video_left_component,video_middle_component, video_right_component, message_component]
396
  )
397
 
398
- # Launch the app with share=True to create a public link
399
- demo.launch(share=True)
 
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/",
 
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
 
59
 
60
  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
  )
72
 
73
  def experiment_to_dict(experiment, skip=False):
74
  """
75
  Converts the Experiment object to a dictionary for Firebase storage.
76
+ Includes whether the selected video is from 'sdf', 'sum', or 'ours' folder.
77
  """
78
  info = {
79
  "dataset": experiment["dataset"],
 
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:
99
  selected_video_path = None
100
 
 
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:
 
117
 
118
  def generate_new_experiment() -> Experiment:
119
  """
120
+ Generates a new experiment by randomly selecting a sample number and fetching corresponding videos from multiple folders.
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
+ random_int = random.randint(0, NUMBER_OF_EXPERIMENTS - 1)
 
129
  video_name = f"sample00_rep{random_int:02d}.mp4"
130
 
131
+ # Paths to the three videos
132
+ try:
133
+ video_ours_path = Path(VIDEO_FOLDERS[f"ours_{random_action}_{random_folder}"]) / video_name
134
+ video_sdf_path = Path(VIDEO_FOLDERS[f"sdf_{random_action}_{random_folder}"]) / video_name
135
+ video_sum_path = Path(VIDEO_FOLDERS[f"sum_{random_action}_{random_folder}"]) / video_name
136
+ except KeyError as e:
137
+ raise KeyError(f"Invalid folder key: {e}")
138
+
139
  videos = [video_ours_path, video_sdf_path, video_sum_path]
140
+
141
+ # Ensure that all videos exist
142
+ missing_videos = [str(v) for v in videos if not v.exists()]
143
+ if missing_videos:
144
+ raise FileNotFoundError(f"The following video files were not found: {', '.join(missing_videos)}")
145
+
146
+ # Shuffle the videos to assign to left, middle, right
147
+ random.shuffle(videos)
148
+ video_left, video_middle, video_right = videos # Correct assignment after shuffling
149
+
150
+ print(f"Generated new experiment with left video: {video_left}, middle video: {video_middle}, right video: {video_right}")
151
  return Experiment(
152
  DATASET,
153
  str(video_left),
 
318
  message_component = gr.Markdown("") # For displaying messages
319
 
320
  with gr.Row():
321
+ # Define three video components first
322
  with gr.Column(scale=1):
323
  video_left_component = gr.Video(
324
  label="left",
 
356
  with gr.Column(scale=1):
357
  select_right = gr.Button("Select Right Video", elem_id="select-button-right", variant="primary")
358
 
359
+ # Configure the skip button
360
+ btn_skip.click(
361
+ fn=skip_experiment,
362
+ inputs=[experiment, message_component],
363
+ outputs=[experiment, video_left_component, video_middle_component, video_right_component, message_component],
364
+ queue=True,
365
+ show_progress=True
366
+ )
 
367
 
368
  # Configure the select buttons to handle selection and load next experiment
369
  select_left.click(
 
374
  show_progress=True
375
  )
376
  select_middle.click(
377
+ fn=lambda exp, msg: select_and_load("middle", exp, msg), # Corrected to "middle"
378
  inputs=[experiment, message_component],
379
  outputs=[experiment, video_left_component, video_middle_component, video_right_component, message_component],
380
  queue=True,
 
392
  demo.load(
393
  fn=load_initial,
394
  inputs=None,
395
+ outputs=[experiment, video_left_component, video_middle_component, video_right_component, message_component]
396
  )
397
 
398
+ # Launch the app without share=True since it's not supported on Hugging Face Spaces
399
+ demo.launch()