SURESHBEEKHANI commited on
Commit
385665b
·
verified ·
1 Parent(s): 33cc2d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -12
app.py CHANGED
@@ -5,8 +5,6 @@ import torchvision.transforms as transforms
5
  import base64
6
  import cv2
7
  import numpy as np
8
- from streamlit_webrtc import webrtc_streamer
9
- import av
10
 
11
  # Set Streamlit Page Configuration
12
  st.set_page_config(
@@ -47,12 +45,32 @@ def get_base64_image(image_path):
47
  except FileNotFoundError:
48
  return None
49
 
50
- # Function for real-time PPE detection using WebRTC
51
- def process_frame(frame):
52
- img = frame.to_ndarray(format="bgr24") # Convert frame to numpy array
53
- results = model.predict(img) # Run detection
54
- output_img = results[0].plot() # Draw detection results
55
- return av.VideoFrame.from_ndarray(output_img, format="bgr24") # Convert back to VideoFrame
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  # Display logo
58
  image_base64 = get_base64_image("logo/logo.png")
@@ -79,8 +97,8 @@ uploaded_file = st.sidebar.file_uploader("Drag and drop or browse", type=['jpg',
79
 
80
  # Sidebar - Live Predictions
81
  st.sidebar.header("📡 Live Predictions")
82
- st.sidebar.write("Start real-time PPE detection using your webcam")
83
- webrtc_streamer(key="live", video_frame_callback=process_frame)
84
 
85
  # Main Page
86
  st.title("PPE Detect")
@@ -102,6 +120,4 @@ if uploaded_file:
102
  st.error("Detection failed. Please try again.")
103
 
104
 
105
-
106
-
107
  st.info("This app uses **YOLO** for PPE detection. Upload an image or start live detection to get started.")
 
5
  import base64
6
  import cv2
7
  import numpy as np
 
 
8
 
9
  # Set Streamlit Page Configuration
10
  st.set_page_config(
 
45
  except FileNotFoundError:
46
  return None
47
 
48
+ # Function for real-time PPE detection using webcam
49
+ def live_ppe_detection():
50
+ st.sidebar.write("Starting live detection...")
51
+ cap = cv2.VideoCapture(0)
52
+ if not cap.isOpened():
53
+ st.sidebar.error("Error: Could not open webcam.")
54
+ return
55
+
56
+ stframe = st.empty()
57
+ stop_button = st.sidebar.button("Stop Live Detection", key="stop_button")
58
+
59
+ while cap.isOpened():
60
+ ret, frame = cap.read()
61
+ if not ret:
62
+ st.sidebar.error("Failed to capture video frame.")
63
+ break
64
+
65
+ results = model.predict(frame)
66
+ output_frame = results[0].plot()
67
+ stframe.image(output_frame, channels="BGR")
68
+
69
+ if stop_button:
70
+ break
71
+
72
+ cap.release()
73
+ cv2.destroyAllWindows()
74
 
75
  # Display logo
76
  image_base64 = get_base64_image("logo/logo.png")
 
97
 
98
  # Sidebar - Live Predictions
99
  st.sidebar.header("📡 Live Predictions")
100
+ if st.sidebar.button("Start Live Detection", key="start_button"):
101
+ live_ppe_detection()
102
 
103
  # Main Page
104
  st.title("PPE Detect")
 
120
  st.error("Detection failed. Please try again.")
121
 
122
 
 
 
123
  st.info("This app uses **YOLO** for PPE detection. Upload an image or start live detection to get started.")