Spaces:
Sleeping
Sleeping
Update camera_view.py
Browse files- camera_view.py +42 -31
camera_view.py
CHANGED
|
@@ -2,61 +2,72 @@ import streamlit as st
|
|
| 2 |
import cv2
|
| 3 |
import time
|
| 4 |
|
|
|
|
| 5 |
if "show_button" not in st.session_state:
|
| 6 |
st.session_state.show_button = True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
class Camera_View:
|
| 8 |
def __init__(self, app, model):
|
| 9 |
self.app = app
|
| 10 |
self.model = model
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
st.session_state.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def show(self):
|
|
|
|
| 15 |
col1_back, col2_back = st.columns([0.2, 0.8])
|
| 16 |
with col1_back:
|
| 17 |
if st.button("Back", key='upload_back', icon=':material/arrow_back:', type='primary'):
|
| 18 |
self.app.change_page("Main")
|
| 19 |
-
st.markdown("<h1 style='text-align: center;'>🧠 Real time detection</h1>", unsafe_allow_html=True)
|
| 20 |
|
|
|
|
| 21 |
st.divider()
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
with col1_button:
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
with col2_button:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
if st.session_state.show_button:
|
| 32 |
-
|
| 33 |
-
if start_button:
|
| 34 |
-
cap = cv2.VideoCapture(0) # Open webcam
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
while cap.isOpened():
|
| 37 |
ret, frame = cap.read()
|
| 38 |
-
if not ret or
|
| 39 |
break
|
| 40 |
|
| 41 |
-
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 42 |
-
results = self.model(frame)[0]
|
| 43 |
|
| 44 |
-
# Draw bounding boxes
|
| 45 |
for result in results.boxes.data.tolist():
|
| 46 |
x1, y1, x2, y2, score, class_id = result
|
| 47 |
-
if score > 0.5
|
| 48 |
-
|
| 49 |
-
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), color, 1)
|
| 50 |
-
else:
|
| 51 |
-
color = (0, 255, 0)
|
| 52 |
-
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), color, 1)
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
frame_placeholder.image(frame, use_container_width=True) # Replace previous frame
|
| 56 |
-
time.sleep(0.1) # Control frame rate
|
| 57 |
-
|
| 58 |
-
cap.release() # Release the webcam when stopped
|
| 59 |
-
st.warning("Camera stopped!") # Notify user
|
| 60 |
-
|
| 61 |
|
|
|
|
|
|
|
| 62 |
|
|
|
|
|
|
|
|
|
| 2 |
import cv2
|
| 3 |
import time
|
| 4 |
|
| 5 |
+
# Initialize session state
|
| 6 |
if "show_button" not in st.session_state:
|
| 7 |
st.session_state.show_button = True
|
| 8 |
+
|
| 9 |
+
if "camera_active" not in st.session_state:
|
| 10 |
+
st.session_state.camera_active = False
|
| 11 |
+
|
| 12 |
class Camera_View:
|
| 13 |
def __init__(self, app, model):
|
| 14 |
self.app = app
|
| 15 |
self.model = model
|
| 16 |
|
| 17 |
+
def toggle_camera(self):
|
| 18 |
+
st.session_state.camera_active = True
|
| 19 |
+
st.session_state.show_button = False # Hide st.camera_input when camera is started
|
| 20 |
+
|
| 21 |
+
def stop_camera(self):
|
| 22 |
+
st.session_state.camera_active = False
|
| 23 |
+
|
| 24 |
def show(self):
|
| 25 |
+
# Top navigation
|
| 26 |
col1_back, col2_back = st.columns([0.2, 0.8])
|
| 27 |
with col1_back:
|
| 28 |
if st.button("Back", key='upload_back', icon=':material/arrow_back:', type='primary'):
|
| 29 |
self.app.change_page("Main")
|
|
|
|
| 30 |
|
| 31 |
+
st.markdown("<h1 style='text-align: center;'>🧠 Real-time Detection</h1>", unsafe_allow_html=True)
|
| 32 |
st.divider()
|
| 33 |
+
|
| 34 |
+
# Buttons to control camera
|
| 35 |
+
col1_button, col2_button = st.columns(2)
|
| 36 |
with col1_button:
|
| 37 |
+
st.button("Stop Camera",
|
| 38 |
+
icon=':material/videocam_off:',
|
| 39 |
+
type='secondary',
|
| 40 |
+
on_click=self.stop_camera) # Button to stop video
|
| 41 |
with col2_button:
|
| 42 |
+
st.button("Start Camera",
|
| 43 |
+
icon=':material/videocam:',
|
| 44 |
+
type='primary',
|
| 45 |
+
on_click=self.toggle_camera)
|
| 46 |
+
|
| 47 |
+
# Display st.camera_input if camera is not started yet
|
| 48 |
if st.session_state.show_button:
|
| 49 |
+
st.camera_input("")
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
frame_placeholder = st.empty()
|
| 52 |
+
|
| 53 |
+
# Run real-time detection if camera is active
|
| 54 |
+
if st.session_state.camera_active:
|
| 55 |
+
cap = cv2.VideoCapture(0)
|
| 56 |
while cap.isOpened():
|
| 57 |
ret, frame = cap.read()
|
| 58 |
+
if not ret or not st.session_state.camera_active:
|
| 59 |
break
|
| 60 |
|
| 61 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 62 |
+
results = self.model(frame)[0]
|
| 63 |
|
|
|
|
| 64 |
for result in results.boxes.data.tolist():
|
| 65 |
x1, y1, x2, y2, score, class_id = result
|
| 66 |
+
color = (0, 0, 255) if score > 0.5 else (0, 255, 0)
|
| 67 |
+
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), color, 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
frame_placeholder.image(frame, channels="RGB", use_container_width=True)
|
| 70 |
+
time.sleep(0.1)
|
| 71 |
|
| 72 |
+
cap.release()
|
| 73 |
+
st.warning("Camera stopped.")
|