Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,42 +10,29 @@ from streamlit_option_menu import option_menu
|
|
| 10 |
|
| 11 |
st.set_page_config(page_title="Facial Analysis", page_icon="👤", layout="wide")
|
| 12 |
|
| 13 |
-
# --- Path Setup & Model Loading ---
|
| 14 |
try:
|
| 15 |
-
# Use a simpler path for deployment
|
| 16 |
from src.cnnClassifier.pipeline.prediction import PredictionPipeline
|
| 17 |
except ImportError:
|
| 18 |
-
# Fallback for local development
|
| 19 |
src_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'src'))
|
| 20 |
if src_path not in sys.path: sys.path.append(src_path)
|
| 21 |
from cnnClassifier.pipeline.prediction import PredictionPipeline
|
| 22 |
|
| 23 |
-
# --- TF Config (for MTCNN in Image/Video modes) ---
|
| 24 |
-
try:
|
| 25 |
-
gpus = tf.config.list_physical_devices('GPU')
|
| 26 |
-
if gpus:
|
| 27 |
-
for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True)
|
| 28 |
-
except Exception: pass
|
| 29 |
-
|
| 30 |
@st.cache_resource
|
| 31 |
def load_pipeline():
|
| 32 |
-
|
| 33 |
-
return PredictionPipeline(repo_id="ALYYAN/Facial-Age-Det")
|
| 34 |
-
|
| 35 |
pipeline = load_pipeline()
|
| 36 |
|
| 37 |
if 'webcam_running' not in st.session_state: st.session_state.webcam_running = False
|
| 38 |
def start_webcam(): st.session_state.webcam_running = True
|
| 39 |
def stop_webcam(): st.session_state.webcam_running = False
|
| 40 |
|
| 41 |
-
# --- UI ---
|
| 42 |
with st.sidebar:
|
| 43 |
st.markdown("## ⚙️ Controls")
|
| 44 |
app_mode = option_menu(None, ["Image", "Video", "Live Feed"],
|
| 45 |
icons=['image', 'film', 'camera-video'], menu_icon="cast", default_index=0)
|
| 46 |
|
| 47 |
if not pipeline:
|
| 48 |
-
st.error("AI Pipeline failed to load.
|
| 49 |
else:
|
| 50 |
st.title("👤 Facial Demographics Analysis")
|
| 51 |
st.header(f"Mode: {app_mode}")
|
|
@@ -95,7 +82,7 @@ else:
|
|
| 95 |
st.download_button("Download Processed Video", f, "output.mp4", "video/mp4", use_container_width=True)
|
| 96 |
|
| 97 |
elif app_mode == "Live Feed":
|
| 98 |
-
st.info("Live feed uses a lightweight face detector for
|
| 99 |
col1, col2 = st.columns(2)
|
| 100 |
with col1: st.button("Start Feed", on_click=start_webcam, use_container_width=True, type="primary")
|
| 101 |
with col2: st.button("Stop Feed", on_click=stop_webcam, use_container_width=True)
|
|
@@ -108,14 +95,23 @@ else:
|
|
| 108 |
while st.session_state.webcam_running:
|
| 109 |
start_time = time.time()
|
| 110 |
ret, frame = cap.read()
|
| 111 |
-
if not ret:
|
|
|
|
|
|
|
|
|
|
| 112 |
frame = cv2.flip(frame, 1)
|
| 113 |
-
|
| 114 |
-
annotated_frame, _ = pipeline.predict_lq(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 115 |
FRAME_WINDOW.image(annotated_frame, channels="RGB")
|
| 116 |
fps = 1.0 / (time.time() - start_time) if (time.time() - start_time) > 0 else 0
|
| 117 |
fps_display.markdown(f"<p style='text-align: center;'><b>FPS: {fps:.2f}</b></p>", unsafe_allow_html=True)
|
|
|
|
| 118 |
cap.release()
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
st.set_page_config(page_title="Facial Analysis", page_icon="👤", layout="wide")
|
| 12 |
|
|
|
|
| 13 |
try:
|
|
|
|
| 14 |
from src.cnnClassifier.pipeline.prediction import PredictionPipeline
|
| 15 |
except ImportError:
|
|
|
|
| 16 |
src_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'src'))
|
| 17 |
if src_path not in sys.path: sys.path.append(src_path)
|
| 18 |
from cnnClassifier.pipeline.prediction import PredictionPipeline
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
@st.cache_resource
|
| 21 |
def load_pipeline():
|
| 22 |
+
return PredictionPipeline()
|
|
|
|
|
|
|
| 23 |
pipeline = load_pipeline()
|
| 24 |
|
| 25 |
if 'webcam_running' not in st.session_state: st.session_state.webcam_running = False
|
| 26 |
def start_webcam(): st.session_state.webcam_running = True
|
| 27 |
def stop_webcam(): st.session_state.webcam_running = False
|
| 28 |
|
|
|
|
| 29 |
with st.sidebar:
|
| 30 |
st.markdown("## ⚙️ Controls")
|
| 31 |
app_mode = option_menu(None, ["Image", "Video", "Live Feed"],
|
| 32 |
icons=['image', 'film', 'camera-video'], menu_icon="cast", default_index=0)
|
| 33 |
|
| 34 |
if not pipeline:
|
| 35 |
+
st.error("AI Pipeline failed to load. Check terminal logs.")
|
| 36 |
else:
|
| 37 |
st.title("👤 Facial Demographics Analysis")
|
| 38 |
st.header(f"Mode: {app_mode}")
|
|
|
|
| 82 |
st.download_button("Download Processed Video", f, "output.mp4", "video/mp4", use_container_width=True)
|
| 83 |
|
| 84 |
elif app_mode == "Live Feed":
|
| 85 |
+
st.info("Live feed uses a lightweight face detector for performance.")
|
| 86 |
col1, col2 = st.columns(2)
|
| 87 |
with col1: st.button("Start Feed", on_click=start_webcam, use_container_width=True, type="primary")
|
| 88 |
with col2: st.button("Stop Feed", on_click=stop_webcam, use_container_width=True)
|
|
|
|
| 95 |
while st.session_state.webcam_running:
|
| 96 |
start_time = time.time()
|
| 97 |
ret, frame = cap.read()
|
| 98 |
+
if not ret:
|
| 99 |
+
st.warning("Could not read frame from webcam. Stopping.")
|
| 100 |
+
stop_webcam()
|
| 101 |
+
break
|
| 102 |
frame = cv2.flip(frame, 1)
|
| 103 |
+
annotated_frame, _ = pipeline.predict(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
|
|
|
| 104 |
FRAME_WINDOW.image(annotated_frame, channels="RGB")
|
| 105 |
fps = 1.0 / (time.time() - start_time) if (time.time() - start_time) > 0 else 0
|
| 106 |
fps_display.markdown(f"<p style='text-align: center;'><b>FPS: {fps:.2f}</b></p>", unsafe_allow_html=True)
|
| 107 |
+
|
| 108 |
cap.release()
|
| 109 |
+
# --- THE FIX ---
|
| 110 |
+
# cv2.destroyAllWindows() # This line is removed
|
| 111 |
+
# --- END FIX ---
|
| 112 |
+
|
| 113 |
+
if st.session_state.webcam_running:
|
| 114 |
+
st.session_state.webcam_running = False
|
| 115 |
+
st.rerun()
|
| 116 |
+
|
| 117 |
+
|