-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.py
97 lines (80 loc) · 3.27 KB
/
main2.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import cv2
import streamlit as st
import os
from datetime import datetime # Import datetime to generate timestamp
from predict2 import predict_frames_from_folder
# Import the predict_video function from predict.py
# Function to save a frame with timestamp
def save_frame(frame):
# Use datetime to generate a timestamp
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
filename = os.path.join("frames", f"frame_{timestamp}.jpg")
cv2.imwrite(filename, frame)
# Create the 'frames' directory if it doesn't exist
if not os.path.exists("frames"):
os.makedirs("frames")
# Initialize session states
if "capture" not in st.session_state:
st.session_state.capture = False
# UI
st.title("Violence Detection with ML & OpenCV ")
# Placeholder for displaying messages
message_placeholder = st.empty()
# Container for the video feed
video_container = st.empty()
# Buttons for starting and stopping video capture
col1, col2, col3 = st.columns(
3
) # Adjust to add an extra column for the "Predict" button
with col1:
if st.button("Start"):
# Reinitialize
st.session_state.capture = True
message_placeholder.empty()
if not hasattr(st.session_state, "cap") or not st.session_state.cap.isOpened():
st.session_state.cap = cv2.VideoCapture(
1
) # Make sure to use the correct camera index
st.rerun()
with col2:
if st.button("Stop"):
st.session_state.capture = False
if hasattr(st.session_state, "cap"):
st.session_state.cap.release()
message_placeholder.success("Video capturing has been stopped.")
# Predict button
# with col3:
# if st.button("Predict"):
# # Call the predict_video function and display its output
# # # Specify the path to your folder containing frames
# frames_folder_path = "frames"
# prediction = predict_frames_from_folder(frames_folder_path) # Assuming predict_video() is properly defined in predict.py
# # Display the prediction result beautifully
# st.markdown("### Prediction Result")
# st.write(prediction) # Customize this part based on how you want to display the prediction
# Predict button
with col3:
if st.button("Predict"):
# Call the predict_video function and display its output
# Specify the path to your folder containing frames
frames_folder_path = "frames"
st.markdown("### Prediction Result")
st.text("\n") # Add a newline for spacing, if needed
prediction = predict_frames_from_folder(
frames_folder_path
) # Assuming predict_video() is properly defined in predict
# Capture and display loop
if st.session_state.capture:
while st.session_state.capture:
ret, frame = st.session_state.cap.read()
if not ret:
message_placeholder.error("Failed to capture video.")
st.session_state.capture = False
if hasattr(st.session_state, "cap"):
st.session_state.cap.release()
break
# Display the frame
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
video_container.image(frame_rgb, channels="RGB", use_column_width=True)
# Save the frame using the new timestamp naming convention
save_frame(frame)