Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- agetest_mobilevit_V_1.1.pt +3 -0
- app.py +235 -0
- gendertest_mobilevit_V_1.1.pt +3 -0
- modelnew.h5 +3 -0
- yolov8_people.pt +3 -0
agetest_mobilevit_V_1.1.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c1a6587212e34bcdcc7c60d50b63810f3f325f28fb3a638a706e3ae905ce2b12
|
| 3 |
+
size 11701594
|
app.py
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from collections import deque
|
| 7 |
+
from keras.models import load_model
|
| 8 |
+
from ultralytics import YOLO
|
| 9 |
+
import csv
|
| 10 |
+
from Crypto.Cipher import AES
|
| 11 |
+
from Crypto.Random import get_random_bytes
|
| 12 |
+
import timm
|
| 13 |
+
import base64
|
| 14 |
+
import pandas as pd
|
| 15 |
+
|
| 16 |
+
# Set page title and favicon
|
| 17 |
+
st.set_page_config(page_title="Violence Detection and Analysis App", page_icon=":boom:")
|
| 18 |
+
|
| 19 |
+
# CSS styling
|
| 20 |
+
st.markdown(
|
| 21 |
+
"""
|
| 22 |
+
<style>
|
| 23 |
+
.title {
|
| 24 |
+
color: #1f78b4;
|
| 25 |
+
text-align: center;
|
| 26 |
+
font-size: 36px;
|
| 27 |
+
margin-bottom: 30px;
|
| 28 |
+
}
|
| 29 |
+
.header {
|
| 30 |
+
color: #1f78b4;
|
| 31 |
+
font-size: 24px;
|
| 32 |
+
margin-top: 30px;
|
| 33 |
+
}
|
| 34 |
+
.btn-download {
|
| 35 |
+
background-color: #4CAF50;
|
| 36 |
+
border: none;
|
| 37 |
+
color: white;
|
| 38 |
+
padding: 15px 32px;
|
| 39 |
+
text-align: center;
|
| 40 |
+
text-decoration: none;
|
| 41 |
+
display: inline-block;
|
| 42 |
+
font-size: 16px;
|
| 43 |
+
margin: 4px 2px;
|
| 44 |
+
cursor: pointer;
|
| 45 |
+
border-radius: 10px;
|
| 46 |
+
}
|
| 47 |
+
</style>
|
| 48 |
+
""",
|
| 49 |
+
unsafe_allow_html=True
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Load the pre-trained models for age and gender prediction
|
| 53 |
+
age_model = timm.create_model('mobilevitv2_075.cvnets_in1k', pretrained=True, num_classes=5, global_pool='catavgmax')
|
| 54 |
+
num_in_features = age_model.get_classifier().in_features
|
| 55 |
+
age_model.fc = nn.Sequential(
|
| 56 |
+
nn.BatchNorm1d(num_in_features),
|
| 57 |
+
nn.Linear(in_features=num_in_features, out_features=512, bias=False),
|
| 58 |
+
nn.ReLU(),
|
| 59 |
+
nn.BatchNorm1d(512),
|
| 60 |
+
nn.Dropout(0.4),
|
| 61 |
+
nn.Linear(in_features=512, out_features=5, bias=False)
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
gender_model = timm.create_model('mobilevitv2_075.cvnets_in1k', pretrained=True, num_classes=2, global_pool='catavgmax')
|
| 65 |
+
num_in_features = gender_model.get_classifier().in_features
|
| 66 |
+
gender_model.fc = nn.Sequential(
|
| 67 |
+
nn.BatchNorm1d(num_in_features),
|
| 68 |
+
nn.Linear(in_features=num_in_features, out_features=512, bias=False),
|
| 69 |
+
nn.ReLU(),
|
| 70 |
+
nn.BatchNorm1d(512),
|
| 71 |
+
nn.Dropout(0.4),
|
| 72 |
+
nn.Linear(in_features=512, out_features=2, bias=False)
|
| 73 |
+
)
|
| 74 |
+
age_model.load_state_dict(torch.load('E:/Drive D/barchart/age models/age_mobile_V_1.1/PT/agetest_mobilevit_V_1.1.pt', map_location=torch.device('cpu')))
|
| 75 |
+
gender_model.load_state_dict(torch.load('E:/Drive D/barchart/gender models/gender_mobilevit_version_1.1/pt/gendertest_mobilevit_V_1.1.pt', map_location=torch.device('cpu')))
|
| 76 |
+
|
| 77 |
+
# Load the violence detection model
|
| 78 |
+
violence_model = load_model('E:/TNIBF/modelnew (1).h5')
|
| 79 |
+
MODEL = "E:/TNIBF/yolov8_people.pt"
|
| 80 |
+
yolo_model = YOLO(MODEL)
|
| 81 |
+
|
| 82 |
+
# Function to preprocess image for detection
|
| 83 |
+
def preprocess_image(image, target_size=(256, 256)):
|
| 84 |
+
img = cv2.resize(image, target_size, interpolation=cv2.INTER_CUBIC)
|
| 85 |
+
img = img.astype(np.float32) / 255.0
|
| 86 |
+
img = (img - np.array([0.485, 0.456, 0.406])) / np.array([0.229, 0.224, 0.225])
|
| 87 |
+
img = np.transpose(img, (2, 0, 1))
|
| 88 |
+
img = np.expand_dims(img, axis=0)
|
| 89 |
+
return torch.tensor(img, dtype=torch.float32)
|
| 90 |
+
|
| 91 |
+
# Function to detect violence in a frame
|
| 92 |
+
def detect_violence(frame):
|
| 93 |
+
true_count = 0
|
| 94 |
+
Q = deque(maxlen=128)
|
| 95 |
+
frame_copy = frame.copy()
|
| 96 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 97 |
+
frame = cv2.resize(frame, (128, 128)).astype("float32") / 255
|
| 98 |
+
preds = violence_model.predict(np.expand_dims(frame, axis=0))[0]
|
| 99 |
+
Q.append(preds)
|
| 100 |
+
results = np.array(Q).mean(axis=0)
|
| 101 |
+
label = (results > 0.50)[0]
|
| 102 |
+
text_color = (255, 0, 0) if label else (0, 255, 0)
|
| 103 |
+
text = "Violence Detected" if label else "No Violence Detected"
|
| 104 |
+
cv2.putText(frame_copy, text, (35, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.25, text_color, 3)
|
| 105 |
+
return frame_copy, label
|
| 106 |
+
|
| 107 |
+
# Function to detect people, their age, and gender in a frame
|
| 108 |
+
def detect_people_age_gender(frame):
|
| 109 |
+
results = yolo_model(frame)
|
| 110 |
+
detections = []
|
| 111 |
+
male_count = 0
|
| 112 |
+
female_count = 0
|
| 113 |
+
|
| 114 |
+
for result in results:
|
| 115 |
+
boxes = result.boxes.xyxy
|
| 116 |
+
confidences = result.boxes.conf
|
| 117 |
+
classes = result.boxes.cls
|
| 118 |
+
|
| 119 |
+
for box, confidence, cls in zip(boxes, confidences, classes):
|
| 120 |
+
x1, y1, x2, y2 = map(int, box)
|
| 121 |
+
person_crop = frame[y1:y2, x1:x2]
|
| 122 |
+
person_crop_resized = preprocess_image(person_crop, target_size=(224, 224))
|
| 123 |
+
|
| 124 |
+
age_preds = age_model(person_crop_resized)
|
| 125 |
+
age_class_index = np.argmax(age_preds.detach().numpy())
|
| 126 |
+
age_class_names = ['0-15', '15-30', '30-45', '45-60', '60+']
|
| 127 |
+
age = age_class_names[age_class_index]
|
| 128 |
+
|
| 129 |
+
gender_preds = gender_model(person_crop_resized)
|
| 130 |
+
gender = "Male" if np.argmax(gender_preds.detach().numpy()) == 0 else "Female"
|
| 131 |
+
|
| 132 |
+
detections.append((x1, y1, x2, y2, age, gender))
|
| 133 |
+
|
| 134 |
+
if gender == 'Male':
|
| 135 |
+
male_count += 1
|
| 136 |
+
else:
|
| 137 |
+
female_count += 1
|
| 138 |
+
|
| 139 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 140 |
+
cv2.putText(frame, f"Age: {age}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 141 |
+
cv2.putText(frame, f"Gender: {gender}", (x1, y1 - 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 142 |
+
|
| 143 |
+
return frame, detections, male_count, female_count
|
| 144 |
+
|
| 145 |
+
# Function to encrypt a file
|
| 146 |
+
def encrypt_file(input_file, output_file, key):
|
| 147 |
+
cipher = AES.new(key, AES.MODE_CBC)
|
| 148 |
+
with open(input_file, 'rb') as infile:
|
| 149 |
+
data = infile.read()
|
| 150 |
+
# Add padding if needed
|
| 151 |
+
if len(data) % 16 != 0:
|
| 152 |
+
padding_length = 16 - len(data) % 16
|
| 153 |
+
data += bytes([padding_length]) * padding_length
|
| 154 |
+
ciphertext = cipher.encrypt(data)
|
| 155 |
+
with open(output_file, 'wb') as outfile:
|
| 156 |
+
outfile.write(cipher.iv)
|
| 157 |
+
outfile.write(ciphertext)
|
| 158 |
+
|
| 159 |
+
# Function to decrypt a file
|
| 160 |
+
def decrypt_file(input_file, output_file, key):
|
| 161 |
+
with open(input_file, 'rb') as infile:
|
| 162 |
+
iv = infile.read(16)
|
| 163 |
+
cipher = AES.new(key, AES.MODE_CBC, iv)
|
| 164 |
+
plaintext = cipher.decrypt(infile.read())
|
| 165 |
+
# Remove padding
|
| 166 |
+
padding_length = plaintext[-1]
|
| 167 |
+
plaintext = plaintext[:-padding_length]
|
| 168 |
+
with open(output_file, 'wb') as outfile:
|
| 169 |
+
outfile.write(plaintext)
|
| 170 |
+
|
| 171 |
+
# Streamlit UI
|
| 172 |
+
def main():
|
| 173 |
+
st.markdown('<h1 class="title">Violence Detection and Analysis App</h1>', unsafe_allow_html=True)
|
| 174 |
+
|
| 175 |
+
uploaded_file = st.file_uploader("Upload Video", type=["mp4"])
|
| 176 |
+
if uploaded_file is not None:
|
| 177 |
+
video_path = "temp_video.mp4"
|
| 178 |
+
with open(video_path, "wb") as f:
|
| 179 |
+
f.write(uploaded_file.getbuffer())
|
| 180 |
+
|
| 181 |
+
st.video(video_path)
|
| 182 |
+
|
| 183 |
+
cap = cv2.VideoCapture(video_path)
|
| 184 |
+
frame_width = int(cap.get(3))
|
| 185 |
+
frame_height = int(cap.get(4))
|
| 186 |
+
out = cv2.VideoWriter("temp_output_video.mp4", cv2.VideoWriter_fourcc(*'mp4v'), 30, (frame_width, frame_height))
|
| 187 |
+
|
| 188 |
+
csv_filename = "temp_detection_results.csv"
|
| 189 |
+
with open(csv_filename, mode='w', newline='') as file:
|
| 190 |
+
writer = csv.writer(file)
|
| 191 |
+
writer.writerow(['Frame', 'Violence', 'People Count', 'Male Count', 'Female Count'])
|
| 192 |
+
|
| 193 |
+
frame_number = 0
|
| 194 |
+
while cap.isOpened():
|
| 195 |
+
ret, frame = cap.read()
|
| 196 |
+
if not ret:
|
| 197 |
+
break
|
| 198 |
+
|
| 199 |
+
frame_violence, violence_label = detect_violence(frame)
|
| 200 |
+
frame_result, detections, male_count, female_count = detect_people_age_gender(frame_violence)
|
| 201 |
+
|
| 202 |
+
writer.writerow([frame_number, violence_label, len(detections), male_count, female_count])
|
| 203 |
+
out.write(frame_result)
|
| 204 |
+
frame_number += 1
|
| 205 |
+
|
| 206 |
+
cap.release()
|
| 207 |
+
out.release()
|
| 208 |
+
|
| 209 |
+
key = b'ThisIsASecretKey'
|
| 210 |
+
encrypted_file = 'temp_encrypted.csv'
|
| 211 |
+
decrypted_file = 'temp_decrypted.csv'
|
| 212 |
+
|
| 213 |
+
encrypt_file(csv_filename, encrypted_file, key)
|
| 214 |
+
decrypt_file(encrypted_file, decrypted_file, key)
|
| 215 |
+
|
| 216 |
+
# Download processed video
|
| 217 |
+
with open("temp_output_video.mp4", "rb") as f:
|
| 218 |
+
video_bytes = f.read()
|
| 219 |
+
video_b64 = base64.b64encode(video_bytes).decode('utf-8')
|
| 220 |
+
href = f'<a class="btn-download" href="data:video/mp4;base64,{video_b64}" download="processed_video.mp4">Download Processed Video</a>'
|
| 221 |
+
st.markdown(href, unsafe_allow_html=True)
|
| 222 |
+
|
| 223 |
+
# Download encrypted CSV
|
| 224 |
+
with open(encrypted_file, "rb") as f:
|
| 225 |
+
encrypted_data = f.read()
|
| 226 |
+
href = f'<a class="btn-download" href="data:file/csv;base64,{base64.b64encode(encrypted_data).decode()}" download="encrypted_csv.csv">Download Encrypted CSV</a>'
|
| 227 |
+
st.markdown(href, unsafe_allow_html=True)
|
| 228 |
+
|
| 229 |
+
# Download decrypted CSV
|
| 230 |
+
decrypted_data = pd.read_csv(decrypted_file)
|
| 231 |
+
st.markdown('<h2 class="header">Decrypted CSV:</h2>', unsafe_allow_html=True)
|
| 232 |
+
st.dataframe(decrypted_data)
|
| 233 |
+
|
| 234 |
+
if __name__ == "__main__":
|
| 235 |
+
main()
|
gendertest_mobilevit_V_1.1.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:eef8158029cee77a5eb0b4410e44d8e56e693ed632d968f4ba4c4be8bbfdaadc
|
| 3 |
+
size 11687083
|
modelnew.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c0ca2e447d364ec829fdfdc0f70faf2c3a1f6d71d7ca24cc835de7f7b9a229fb
|
| 3 |
+
size 9533528
|
yolov8_people.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:35b0bf8bdab93e98441de8a325532d48755ee1b3b3ece70cf3341b7c735e4f03
|
| 3 |
+
size 6229593
|