tstone87 commited on
Commit
e57fc59
·
verified ·
1 Parent(s): 98c55ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -21
app.py CHANGED
@@ -9,6 +9,14 @@ import imageio
9
  from ultralytics import YOLO
10
  from huggingface_hub import hf_hub_download
11
 
 
 
 
 
 
 
 
 
12
  ###############################################################################
13
  # Helper: Embed an HTML5 video that autoplays (muted) with controls.
14
  ###############################################################################
@@ -39,25 +47,24 @@ if "shortened_video_ready" not in st.session_state:
39
  ###############################################################################
40
  # Download YOLO Model from Hugging Face
41
  ###############################################################################
42
- repo_id = "tstone87/ccr-colorado"
43
  model_filename = "best.pt"
44
 
45
  try:
46
- local_model_path = hf_hub_download(repo_id=repo_id, filename=model_filename)
 
 
 
 
 
47
  model = YOLO(local_model_path)
48
  except Exception as ex:
49
- st.error(f"Unable to load model. Check model path: {model_filename}")
50
- st.error(ex)
51
-
52
- ###############################################################################
53
- # Configure Streamlit Page Layout
54
- ###############################################################################
55
- st.set_page_config(
56
- page_title="Fire Detection: Original vs. Processed Video",
57
- page_icon="🔥",
58
- layout="wide",
59
- initial_sidebar_state="expanded"
60
- )
61
 
62
  ###############################################################################
63
  # SIDEBAR: Video input options
@@ -79,7 +86,7 @@ with st.sidebar:
79
  )
80
  progress_text = st.empty()
81
  progress_bar = st.progress(0)
82
- download_placeholder = st.empty() # Placeholder for download button
83
 
84
  ###############################################################################
85
  # MAIN TITLE
@@ -93,7 +100,6 @@ original_video_data = None
93
  processed_video_data = None
94
 
95
  if example_option != "None":
96
- # Use example videos from remote URLs.
97
  example_videos = {
98
  "T Example": ("T1.mp4", "T2.mpg"),
99
  "LA Example": ("LA1.mp4", "LA2.mp4")
@@ -107,9 +113,8 @@ if example_option != "None":
107
  original_video_data = requests.get(orig_url).content
108
  processed_video_data = requests.get(proc_url).content
109
  except Exception as ex:
110
- st.error("Error loading example videos. Check your URLs.")
111
  else:
112
- # No example selected. If a file is uploaded, use it.
113
  if source_file:
114
  file_type = source_file.type.split('/')[0]
115
  if file_type == 'image':
@@ -150,7 +155,7 @@ with col2:
150
  # Process Video if No Example is Selected
151
  ###############################################################################
152
  if example_option == "None" and source_file and source_file.type.split('/')[0] != 'image':
153
- if st.sidebar.button("Let's Detect Wildfire"):
154
  st.session_state["processed_frames"] = []
155
  processed_frames = st.session_state["processed_frames"]
156
 
@@ -176,7 +181,7 @@ if example_option == "None" and source_file and source_file.type.split('/')[0] !
176
 
177
  if processed_frames:
178
  temp_video_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
179
- fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Use widely supported codec
180
  out = cv2.VideoWriter(temp_video_file.name, fourcc, fps, (width, height))
181
 
182
  for frame in processed_frames:
@@ -199,4 +204,4 @@ if st.session_state["shortened_video_ready"]:
199
  data=st.session_state["shortened_video_data"],
200
  file_name="processed_video.mp4",
201
  mime="video/mp4"
202
- )
 
9
  from ultralytics import YOLO
10
  from huggingface_hub import hf_hub_download
11
 
12
+ # Move set_page_config to the very top as it must be the first Streamlit command
13
+ st.set_page_config(
14
+ page_title="Fire Detection: Original vs. Processed Video",
15
+ page_icon="🔥",
16
+ layout="wide",
17
+ initial_sidebar_state="expanded"
18
+ )
19
+
20
  ###############################################################################
21
  # Helper: Embed an HTML5 video that autoplays (muted) with controls.
22
  ###############################################################################
 
47
  ###############################################################################
48
  # Download YOLO Model from Hugging Face
49
  ###############################################################################
50
+ repo_id = "tstone87/ccr-colorado" # Verify this is correct
51
  model_filename = "best.pt"
52
 
53
  try:
54
+ # Add error handling for repository access
55
+ local_model_path = hf_hub_download(
56
+ repo_id=repo_id,
57
+ filename=model_filename,
58
+ repo_type="model" # Explicitly specify repo_type
59
+ )
60
  model = YOLO(local_model_path)
61
  except Exception as ex:
62
+ st.error(f"Unable to load model. Error details: {str(ex)}")
63
+ st.error("Please verify:")
64
+ st.error("1. Repository 'tstone87/ccr-colorado' exists and is accessible")
65
+ st.error("2. 'best.pt' file is uploaded to the repository")
66
+ st.error("3. You have proper access permissions")
67
+ model = None # Set model to None if loading fails
 
 
 
 
 
 
68
 
69
  ###############################################################################
70
  # SIDEBAR: Video input options
 
86
  )
87
  progress_text = st.empty()
88
  progress_bar = st.progress(0)
89
+ download_placeholder = st.empty()
90
 
91
  ###############################################################################
92
  # MAIN TITLE
 
100
  processed_video_data = None
101
 
102
  if example_option != "None":
 
103
  example_videos = {
104
  "T Example": ("T1.mp4", "T2.mpg"),
105
  "LA Example": ("LA1.mp4", "LA2.mp4")
 
113
  original_video_data = requests.get(orig_url).content
114
  processed_video_data = requests.get(proc_url).content
115
  except Exception as ex:
116
+ st.error(f"Error loading example videos: {str(ex)}")
117
  else:
 
118
  if source_file:
119
  file_type = source_file.type.split('/')[0]
120
  if file_type == 'image':
 
155
  # Process Video if No Example is Selected
156
  ###############################################################################
157
  if example_option == "None" and source_file and source_file.type.split('/')[0] != 'image':
158
+ if st.sidebar.button("Let's Detect Wildfire") and model is not None:
159
  st.session_state["processed_frames"] = []
160
  processed_frames = st.session_state["processed_frames"]
161
 
 
181
 
182
  if processed_frames:
183
  temp_video_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
184
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
185
  out = cv2.VideoWriter(temp_video_file.name, fourcc, fps, (width, height))
186
 
187
  for frame in processed_frames:
 
204
  data=st.session_state["shortened_video_data"],
205
  file_name="processed_video.mp4",
206
  mime="video/mp4"
207
+ )