tstone87 commited on
Commit
0899559
·
verified ·
1 Parent(s): 24fa59e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -8
app.py CHANGED
@@ -61,7 +61,7 @@ with tabs[0]:
61
  with tabs[1]:
62
  col1, col2 = st.columns([1, 1])
63
  with col1:
64
- st.markdown("**Webcam snapshot or stream**")
65
  webcam_url = st.text_input("URL", "http://<your_webcam_ip>/current.jpg", label_visibility="collapsed")
66
  confidence = st.slider("Confidence", 0.25, 1.0, 0.4, key="webcam_conf")
67
  mode = st.radio("", ["Snapshot", "Stream"], label_visibility="collapsed")
@@ -69,28 +69,50 @@ with tabs[1]:
69
  with col2:
70
  if start:
71
  if mode == "Snapshot":
72
- placeholder = st.empty()
73
  timer_placeholder = st.empty()
74
- refresh_interval = 5 # seconds
75
  while True:
76
  start_time = time.time()
77
  try:
 
78
  response = requests.get(webcam_url, timeout=5)
 
 
 
79
  image_array = np.asarray(bytearray(response.content), dtype=np.uint8)
80
  frame = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
 
 
 
 
 
81
  results = model.predict(frame, conf=confidence)
82
- detected_frame = results[0].plot()[:, :, ::-1]
83
- placeholder.image(detected_frame, use_column_width=True)
 
 
 
 
84
  elapsed = time.time() - start_time
85
  remaining = max(0, refresh_interval - elapsed)
86
  timer_placeholder.write(f"Next refresh in: {int(remaining)}s")
87
- time.sleep(1)
88
- if remaining <= 0:
89
- st.experimental_rerun()
 
 
 
 
 
 
 
 
90
  except Exception as e:
91
  st.error(f"Error: {e}")
92
  break
93
  else:
 
94
  cap = cv2.VideoCapture(webcam_url)
95
  frame_placeholder = st.empty()
96
  while cap.isOpened():
 
61
  with tabs[1]:
62
  col1, col2 = st.columns([1, 1])
63
  with col1:
64
+ st.markdown("**Webcam snapshot**")
65
  webcam_url = st.text_input("URL", "http://<your_webcam_ip>/current.jpg", label_visibility="collapsed")
66
  confidence = st.slider("Confidence", 0.25, 1.0, 0.4, key="webcam_conf")
67
  mode = st.radio("", ["Snapshot", "Stream"], label_visibility="collapsed")
 
69
  with col2:
70
  if start:
71
  if mode == "Snapshot":
72
+ image_placeholder = st.empty()
73
  timer_placeholder = st.empty()
74
+ refresh_interval = 30 # Refresh every 30 seconds
75
  while True:
76
  start_time = time.time()
77
  try:
78
+ # Fetch the webcam image
79
  response = requests.get(webcam_url, timeout=5)
80
+ if response.status_code != 200:
81
+ st.error(f"Failed to fetch image: HTTP {response.status_code}")
82
+ break
83
  image_array = np.asarray(bytearray(response.content), dtype=np.uint8)
84
  frame = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
85
+ if frame is None:
86
+ st.error("Failed to decode image from URL.")
87
+ break
88
+
89
+ # Check for fire/smoke using the YOLO model
90
  results = model.predict(frame, conf=confidence)
91
+ detected_frame = results[0].plot()[:, :, ::-1] # Convert BGR to RGB for display
92
+
93
+ # Display the processed image
94
+ image_placeholder.image(detected_frame, use_column_width=True)
95
+
96
+ # Update timer
97
  elapsed = time.time() - start_time
98
  remaining = max(0, refresh_interval - elapsed)
99
  timer_placeholder.write(f"Next refresh in: {int(remaining)}s")
100
+
101
+ # Wait until 30 seconds have passed, updating the timer
102
+ while remaining > 0:
103
+ time.sleep(1)
104
+ elapsed = time.time() - start_time
105
+ remaining = max(0, refresh_interval - elapsed)
106
+ timer_placeholder.write(f"Next refresh in: {int(remaining)}s")
107
+
108
+ # Refresh the page to fetch a new image
109
+ st.experimental_rerun()
110
+
111
  except Exception as e:
112
  st.error(f"Error: {e}")
113
  break
114
  else:
115
+ # Stream mode (unchanged from previous)
116
  cap = cv2.VideoCapture(webcam_url)
117
  frame_placeholder = st.empty()
118
  while cap.isOpened():