Iskaj commited on
Commit
ed0180d
1 Parent(s): 58b39ad

add doc to app and plot

Browse files
Files changed (2) hide show
  1. app.py +51 -19
  2. plot.py +1 -0
app.py CHANGED
@@ -9,8 +9,9 @@ from faiss import read_index_binary, write_index_binary
9
  from config import *
10
  from videomatch import index_hashes_for_video, get_decent_distance, \
11
  get_video_index, compare_videos, get_change_points, get_videomatch_df
12
- from plot import plot_comparison, plot_multi_comparison, plot_segment_comparison
13
 
 
14
  logging.basicConfig()
15
  logging.getLogger().setLevel(logging.INFO)
16
 
@@ -18,7 +19,15 @@ def transfer_data_indices_to_temp(temp_path = VIDEO_DIRECTORY, data_path='./data
18
  """ The binary indices created from the .json file are not stored in the temporary directory
19
  This function will load these indices and write them to the temporary directory.
20
  Doing it this way preserves the way to link dynamically downloaded files and the static
21
- files are the same """
 
 
 
 
 
 
 
 
22
  index_files = os.listdir(data_path)
23
  for index_file in index_files:
24
  # Read from static location and write to temp storage
@@ -29,9 +38,14 @@ def compare(url, target):
29
  """ Compare a single url (user submitted) to a single target entry and return the corresponding
30
  figure and decision (.json-esque list of dictionaries)
31
 
32
- args:
33
- - url: User submitted url which will be downloaded and cached
34
- - target: Target entry with a 'url' and 'mp4' attribute
 
 
 
 
 
35
  """
36
  target_title = target['url']
37
  target_mp4 = target['mp4']
@@ -60,66 +74,84 @@ def compare(url, target):
60
  return fig, segment_decision
61
 
62
  def multiple_comparison(url, return_figure=False):
63
- """ Compare a single url (user submitted) to all target entries and return the corresponding
64
- figures and decisions (.json-style list of dictionaries)
 
65
 
66
- args:
67
- - url: User submitted url which will be downloaded and cached
68
- - return_figure: Parameter to decide if to return figures or decision, needed for Gradio plotting """
 
 
 
 
 
69
  # Figure and decision (list of dicts) storage
70
  figures, decisions = [], []
71
  for target in TARGET_ENTRIES:
72
- # Make comparison
73
  fig, segment_decision = compare(url, target)
74
 
75
  # Add decisions to global decision list
76
  decisions.extend(segment_decision)
77
  figures.append(fig)
78
 
 
79
  if return_figure:
80
  return figures
81
  return decisions
82
 
83
  def plot_multiple_comparison(url):
 
 
 
 
 
 
 
 
 
84
  return multiple_comparison(url, return_figure=True)
85
 
 
 
86
  # Write stored target videos to temporary storage
87
  transfer_data_indices_to_temp() # NOTE: Only works after doing 'git lfs pull' to actually obtain the .index files
88
 
89
- # Load stored target videos
90
  with open('apb2022.json', "r") as json_file:
91
  TARGET_ENTRIES = json.load(json_file)
92
 
 
93
  EXAMPLE_VIDEO_URLS = ["https://www.youtube.com/watch?v=qIaqMqMweM4",
94
  "https://drive.google.com/uc?id=1Y1-ypXOvLrp1x0cjAe_hMobCEdA0UbEo&export=download",
95
  "https://video.twimg.com/amplify_video/1575576025651617796/vid/480x852/jP057nPfPJSUM0kR.mp4?tag=14",
96
  "https://drive.google.com/uc?id=1XW0niHR1k09vPNv1cp6NvdGXe7FHJc1D&export=download"]
97
 
 
98
  index_iface = gr.Interface(fn=lambda url: index_hashes_for_video(url).ntotal,
99
  inputs="text",
100
  outputs="text",
101
  examples=EXAMPLE_VIDEO_URLS)
102
 
103
- # compare_iface = gr.Interface(fn=get_comparison,
104
- # inputs=["text", "text", gr.Slider(2, 30, 4, step=2)],
105
- # outputs="plot",
106
- # examples=[[x, example_video_urls[-1]] for x in example_video_urls[:-1]])
107
-
108
  plot_compare_iface = gr.Interface(fn=plot_multiple_comparison,
109
  inputs=["text"],
110
  outputs=[gr.Plot(label=entry['url']) for entry in TARGET_ENTRIES],
111
  examples=EXAMPLE_VIDEO_URLS)
112
 
 
113
  auto_compare_iface = gr.Interface(fn=multiple_comparison,
114
  inputs=["text"],
115
  outputs=["json"],
116
  examples=EXAMPLE_VIDEO_URLS)
117
 
 
118
  iface = gr.TabbedInterface([auto_compare_iface, plot_compare_iface, index_iface], ["AutoCompare", "PlotAutoCompare", "Index"])
119
 
120
  if __name__ == "__main__":
 
121
  import matplotlib
122
- matplotlib.use('SVG') # To be able to plot in gradio
123
 
124
  iface.launch(show_error=True)
125
- #iface.launch(auth=("test", "test"), share=True, debug=True)
 
9
  from config import *
10
  from videomatch import index_hashes_for_video, get_decent_distance, \
11
  get_video_index, compare_videos, get_change_points, get_videomatch_df
12
+ from plot import plot_segment_comparison
13
 
14
+ # Basic logging template only showing info, change to debug during debugging
15
  logging.basicConfig()
16
  logging.getLogger().setLevel(logging.INFO)
17
 
 
19
  """ The binary indices created from the .json file are not stored in the temporary directory
20
  This function will load these indices and write them to the temporary directory.
21
  Doing it this way preserves the way to link dynamically downloaded files and the static
22
+ files are the same.
23
+
24
+ Args:
25
+ temp_path (str): Directory of temporary storage for binary indices.
26
+ data_path (str): Directory of the indices created from the .json file.
27
+
28
+ Returns:
29
+ None.
30
+ """
31
  index_files = os.listdir(data_path)
32
  for index_file in index_files:
33
  # Read from static location and write to temp storage
 
38
  """ Compare a single url (user submitted) to a single target entry and return the corresponding
39
  figure and decision (.json-esque list of dictionaries)
40
 
41
+ Args:
42
+ url (str): User submitted url of a video which will be downloaded and cached.
43
+ target (dict): Target entry with a 'url' and 'mp4' attribute.
44
+
45
+ Returns:
46
+ fig (Figure): Figure that shows the comparison between two videos.
47
+ segment_decisions (dict): JSON-style dictionary containing the decision information of the comparison between two videos.
48
+
49
  """
50
  target_title = target['url']
51
  target_mp4 = target['mp4']
 
74
  return fig, segment_decision
75
 
76
  def multiple_comparison(url, return_figure=False):
77
+ """ Compare a url (user submitted) to all target entries and return the corresponding
78
+ figures and decisions (.json-style list of dictionaries). These target entries are defined in the main
79
+ by loading .json file containing the videos to compare to.
80
 
81
+ Args:
82
+ url (str): User submitted url which will be downloaded and cached.
83
+ return_figure (bool): Toggle parameter to decide if to return figures or decision, needed for Gradio plotting.
84
+
85
+ Returns:
86
+ Either a Figure or a .json-style dictionary with decision information.
87
+
88
+ """
89
  # Figure and decision (list of dicts) storage
90
  figures, decisions = [], []
91
  for target in TARGET_ENTRIES:
92
+ # Make single comparison
93
  fig, segment_decision = compare(url, target)
94
 
95
  # Add decisions to global decision list
96
  decisions.extend(segment_decision)
97
  figures.append(fig)
98
 
99
+ # Return figure or decision
100
  if return_figure:
101
  return figures
102
  return decisions
103
 
104
  def plot_multiple_comparison(url):
105
+ """ Helper function to return figure instead of decisions that is needed for Gradio.
106
+
107
+ Args:
108
+ url (str): User submitted url which will be downloaded and cached.
109
+
110
+ Returns:
111
+ The multiple comparison, but then returning the plots as Figure(s).
112
+
113
+ """
114
  return multiple_comparison(url, return_figure=True)
115
 
116
+
117
+
118
  # Write stored target videos to temporary storage
119
  transfer_data_indices_to_temp() # NOTE: Only works after doing 'git lfs pull' to actually obtain the .index files
120
 
121
+ # Load stored target videos that will be compared to
122
  with open('apb2022.json', "r") as json_file:
123
  TARGET_ENTRIES = json.load(json_file)
124
 
125
+ # Some example videos that can be compared to
126
  EXAMPLE_VIDEO_URLS = ["https://www.youtube.com/watch?v=qIaqMqMweM4",
127
  "https://drive.google.com/uc?id=1Y1-ypXOvLrp1x0cjAe_hMobCEdA0UbEo&export=download",
128
  "https://video.twimg.com/amplify_video/1575576025651617796/vid/480x852/jP057nPfPJSUM0kR.mp4?tag=14",
129
  "https://drive.google.com/uc?id=1XW0niHR1k09vPNv1cp6NvdGXe7FHJc1D&export=download"]
130
 
131
+ # Interface to simply index
132
  index_iface = gr.Interface(fn=lambda url: index_hashes_for_video(url).ntotal,
133
  inputs="text",
134
  outputs="text",
135
  examples=EXAMPLE_VIDEO_URLS)
136
 
137
+ # Interface to plot comparisons
 
 
 
 
138
  plot_compare_iface = gr.Interface(fn=plot_multiple_comparison,
139
  inputs=["text"],
140
  outputs=[gr.Plot(label=entry['url']) for entry in TARGET_ENTRIES],
141
  examples=EXAMPLE_VIDEO_URLS)
142
 
143
+ # Interface to get .json decision list
144
  auto_compare_iface = gr.Interface(fn=multiple_comparison,
145
  inputs=["text"],
146
  outputs=["json"],
147
  examples=EXAMPLE_VIDEO_URLS)
148
 
149
+ # Interface consists of three tabs
150
  iface = gr.TabbedInterface([auto_compare_iface, plot_compare_iface, index_iface], ["AutoCompare", "PlotAutoCompare", "Index"])
151
 
152
  if __name__ == "__main__":
153
+ # To be able to plot in Gradio as we want, these steps are a fix
154
  import matplotlib
155
+ matplotlib.use('SVG')
156
 
157
  iface.launch(show_error=True)
 
plot.py CHANGED
@@ -55,6 +55,7 @@ def plot_segment_comparison(df, change_points, video_mp4 = "Placeholder.mp4", vi
55
  Returns:
56
  fig (Figure): Figure that shows the comparison between two videos.
57
  segment_decisions (dict): JSON-style dictionary containing the decision information of the comparison between two videos.
 
58
  """
59
  # Plot it with certain characteristics
60
  fig, ax_arr = plt.subplots(4, 1, figsize=(16, 6), dpi=300, sharex=True)
 
55
  Returns:
56
  fig (Figure): Figure that shows the comparison between two videos.
57
  segment_decisions (dict): JSON-style dictionary containing the decision information of the comparison between two videos.
58
+
59
  """
60
  # Plot it with certain characteristics
61
  fig, ax_arr = plt.subplots(4, 1, figsize=(16, 6), dpi=300, sharex=True)