Commit ·
020588d
1
Parent(s): 9980690
Added application and requirements file
Browse files- app.py +51 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 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.h5")
|
| 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:
|
| 24 |
+
ret, frame = cap.read()
|
| 25 |
+
if not ret:
|
| 26 |
+
break
|
| 27 |
+
frames.append(frame)
|
| 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()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=3.0.0
|
| 2 |
+
opencv-python
|
| 3 |
+
numpy
|
| 4 |
+
tensorflow
|
| 5 |
+
keras
|