File size: 1,342 Bytes
7a0f64b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# app_streamlit_final.py
import streamlit as st
import cv2
from ultralytics import YOLO
import numpy as np

# Page configuration
st.set_page_config(page_title="Live Mask Detector", layout="wide")
st.title("🎭 Real-Time Face Mask Detection")

# 1. Model load karein
model = YOLO("best.pt")

# 2. UI Layout
col1, col2 = st.columns([2, 1])
with col1:
    # Frame placeholder jahan video nazar aayegi
    frame_placeholder = st.empty()

with col2:
    st.info("System Status: Running")
    stop_button = st.button("Stop Camera")
    conf_slider = st.slider("Confidence Threshold", 0.1, 1.0, 0.4)

# 3. Camera Loop
cap = cv2.VideoCapture(0)

while cap.isOpened() and not stop_button:
    ret, frame = cap.read()
    if not ret:
        st.error("Camera access nahi mil raha. Please check permissions.")
        break

    # Prediction (Wahi logic jo mask_test.py mein tha)
    results = model.predict(frame, conf=conf_slider)
    
    # Annotated frame
    annotated_frame = results[0].plot()

    # BGR to RGB (Streamlit requirements)
    frame_rgb = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)

    # Browser mein display karein
    frame_placeholder.image(frame_rgb, channels="RGB", use_container_width=True)

    if stop_button:
        break

cap.release()
st.warning("Camera Stopped.")