GenAIJake commited on
Commit
fd758d8
1 Parent(s): 0a7a9be

back to gradio

Browse files
Files changed (2) hide show
  1. __pycache__/framevis.cpython-312.pyc +0 -0
  2. app.py +9 -124
__pycache__/framevis.cpython-312.pyc ADDED
Binary file (23.2 kB). View file
 
app.py CHANGED
@@ -1,84 +1,15 @@
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
- import tempfile
5
- import os
6
  from framevis import FrameVis
7
- import json
8
-
9
- class InteractiveFrameVis(FrameVis):
10
- """Extended FrameVis class that tracks frame positions"""
11
-
12
- def visualize(self, source, nframes, height=None, width=None, direction="horizontal", trim=False, quiet=True):
13
- """Extended visualize method that returns both the visualization and frame data"""
14
- video = cv2.VideoCapture(source)
15
- if not video.isOpened():
16
- raise FileNotFoundError("Source Video Not Found")
17
-
18
- # Calculate frame positions and timestamps
19
- total_frames = video.get(cv2.CAP_PROP_FRAME_COUNT)
20
- fps = video.get(cv2.CAP_PROP_FPS)
21
- keyframe_interval = total_frames / nframes
22
-
23
- # Get the visualization
24
- output_image = super().visualize(source, nframes, height, width, direction, trim, quiet)
25
-
26
- # Calculate frame positions and timestamps
27
- frame_data = []
28
- img_height, img_width = output_image.shape[:2]
29
-
30
- for i in range(nframes):
31
- frame_pos = int(keyframe_interval * (i + 0.5)) # Same calculation as in visualize
32
- timestamp = frame_pos / fps
33
-
34
- if direction == "horizontal":
35
- x_start = (i * img_width) // nframes
36
- x_end = ((i + 1) * img_width) // nframes
37
- frame_info = {
38
- "frame": frame_pos,
39
- "time": timestamp,
40
- "x_start": int(x_start),
41
- "x_end": int(x_end),
42
- "y_start": 0,
43
- "y_end": img_height
44
- }
45
- else: # vertical
46
- y_start = (i * img_height) // nframes
47
- y_end = ((i + 1) * img_height) // nframes
48
- frame_info = {
49
- "frame": frame_pos,
50
- "time": timestamp,
51
- "x_start": 0,
52
- "x_end": img_width,
53
- "y_start": int(y_start),
54
- "y_end": int(y_end)
55
- }
56
- frame_data.append(frame_info)
57
-
58
- video.release()
59
- return output_image, frame_data
60
-
61
- def extract_frame(video_path, frame_number):
62
- """Extract a specific frame from the video"""
63
- if not video_path:
64
- return None
65
-
66
- cap = cv2.VideoCapture(video_path)
67
- cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
68
- ret, frame = cap.read()
69
- cap.release()
70
-
71
- if ret:
72
- return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
73
- return None
74
 
75
  def process_video(video_path, nframes, height, width, direction, trim, average, blur_amount):
76
- """Process video using FrameVis and return the visualization with frame data"""
77
  try:
78
- fv = InteractiveFrameVis()
79
 
80
  # Process the video
81
- output_image, frame_data = fv.visualize(
82
  video_path,
83
  nframes=nframes,
84
  height=height if height > 0 else None,
@@ -96,53 +27,17 @@ def process_video(video_path, nframes, height, width, direction, trim, average,
96
 
97
  # Convert from BGR to RGB for Gradio
98
  output_image = cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB)
99
-
100
- # Store frame data in a temporary file
101
- temp_dir = tempfile.gettempdir()
102
- data_path = os.path.join(temp_dir, "frame_data.json")
103
- with open(data_path, "w") as f:
104
- json.dump({"video_path": video_path, "frames": frame_data}, f)
105
-
106
- return output_image, data_path
107
 
108
  except Exception as e:
109
  raise gr.Error(str(e))
110
 
111
- def on_mouse_move(evt: gr.EventData, frame_data_path):
112
- """Handle mouseover on the visualization image"""
113
- if not frame_data_path:
114
- return None
115
-
116
- try:
117
- # Load frame data
118
- with open(frame_data_path) as f:
119
- data = json.load(f)
120
-
121
- video_path = data["video_path"]
122
- frames = data["frames"]
123
-
124
- # Get mouse coordinates
125
- x, y = evt.index[0], evt.index[1] # Extract x, y from index
126
-
127
- # Find which frame was hovered
128
- for frame in frames:
129
- if (frame["x_start"] <= x <= frame["x_end"] and
130
- frame["y_start"] <= y <= frame["y_end"]):
131
- # Extract and return the frame
132
- preview = extract_frame(video_path, frame["frame"])
133
- if preview is not None:
134
- return preview, f"Frame {frame['frame']} (Time: {frame['time']:.2f}s)"
135
-
136
- except Exception as e:
137
- print(f"Error handling mouseover: {e}")
138
- return None, ""
139
-
140
  # Create the Gradio interface
141
  with gr.Blocks(title="FrameVis - Video Frame Visualizer") as demo:
142
  gr.Markdown("""
143
  # 🎬 FrameVis - Video Frame Visualizer
144
  Upload a video to create a beautiful visualization of its frames. The tool will extract frames at regular intervals
145
- and combine them into a single image. **Click anywhere on the visualization to see the original frames!**
146
  """)
147
 
148
  with gr.Row():
@@ -168,14 +63,11 @@ with gr.Blocks(title="FrameVis - Video Frame Visualizer") as demo:
168
  process_btn = gr.Button("Generate Visualization", variant="primary")
169
 
170
  with gr.Column(scale=2):
171
- # Output components
172
- frame_data = gr.State() # Hidden component to store frame data
173
- output_image = gr.Image(label="Visualization Result", tool="select", height=300)
174
- frame_info = gr.Markdown("Click on the visualization to see frame details")
175
- preview_frame = gr.Image(label="Frame Preview", interactive=False, height=300)
176
 
177
  # Handle processing
178
- result = process_btn.click(
179
  fn=process_video,
180
  inputs=[
181
  video_input,
@@ -187,14 +79,7 @@ with gr.Blocks(title="FrameVis - Video Frame Visualizer") as demo:
187
  average,
188
  blur_amount
189
  ],
190
- outputs=[output_image, frame_data]
191
- )
192
-
193
- # Handle selection events
194
- output_image.select(
195
- fn=on_mouse_move,
196
- inputs=[frame_data],
197
- outputs=[preview_frame, frame_info]
198
  )
199
 
200
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
 
 
4
  from framevis import FrameVis
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  def process_video(video_path, nframes, height, width, direction, trim, average, blur_amount):
7
+ """Process video using FrameVis and return the visualization"""
8
  try:
9
+ fv = FrameVis()
10
 
11
  # Process the video
12
+ output_image = fv.visualize(
13
  video_path,
14
  nframes=nframes,
15
  height=height if height > 0 else None,
 
27
 
28
  # Convert from BGR to RGB for Gradio
29
  output_image = cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB)
30
+ return output_image
 
 
 
 
 
 
 
31
 
32
  except Exception as e:
33
  raise gr.Error(str(e))
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  # Create the Gradio interface
36
  with gr.Blocks(title="FrameVis - Video Frame Visualizer") as demo:
37
  gr.Markdown("""
38
  # 🎬 FrameVis - Video Frame Visualizer
39
  Upload a video to create a beautiful visualization of its frames. The tool will extract frames at regular intervals
40
+ and combine them into a single image.
41
  """)
42
 
43
  with gr.Row():
 
63
  process_btn = gr.Button("Generate Visualization", variant="primary")
64
 
65
  with gr.Column(scale=2):
66
+ # Output component
67
+ output_image = gr.Image(label="Visualization Result", height=300)
 
 
 
68
 
69
  # Handle processing
70
+ process_btn.click(
71
  fn=process_video,
72
  inputs=[
73
  video_input,
 
79
  average,
80
  blur_amount
81
  ],
82
+ outputs=output_image
 
 
 
 
 
 
 
83
  )
84
 
85
  if __name__ == "__main__":