Spaces:
Runtime error
Runtime error
Uploadin files
Browse files- .gitignore +1 -0
- app.py +92 -0
- model.h5 +3 -0
- requirements.txt +3 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
*.idea
|
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
|
6 |
+
# Model loading
|
7 |
+
model = load_model('model.h5')
|
8 |
+
|
9 |
+
# Specifying Height and Width
|
10 |
+
IMAGE_HEIGHT, IMAGE_WIDTH = 128, 128
|
11 |
+
|
12 |
+
SEQUENCE_LENGTH = 12
|
13 |
+
|
14 |
+
CLASSES_LIST = ["NonViolence", "Violence"]
|
15 |
+
|
16 |
+
def predict_video(video_file_path, sequence_length):
|
17 |
+
|
18 |
+
video_reader = cv2.VideoCapture(video_file_path)
|
19 |
+
|
20 |
+
# Get original width and height of the video.
|
21 |
+
original_video_width = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH))
|
22 |
+
original_video_height = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
23 |
+
|
24 |
+
# List to store video frames
|
25 |
+
frames_list = []
|
26 |
+
|
27 |
+
# Storing the predicted class
|
28 |
+
predicted_class_name = ''
|
29 |
+
|
30 |
+
# Get total number of frames
|
31 |
+
video_frames_count = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT))
|
32 |
+
|
33 |
+
# interval after which frames will be added
|
34 |
+
skip_frames_window = max(int(video_frames_count / sequence_length), 1)
|
35 |
+
|
36 |
+
for frame_counter in range(sequence_length):
|
37 |
+
# set the current frame
|
38 |
+
video_reader.set(cv2.CAP_PROP_POS_FRAMES, frame_counter * skip_frames_window)
|
39 |
+
|
40 |
+
success, frame = video_reader.read()
|
41 |
+
|
42 |
+
if not success:
|
43 |
+
break
|
44 |
+
|
45 |
+
# resize the Frame
|
46 |
+
resized_frame = cv2.resize(frame, (IMAGE_HEIGHT, IMAGE_WIDTH))
|
47 |
+
|
48 |
+
# normalizing the resized frame.
|
49 |
+
normalized_frame = resized_frame / 255
|
50 |
+
|
51 |
+
# appending frames into the frames list
|
52 |
+
frames_list.append(normalized_frame)
|
53 |
+
|
54 |
+
# passing frames to model and get predictions
|
55 |
+
predicted_labels_probabilities = model.predict(np.expand_dims(frames_list, axis=0))[0]
|
56 |
+
|
57 |
+
# get index of class with highest probability.
|
58 |
+
predicted_label = np.argmax(predicted_labels_probabilities)
|
59 |
+
|
60 |
+
# Get the class name using the retrieved index.
|
61 |
+
predicted_class_name = CLASSES_LIST[predicted_label]
|
62 |
+
|
63 |
+
video_reader.release()
|
64 |
+
|
65 |
+
return predicted_class_name, predicted_labels_probabilities[predicted_label]
|
66 |
+
|
67 |
+
|
68 |
+
def main():
|
69 |
+
st.title("Violence Detection")
|
70 |
+
|
71 |
+
extensions = ['mp4', 'png', 'avi', 'mov']
|
72 |
+
# Upload a video file
|
73 |
+
uploaded_file = st.file_uploader("Upload a video file", type=extensions)
|
74 |
+
|
75 |
+
if uploaded_file:
|
76 |
+
# Get video file path
|
77 |
+
video_file_path = "temp_video.mp4"
|
78 |
+
with open(video_file_path, "wb") as f:
|
79 |
+
f.write(uploaded_file.getvalue())
|
80 |
+
|
81 |
+
# Predict on the video
|
82 |
+
predicted_class, confidence = predict_video(video_file_path, SEQUENCE_LENGTH)
|
83 |
+
|
84 |
+
# Display the predicted class along with the prediction confidence.
|
85 |
+
st.write(f'Predicted: {predicted_class}\nConfidence: {confidence}')
|
86 |
+
|
87 |
+
# Play the actual video
|
88 |
+
st.video(video_file_path)
|
89 |
+
|
90 |
+
|
91 |
+
if __name__ == "__main__":
|
92 |
+
main()
|
model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:0d7fe4455c97f6f78f5abedebbf363f160bd0d620dd66f7432db4e5dd9429e7f
|
3 |
+
size 1244288
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
opencv-python==4.8.0
|
2 |
+
tensorflow==2.10.0
|
3 |
+
streamlit==1.26.0
|