Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@
|
|
5 |
#os.environ["STREAMLIT_HOME"] = "./safe_streamlit_home"
|
6 |
#os.environ["STREAMLIT_BROWSER_GATHER_USAGE_STATS"] = "false"
|
7 |
#os.makedirs(os.environ["STREAMLIT_HOME"], exist_ok=True)
|
8 |
-
|
9 |
# Your other imports...
|
10 |
import os
|
11 |
|
@@ -91,3 +91,45 @@ if uploaded_file is not None:
|
|
91 |
|
92 |
cap.release()
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
#os.environ["STREAMLIT_HOME"] = "./safe_streamlit_home"
|
6 |
#os.environ["STREAMLIT_BROWSER_GATHER_USAGE_STATS"] = "false"
|
7 |
#os.makedirs(os.environ["STREAMLIT_HOME"], exist_ok=True)
|
8 |
+
'''
|
9 |
# Your other imports...
|
10 |
import os
|
11 |
|
|
|
91 |
|
92 |
cap.release()
|
93 |
|
94 |
+
'''
|
95 |
+
|
96 |
+
|
97 |
+
import os
|
98 |
+
os.environ["STREAMLIT_BROWSER_GATHER_USAGE_STATS"] = "false"
|
99 |
+
os.environ["XDG_CONFIG_HOME"] = "/tmp"
|
100 |
+
|
101 |
+
import streamlit as st
|
102 |
+
import cv2
|
103 |
+
import numpy as np
|
104 |
+
import tempfile
|
105 |
+
from src.preprocess import preprocess_frame
|
106 |
+
from src.predict import run_prediction, load_trained_model
|
107 |
+
|
108 |
+
st.set_page_config(layout="wide")
|
109 |
+
st.title("🔍 Violence Detection in Video")
|
110 |
+
st.markdown("Upload a video and let the model detect violent scenes in real-time.")
|
111 |
+
|
112 |
+
uploaded_file = st.file_uploader("Upload a video", type=["mp4", "avi", "mpeg", "mov", "mpg"])
|
113 |
+
model = load_trained_model()
|
114 |
+
|
115 |
+
if uploaded_file is not None:
|
116 |
+
tfile = tempfile.NamedTemporaryFile(delete=False)
|
117 |
+
tfile.write(uploaded_file.read())
|
118 |
+
cap = cv2.VideoCapture(tfile.name)
|
119 |
+
stframe = st.empty()
|
120 |
+
|
121 |
+
while cap.isOpened():
|
122 |
+
ret, frame = cap.read()
|
123 |
+
if not ret:
|
124 |
+
break
|
125 |
+
|
126 |
+
processed = preprocess_frame(frame)
|
127 |
+
pred = run_prediction(model, processed)
|
128 |
+
label = "Violent" if pred <= 0.5 else "Non-Violent"
|
129 |
+
color = (0, 0, 255) if label == "Violent" else (0, 255, 0)
|
130 |
+
|
131 |
+
cv2.putText(frame, f'{label} ({pred:.2f})', (10, 30),
|
132 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2)
|
133 |
+
stframe.image(frame, channels="BGR")
|
134 |
+
|
135 |
+
cap.release()
|