Update app.py
Browse files
app.py
CHANGED
|
@@ -2,32 +2,22 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import cv2
|
| 4 |
from keras.models import load_model
|
| 5 |
-
from collections import deque
|
| 6 |
|
| 7 |
# Load the trained model
|
| 8 |
model = load_model("bullying_detection_model.keras")
|
| 9 |
|
| 10 |
-
# Constants
|
| 11 |
IMAGE_HEIGHT, IMAGE_WIDTH = 64, 64
|
| 12 |
SEQUENCE_LENGTH = 16
|
| 13 |
CLASSES_LIST = ["NonBullying", "Bullying"]
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
def preprocess_frames(frames):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def predict_webcam(frame):
|
| 21 |
-
frame_queue.append(frame)
|
| 22 |
-
if len(frame_queue) < SEQUENCE_LENGTH:
|
| 23 |
-
return "Collecting frames..."
|
| 24 |
-
input_frames = preprocess_frames(list(frame_queue))
|
| 25 |
-
input_frames = np.expand_dims(input_frames, axis=0)
|
| 26 |
-
preds = model.predict(input_frames, verbose=0)[0]
|
| 27 |
-
idx = np.argmax(preds)
|
| 28 |
-
return f"Prediction: {CLASSES_LIST[idx]} (Confidence: {preds[idx]:.2f})"
|
| 29 |
|
| 30 |
-
|
|
|
|
| 31 |
cap = cv2.VideoCapture(video)
|
| 32 |
frames = []
|
| 33 |
while True:
|
|
@@ -38,27 +28,24 @@ def predict_video(video):
|
|
| 38 |
cap.release()
|
| 39 |
if len(frames) < SEQUENCE_LENGTH:
|
| 40 |
return "Video too short"
|
|
|
|
| 41 |
idxs = np.linspace(0, len(frames)-1, SEQUENCE_LENGTH).astype(int)
|
| 42 |
-
|
| 43 |
-
input_frames = preprocess_frames(
|
| 44 |
input_frames = np.expand_dims(input_frames, axis=0)
|
| 45 |
-
preds = model.predict(input_frames
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
video_input = gr.Video()
|
| 59 |
-
output_video = gr.Label(num_top_classes=1)
|
| 60 |
-
btn = gr.Button("Predict")
|
| 61 |
-
btn.click(predict_video, inputs=video_input, outputs=output_video)
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import cv2
|
| 4 |
from keras.models import load_model
|
|
|
|
| 5 |
|
| 6 |
# Load the trained model
|
| 7 |
model = load_model("bullying_detection_model.keras")
|
| 8 |
|
| 9 |
+
# Constants (should match your training)
|
| 10 |
IMAGE_HEIGHT, IMAGE_WIDTH = 64, 64
|
| 11 |
SEQUENCE_LENGTH = 16
|
| 12 |
CLASSES_LIST = ["NonBullying", "Bullying"]
|
| 13 |
|
| 14 |
+
# Helper to preprocess frames
|
|
|
|
| 15 |
def preprocess_frames(frames):
|
| 16 |
+
processed = [cv2.resize(f, (IMAGE_HEIGHT, IMAGE_WIDTH))/255.0 for f in frames]
|
| 17 |
+
return np.array(processed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Gradio function: receives a video, returns prediction
|
| 20 |
+
def predict_bullying(video):
|
| 21 |
cap = cv2.VideoCapture(video)
|
| 22 |
frames = []
|
| 23 |
while True:
|
|
|
|
| 28 |
cap.release()
|
| 29 |
if len(frames) < SEQUENCE_LENGTH:
|
| 30 |
return "Video too short"
|
| 31 |
+
# Sample SEQUENCE_LENGTH evenly spaced frames
|
| 32 |
idxs = np.linspace(0, len(frames)-1, SEQUENCE_LENGTH).astype(int)
|
| 33 |
+
sampled_frames = [frames[i] for i in idxs]
|
| 34 |
+
input_frames = preprocess_frames(sampled_frames)
|
| 35 |
input_frames = np.expand_dims(input_frames, axis=0)
|
| 36 |
+
preds = model.predict(input_frames)[0]
|
| 37 |
+
pred_idx = np.argmax(preds)
|
| 38 |
+
pred_class = CLASSES_LIST[pred_idx]
|
| 39 |
+
confidence = float(preds[pred_idx])
|
| 40 |
+
return f"Prediction: {pred_class} (Confidence: {confidence:.2f})"
|
| 41 |
+
|
| 42 |
+
iface = gr.Interface(
|
| 43 |
+
fn=predict_bullying,
|
| 44 |
+
inputs=gr.Video(sources=["webcam", "upload"], label="Input Video"),
|
| 45 |
+
outputs="text",
|
| 46 |
+
title="Bullying Detection in Video",
|
| 47 |
+
description="Upload a video or use your webcam. The model will predict if bullying is present."
|
| 48 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
+
iface.launch()
|