tstone87 commited on
Commit
ebb9e28
·
verified ·
1 Parent(s): 086ae8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -26
app.py CHANGED
@@ -30,7 +30,7 @@ with st.sidebar:
30
  )
31
  progress_text = st.empty()
32
  progress_bar = st.progress(0)
33
- # A container for our frame slider (viewer)
34
  slider_container = st.empty()
35
 
36
  # Main page header and intro images
@@ -67,11 +67,11 @@ except Exception as ex:
67
  st.error(f"Unable to load model. Check the specified path: {model_path}")
68
  st.error(ex)
69
 
70
- # Initialize session state for frame viewer if not already set
71
- if 'viewer_frame' not in st.session_state:
72
- st.session_state.viewer_frame = 0
73
 
74
- # This container will display the currently viewed frame
75
  viewer_slot = st.empty()
76
 
77
  # When the user clicks the detect button...
@@ -99,7 +99,7 @@ if st.sidebar.button("Let's Detect Wildfire"):
99
  width = int(vidcap.get(cv2.CAP_PROP_FRAME_WIDTH))
100
  height = int(vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))
101
 
102
- # Determine sampling interval and output fps based on option selected.
103
  if video_option == "Original FPS":
104
  sample_interval = 1
105
  output_fps = orig_fps
@@ -119,10 +119,6 @@ if st.sidebar.button("Let's Detect Wildfire"):
119
  sample_interval = 1
120
  output_fps = orig_fps
121
 
122
- # Initial slider for frame viewing.
123
- slider_val = st.session_state.viewer_frame
124
- slider = slider_container.slider("Frame Viewer", min_value=0, max_value=0, value=slider_val, step=1, key="frame_slider")
125
-
126
  success, image = vidcap.read()
127
  while success:
128
  if frame_count % sample_interval == 0:
@@ -139,27 +135,37 @@ if st.sidebar.button("Let's Detect Wildfire"):
139
  else:
140
  progress_text.text(f"Processing frame {frame_count}")
141
 
142
- # Update the slider's max value. Preserve current value.
143
- current_index = st.session_state.get("frame_slider", len(processed_frames) - 1)
144
- slider = slider_container.slider("Frame Viewer",
145
- min_value=0,
146
- max_value=len(processed_frames)-1,
147
- value=current_index,
148
- step=1,
149
- key="frame_slider")
150
-
151
- # If the user is at the latest frame, update the viewer.
152
- if st.session_state.frame_slider == len(processed_frames)-1:
153
- viewer_slot.image(processed_frames[-1], caption=f"Frame {len(processed_frames)-1}", use_column_width=True)
154
-
 
 
 
 
 
 
 
 
 
 
155
  frame_count += 1
156
  success, image = vidcap.read()
157
 
158
- # Video processing complete.
159
  progress_text.text("Video processing complete!")
160
  progress_bar.progress(100)
161
 
162
- # After processing, allow downloading the shortened video.
163
  if processed_frames:
164
  temp_video_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
165
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
@@ -167,7 +173,7 @@ if st.sidebar.button("Let's Detect Wildfire"):
167
  for frame in processed_frames:
168
  out.write(frame)
169
  out.release()
170
-
171
  st.success("Shortened video created successfully!")
172
  with open(temp_video_file.name, 'rb') as video_file:
173
  st.download_button(
 
30
  )
31
  progress_text = st.empty()
32
  progress_bar = st.progress(0)
33
+ # Container for our dynamic slider (frame viewer)
34
  slider_container = st.empty()
35
 
36
  # Main page header and intro images
 
67
  st.error(f"Unable to load model. Check the specified path: {model_path}")
68
  st.error(ex)
69
 
70
+ # We'll use a session_state variable to remember the current slider value.
71
+ if "frame_slider" not in st.session_state:
72
+ st.session_state.frame_slider = 0
73
 
74
+ # A container to display the currently viewed frame.
75
  viewer_slot = st.empty()
76
 
77
  # When the user clicks the detect button...
 
99
  width = int(vidcap.get(cv2.CAP_PROP_FRAME_WIDTH))
100
  height = int(vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))
101
 
102
+ # Determine sampling interval and output fps based on the option selected.
103
  if video_option == "Original FPS":
104
  sample_interval = 1
105
  output_fps = orig_fps
 
119
  sample_interval = 1
120
  output_fps = orig_fps
121
 
 
 
 
 
122
  success, image = vidcap.read()
123
  while success:
124
  if frame_count % sample_interval == 0:
 
135
  else:
136
  progress_text.text(f"Processing frame {frame_count}")
137
 
138
+ # Only update slider if we have at least one processed frame.
139
+ if len(processed_frames) > 0:
140
+ # Clear the previous slider widget.
141
+ slider_container.empty()
142
+ # Determine the current slider value.
143
+ curr_slider_val = st.session_state.get("frame_slider", len(processed_frames)-1)
144
+ # Ensure the slider value is within the new bounds.
145
+ if curr_slider_val > len(processed_frames)-1:
146
+ curr_slider_val = len(processed_frames)-1
147
+ # Create a new slider. This slider's key is fixed because we cleared the container beforehand.
148
+ slider_val = slider_container.slider(
149
+ "Frame Viewer",
150
+ min_value=0,
151
+ max_value=len(processed_frames)-1,
152
+ value=curr_slider_val,
153
+ step=1,
154
+ key="frame_slider"
155
+ )
156
+ st.session_state.frame_slider = slider_val
157
+
158
+ # If the user is at the most recent frame, update the viewer.
159
+ if slider_val == len(processed_frames)-1:
160
+ viewer_slot.image(processed_frames[-1], caption=f"Frame {len(processed_frames)-1}", use_column_width=True)
161
  frame_count += 1
162
  success, image = vidcap.read()
163
 
164
+ # Finalize progress.
165
  progress_text.text("Video processing complete!")
166
  progress_bar.progress(100)
167
 
168
+ # Create and provide the downloadable shortened video.
169
  if processed_frames:
170
  temp_video_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
171
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
 
173
  for frame in processed_frames:
174
  out.write(frame)
175
  out.release()
176
+
177
  st.success("Shortened video created successfully!")
178
  with open(temp_video_file.name, 'rb') as video_file:
179
  st.download_button(