Spaces:
Sleeping
Sleeping
#import os | |
#import streamlit as st | |
# Fix permission error for Hugging Face Spaces | |
#os.environ["STREAMLIT_HOME"] = "./safe_streamlit_home" | |
#os.environ["STREAMLIT_BROWSER_GATHER_USAGE_STATS"] = "false" | |
#os.makedirs(os.environ["STREAMLIT_HOME"], exist_ok=True) | |
# Your other imports... | |
import os | |
# β Tell Streamlit NOT to collect usage data or write config files | |
os.environ["STREAMLIT_BROWSER_GATHER_USAGE_STATS"] = "false" | |
os.environ["XDG_CONFIG_HOME"] = "/tmp" | |
import streamlit as st | |
#hf_token = os.getenv("HF_token") # Match the exact name you set in the Secrets tab | |
# Example usage with requests | |
#headers = { | |
# "Authorization": f"Bearer {hf_token}" | |
#} | |
# Example: Access a private file or API on Hugging Face | |
#import requests | |
#response = requests.get("https://huggingface.co/api/whoami-v2", headers=headers) | |
#print(response.json()) | |
import os | |
token = os.getenv("app") # secure | |
from huggingface_hub import hf_hub_download | |
#hf_hub_download( | |
#repo_id="muskan19/Violence_Detector", | |
#filename="violence_model.h5", | |
#use_auth_token=token | |
#) | |
import sys | |
import os | |
sys.path.append(os.path.join(os.path.dirname(__file__), 'src')) | |
# Streamlit app will be generated here | |
import streamlit as st | |
import cv2 | |
import numpy as np | |
from preprocess import preprocess_frame | |
#from predict import run_prediction | |
from predict import run_prediction, load_trained_model | |
#from src.preprocess import preprocess_frame | |
#from src.predict import load_trained_model, predict_violence | |
import tempfile | |
st.set_page_config(layout="wide") | |
st.title("π Violence Detection in Video") | |
st.markdown("Upload a video and let the model detect violent scenes in real-time.") | |
uploaded_file = st.file_uploader("Upload a video", type=["mp4", "avi","mpeg","mov","mpg"]) | |
model = load_trained_model() | |
if uploaded_file is not None: | |
tfile = tempfile.NamedTemporaryFile(delete=False) | |
tfile.write(uploaded_file.read()) | |
cap = cv2.VideoCapture(tfile.name) | |
stframe = st.empty() | |
while cap.isOpened(): | |
ret, frame = cap.read() | |
if not ret: | |
break | |
processed = preprocess_frame(frame) | |
pred = run_prediction(model, processed) | |
label = "Violent" if pred <= 0.5 else "Non-Violent" | |
color = (0, 0, 255) if label == "Violent" else (0, 255, 0) | |
cv2.putText(frame, f'{label} ({pred:.2f})', (10, 30), | |
cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2) | |
stframe.image(frame, channels="BGR") | |
cap.release() | |