tstone87 commited on
Commit
2f1733d
·
verified ·
1 Parent(s): b07a4fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -11
app.py CHANGED
@@ -6,6 +6,7 @@ import tempfile
6
  import time
7
  import requests
8
  import numpy as np
 
9
 
10
  # Page Config
11
  st.set_page_config(page_title="WildfireWatch", page_icon="🔥", layout="wide")
@@ -48,7 +49,7 @@ st.markdown(
48
  )
49
 
50
  # Load Model
51
- model_path = 'https://huggingface.co/spaces/tstone87/ccr-colorado/resolve/main/best.pt' # Your updated model
52
  try:
53
  model = YOLO(model_path)
54
  except Exception as ex:
@@ -72,7 +73,7 @@ st.markdown("---")
72
  # Tabs
73
  tabs = st.tabs(["Upload", "Webcam"])
74
 
75
- # Tab 1: Upload (Your original working version)
76
  with tabs[0]:
77
  col1, col2 = st.columns(2)
78
  with col1:
@@ -84,6 +85,9 @@ with tabs[0]:
84
  with col2:
85
  frame_placeholder = st.empty()
86
  status_placeholder = st.empty()
 
 
 
87
  if source_file and st.button("Detect Wildfire", key="upload_detect"):
88
  file_type = source_file.type.split('/')[0]
89
  if file_type == 'image':
@@ -93,29 +97,65 @@ with tabs[0]:
93
  frame_placeholder.image(detected_image, use_column_width=True)
94
  status_placeholder.write(f"Objects detected: {len(res[0].boxes)}")
95
  elif file_type == 'video':
96
- tfile = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
97
- tfile.write(source_file.read())
98
- tfile.close()
99
- vidcap = cv2.VideoCapture(tfile.name)
 
 
 
100
  if not vidcap.isOpened():
101
  status_placeholder.error("Failed to open video file.")
102
  else:
 
 
 
 
 
 
 
 
 
 
 
 
103
  success, frame = vidcap.read()
104
  frame_count = 0
105
  while success:
106
  res = model.predict(frame, conf=confidence)
107
  detected_frame = res[0].plot()[:, :, ::-1]
108
- frame_placeholder.image(detected_frame, use_column_width=True)
109
  status_placeholder.write(f"Frame {frame_count}: Objects detected: {len(res[0].boxes)}")
 
 
 
 
 
 
 
 
 
 
 
 
110
  success, frame = vidcap.read()
111
  frame_count += 1
112
  time.sleep(0.05)
 
113
  vidcap.release()
114
- import os
115
- os.unlink(tfile.name)
 
 
 
 
 
 
 
 
116
  status_placeholder.write(f"Video processing complete. Processed {frame_count} frames.")
117
 
118
- # Tab 2: Webcam (Enhanced with video and image support)
119
  with tabs[1]:
120
  col1, col2 = st.columns([1, 1])
121
  with col1:
@@ -158,7 +198,7 @@ with tabs[1]:
158
  detected_frame = res[0].plot()[:, :, ::-1]
159
  frame_placeholder.image(detected_frame, use_column_width=True)
160
  status_placeholder.write(f"Objects detected: {len(res[0].boxes)}")
161
- time.sleep(0.1) # Fast update for video
162
  except Exception as e:
163
  status_placeholder.error(f"Video error: {e}")
164
  st.session_state.monitoring = False
 
6
  import time
7
  import requests
8
  import numpy as np
9
+ import os
10
 
11
  # Page Config
12
  st.set_page_config(page_title="WildfireWatch", page_icon="🔥", layout="wide")
 
49
  )
50
 
51
  # Load Model
52
+ model_path = 'https://huggingface.co/spaces/tstone87/ccr-colorado/resolve/main/best.pt'
53
  try:
54
  model = YOLO(model_path)
55
  except Exception as ex:
 
73
  # Tabs
74
  tabs = st.tabs(["Upload", "Webcam"])
75
 
76
+ # Tab 1: Upload (With preview and progress)
77
  with tabs[0]:
78
  col1, col2 = st.columns(2)
79
  with col1:
 
85
  with col2:
86
  frame_placeholder = st.empty()
87
  status_placeholder = st.empty()
88
+ progress_placeholder = st.empty() # For progress percentage
89
+ download_placeholder = st.empty()
90
+
91
  if source_file and st.button("Detect Wildfire", key="upload_detect"):
92
  file_type = source_file.type.split('/')[0]
93
  if file_type == 'image':
 
97
  frame_placeholder.image(detected_image, use_column_width=True)
98
  status_placeholder.write(f"Objects detected: {len(res[0].boxes)}")
99
  elif file_type == 'video':
100
+ # Save input video to temporary file
101
+ input_tfile = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
102
+ input_tfile.write(source_file.read())
103
+ input_tfile.close()
104
+
105
+ # Open video to get total frame count
106
+ vidcap = cv2.VideoCapture(input_tfile.name)
107
  if not vidcap.isOpened():
108
  status_placeholder.error("Failed to open video file.")
109
  else:
110
+ total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
111
+ if total_frames <= 0:
112
+ total_frames = None # Fallback if frame count unavailable
113
+
114
+ # Prepare output video
115
+ output_tfile = tempfile.NamedTemporaryFile(delete=False, suffix='_detected.mp4')
116
+ frame_width = int(vidcap.get(cv2.CAP_PROP_FRAME_WIDTH))
117
+ frame_height = int(vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))
118
+ fps = int(vidcap.get(cv2.CAP_PROP_FPS)) or 30
119
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
120
+ out = cv2.VideoWriter(output_tfile.name, fourcc, fps, (frame_width, frame_height))
121
+
122
  success, frame = vidcap.read()
123
  frame_count = 0
124
  while success:
125
  res = model.predict(frame, conf=confidence)
126
  detected_frame = res[0].plot()[:, :, ::-1]
127
+ frame_placeholder.image(detected_frame, use_column_width=True) # Preview
128
  status_placeholder.write(f"Frame {frame_count}: Objects detected: {len(res[0].boxes)}")
129
+
130
+ # Write to output video
131
+ detected_frame_bgr = detected_frame[:, :, ::-1]
132
+ out.write(detected_frame_bgr)
133
+
134
+ # Update progress
135
+ if total_frames:
136
+ progress_percent = (frame_count + 1) / total_frames * 100
137
+ progress_placeholder.write(f"Progress: {progress_percent:.1f}%")
138
+ else:
139
+ progress_placeholder.write(f"Progress: {frame_count} frames processed (total unknown)")
140
+
141
  success, frame = vidcap.read()
142
  frame_count += 1
143
  time.sleep(0.05)
144
+
145
  vidcap.release()
146
+ out.release()
147
+
148
+ os.unlink(input_tfile.name)
149
+ with open(output_tfile.name, 'rb') as f:
150
+ download_placeholder.download_button(
151
+ label="Download Analyzed Video",
152
+ data=f,
153
+ file_name="analyzed_video.mp4",
154
+ mime="video/mp4"
155
+ )
156
  status_placeholder.write(f"Video processing complete. Processed {frame_count} frames.")
157
 
158
+ # Tab 2: Webcam (Unchanged)
159
  with tabs[1]:
160
  col1, col2 = st.columns([1, 1])
161
  with col1:
 
198
  detected_frame = res[0].plot()[:, :, ::-1]
199
  frame_placeholder.image(detected_frame, use_column_width=True)
200
  status_placeholder.write(f"Objects detected: {len(res[0].boxes)}")
201
+ time.sleep(0.1)
202
  except Exception as e:
203
  status_placeholder.error(f"Video error: {e}")
204
  st.session_state.monitoring = False