Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
"""
|
| 3 |
Created on Tue May 20 11:00:14 2025
|
| 4 |
-
|
| 5 |
@author: ColinWang
|
| 6 |
"""
|
| 7 |
import streamlit as st
|
|
@@ -200,11 +199,64 @@ class VideoTransformer(VideoTransformerBase):
|
|
| 200 |
st.write(f"已捕获快照 {len(self.snapshots)}/20")
|
| 201 |
return img # 返回原始帧以供前端显示
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
# ======================
|
| 204 |
# 照片检测页面
|
| 205 |
# ======================
|
| 206 |
|
| 207 |
def photo_detection_page():
|
|
|
|
| 208 |
st.title("照片检测")
|
| 209 |
st.write("上传一张图片或使用摄像头拍摄,检测是否吸烟,若检测到吸烟则进一步分析性别和年龄。")
|
| 210 |
|
|
@@ -245,9 +297,17 @@ def photo_detection_page():
|
|
| 245 |
age_result = age_detection(image)
|
| 246 |
st.success("The age result is:")
|
| 247 |
st.write(age_result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
# ======================
|
| 250 |
-
#
|
| 251 |
# ======================
|
| 252 |
|
| 253 |
def real_time_detection_page():
|
|
@@ -358,11 +418,13 @@ def real_time_detection_page():
|
|
| 358 |
|
| 359 |
def main():
|
| 360 |
st.sidebar.title("导航")
|
| 361 |
-
page = st.sidebar.selectbox("选择页面", ["照片检测", "实时视频检测"])
|
| 362 |
|
|
|
|
|
|
|
| 363 |
if page == "照片检测":
|
| 364 |
photo_detection_page()
|
| 365 |
-
|
| 366 |
real_time_detection_page()
|
| 367 |
|
| 368 |
if __name__ == "__main__":
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
"""
|
| 3 |
Created on Tue May 20 11:00:14 2025
|
|
|
|
| 4 |
@author: ColinWang
|
| 5 |
"""
|
| 6 |
import streamlit as st
|
|
|
|
| 199 |
st.write(f"已捕获快照 {len(self.snapshots)}/20")
|
| 200 |
return img # 返回原始帧以供前端显示
|
| 201 |
|
| 202 |
+
# ======================
|
| 203 |
+
# Cover Page
|
| 204 |
+
# ======================
|
| 205 |
+
|
| 206 |
+
def cover_page():
|
| 207 |
+
"""Display the cover page with project overview and usage instructions."""
|
| 208 |
+
st.title("Smoking Detection System")
|
| 209 |
+
|
| 210 |
+
st.header("Project Overview")
|
| 211 |
+
st.write("""
|
| 212 |
+
The Smoking Detection System is a Streamlit-based web application designed to detect smoking behavior
|
| 213 |
+
in images or real-time video streams. It leverages advanced machine learning models to classify images
|
| 214 |
+
for smoking activity, gender, and age range. The system is structured to provide both static image analysis
|
| 215 |
+
and real-time video processing, with audio feedback for detected smoking incidents.
|
| 216 |
+
|
| 217 |
+
**Purpose**: The primary goal is to identify smoking behavior in public or controlled environments,
|
| 218 |
+
providing insights into the demographics (gender and age) of individuals engaged in smoking. This can
|
| 219 |
+
be used for monitoring compliance with no-smoking policies or conducting behavioral studies.
|
| 220 |
+
|
| 221 |
+
**Significance**: The application promotes public health by enabling automated monitoring of smoking
|
| 222 |
+
activities, potentially aiding in the enforcement of smoking regulations and raising awareness about
|
| 223 |
+
smoking prevalence across different demographics.
|
| 224 |
+
|
| 225 |
+
**Structure**:
|
| 226 |
+
- **Cover Page**: Provides an overview and usage instructions.
|
| 227 |
+
- **Photo Detection**: Analyzes a single uploaded or captured image for smoking, gender, and age.
|
| 228 |
+
- **Real-Time Video Detection**: Processes video streams, capturing snapshots to detect smoking and
|
| 229 |
+
analyze demographics if smoking is detected.
|
| 230 |
+
""")
|
| 231 |
+
|
| 232 |
+
st.header("Usage Instructions")
|
| 233 |
+
st.write("""
|
| 234 |
+
1. **Navigation**: Use the sidebar to select a page:
|
| 235 |
+
- **Cover Page**: View this project overview.
|
| 236 |
+
- **Photo Detection**: Upload an image or use the camera to capture a photo for analysis.
|
| 237 |
+
- **Real-Time Video Detection**: Enable the webcam for continuous monitoring.
|
| 238 |
+
2. **Photo Detection**:
|
| 239 |
+
- Choose to upload an image or capture one using the camera.
|
| 240 |
+
- The system will classify the image for smoking. If smoking is detected, it will further analyze
|
| 241 |
+
gender and age, and play an audio alert based on the results.
|
| 242 |
+
3. **Real-Time Video Detection**:
|
| 243 |
+
- Start the webcam to capture 20 snapshots over one minute.
|
| 244 |
+
- The system analyzes each snapshot for smoking. If smoking is detected in more than two snapshots,
|
| 245 |
+
it performs gender and age classification and displays the results.
|
| 246 |
+
- An audio alert is played if smoking is confirmed, based on the most common gender and age range.
|
| 247 |
+
4. **Requirements**:
|
| 248 |
+
- Ensure the 'audio' directory contains .wav files named in the format '<age_range> <gender>.wav'
|
| 249 |
+
(e.g., '10-19 male.wav') for audio feedback.
|
| 250 |
+
- Set up Twilio environment variables (TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN) for WebRTC.
|
| 251 |
+
""")
|
| 252 |
+
|
| 253 |
+
|
| 254 |
# ======================
|
| 255 |
# 照片检测页面
|
| 256 |
# ======================
|
| 257 |
|
| 258 |
def photo_detection_page():
|
| 259 |
+
audio_placeholder = st.empty()
|
| 260 |
st.title("照片检测")
|
| 261 |
st.write("上传一张图片或使用摄像头拍摄,检测是否吸烟,若检测到吸烟则进一步分析性别和年龄。")
|
| 262 |
|
|
|
|
| 297 |
age_result = age_detection(image)
|
| 298 |
st.success("The age result is:")
|
| 299 |
st.write(age_result)
|
| 300 |
+
|
| 301 |
+
audio_placeholder.empty()
|
| 302 |
+
audio_key = f"{age_result} {gender_result.lower()}"
|
| 303 |
+
if audio_key in audio_data:
|
| 304 |
+
audio_bytes = audio_data[audio_key]
|
| 305 |
+
play_audio_via_js(audio_bytes)
|
| 306 |
+
else:
|
| 307 |
+
st.error(f"音频文件不存在: {audio_key}.wav")
|
| 308 |
|
| 309 |
# ======================
|
| 310 |
+
# 实时检测页面(
|
| 311 |
# ======================
|
| 312 |
|
| 313 |
def real_time_detection_page():
|
|
|
|
| 418 |
|
| 419 |
def main():
|
| 420 |
st.sidebar.title("导航")
|
| 421 |
+
page = st.sidebar.selectbox("选择页面", ["coverpage","照片检测", "实时视频检测"])
|
| 422 |
|
| 423 |
+
if page == "coverpage":
|
| 424 |
+
cover_page()
|
| 425 |
if page == "照片检测":
|
| 426 |
photo_detection_page()
|
| 427 |
+
if page == "实时视频检测":
|
| 428 |
real_time_detection_page()
|
| 429 |
|
| 430 |
if __name__ == "__main__":
|