Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ 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")
|
|
@@ -12,29 +11,13 @@ IMAGE_HEIGHT, IMAGE_WIDTH = 64, 64
|
|
| 12 |
SEQUENCE_LENGTH = 16
|
| 13 |
CLASSES_LIST = ["NonBullying", "Bullying"]
|
| 14 |
|
| 15 |
-
# Queue for real-time webcam frames
|
| 16 |
-
frame_queue = deque(maxlen=SEQUENCE_LENGTH)
|
| 17 |
-
|
| 18 |
# Helper to preprocess frames
|
| 19 |
def preprocess_frames(frames):
|
| 20 |
processed = [cv2.resize(f, (IMAGE_HEIGHT, IMAGE_WIDTH)) / 255.0 for f in frames]
|
| 21 |
return np.array(processed)
|
| 22 |
|
| 23 |
-
# Function for
|
| 24 |
-
def
|
| 25 |
-
frame_queue.append(frame)
|
| 26 |
-
if len(frame_queue) < SEQUENCE_LENGTH:
|
| 27 |
-
return "Collecting frames..."
|
| 28 |
-
input_frames = preprocess_frames(list(frame_queue))
|
| 29 |
-
input_frames = np.expand_dims(input_frames, axis=0)
|
| 30 |
-
preds = model.predict(input_frames, verbose=0)[0]
|
| 31 |
-
pred_idx = np.argmax(preds)
|
| 32 |
-
pred_class = CLASSES_LIST[pred_idx]
|
| 33 |
-
confidence = float(preds[pred_idx])
|
| 34 |
-
return f"Prediction: {pred_class} (Confidence: {confidence:.2f})"
|
| 35 |
-
|
| 36 |
-
# Function for uploaded video prediction
|
| 37 |
-
def predict_video(video):
|
| 38 |
cap = cv2.VideoCapture(video)
|
| 39 |
frames = []
|
| 40 |
while True:
|
|
@@ -43,33 +26,49 @@ def predict_video(video):
|
|
| 43 |
break
|
| 44 |
frames.append(frame)
|
| 45 |
cap.release()
|
|
|
|
| 46 |
if len(frames) < SEQUENCE_LENGTH:
|
| 47 |
return "Video too short"
|
|
|
|
| 48 |
# Sample SEQUENCE_LENGTH evenly spaced frames
|
| 49 |
idxs = np.linspace(0, len(frames) - 1, SEQUENCE_LENGTH).astype(int)
|
| 50 |
sampled_frames = [frames[i] for i in idxs]
|
| 51 |
input_frames = preprocess_frames(sampled_frames)
|
| 52 |
input_frames = np.expand_dims(input_frames, axis=0)
|
| 53 |
-
|
|
|
|
| 54 |
pred_idx = np.argmax(preds)
|
| 55 |
pred_class = CLASSES_LIST[pred_idx]
|
| 56 |
confidence = float(preds[pred_idx])
|
| 57 |
return f"Prediction: {pred_class} (Confidence: {confidence:.2f})"
|
| 58 |
|
| 59 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
with gr.Blocks() as demo:
|
| 61 |
-
gr.Markdown("#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
with gr.Tab("Webcam"):
|
| 64 |
webcam_output = gr.Label(num_top_classes=1)
|
| 65 |
-
webcam_input = gr.
|
| 66 |
webcam_input.stream(predict_webcam, outputs=webcam_output)
|
| 67 |
|
| 68 |
-
with gr.Tab("Video Upload"):
|
| 69 |
-
video_input = gr.Video()
|
| 70 |
-
video_output = gr.Label(num_top_classes=1)
|
| 71 |
-
video_button = gr.Button("Predict")
|
| 72 |
-
video_button.click(predict_video, inputs=video_input, outputs=video_output)
|
| 73 |
-
|
| 74 |
if __name__ == "__main__":
|
| 75 |
demo.launch()
|
|
|
|
| 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")
|
|
|
|
| 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 |
+
# Function for uploaded video
|
| 20 |
+
def predict_bullying(video):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
cap = cv2.VideoCapture(video)
|
| 22 |
frames = []
|
| 23 |
while True:
|
|
|
|
| 26 |
break
|
| 27 |
frames.append(frame)
|
| 28 |
cap.release()
|
| 29 |
+
|
| 30 |
if len(frames) < SEQUENCE_LENGTH:
|
| 31 |
return "Video too short"
|
| 32 |
+
|
| 33 |
# Sample SEQUENCE_LENGTH evenly spaced frames
|
| 34 |
idxs = np.linspace(0, len(frames) - 1, SEQUENCE_LENGTH).astype(int)
|
| 35 |
sampled_frames = [frames[i] for i in idxs]
|
| 36 |
input_frames = preprocess_frames(sampled_frames)
|
| 37 |
input_frames = np.expand_dims(input_frames, axis=0)
|
| 38 |
+
|
| 39 |
+
preds = model.predict(input_frames)[0]
|
| 40 |
pred_idx = np.argmax(preds)
|
| 41 |
pred_class = CLASSES_LIST[pred_idx]
|
| 42 |
confidence = float(preds[pred_idx])
|
| 43 |
return f"Prediction: {pred_class} (Confidence: {confidence:.2f})"
|
| 44 |
|
| 45 |
+
# Function for live webcam input (single frame stream)
|
| 46 |
+
def predict_webcam(frame):
|
| 47 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # Convert from Gradio (RGB) to OpenCV (BGR)
|
| 48 |
+
frame_resized = cv2.resize(frame, (IMAGE_HEIGHT, IMAGE_WIDTH)) / 255.0
|
| 49 |
+
frames = np.repeat(frame_resized[np.newaxis, :, :, :], SEQUENCE_LENGTH, axis=0) # Fake sequence
|
| 50 |
+
input_frames = np.expand_dims(frames, axis=0)
|
| 51 |
+
|
| 52 |
+
preds = model.predict(input_frames)[0]
|
| 53 |
+
pred_idx = np.argmax(preds)
|
| 54 |
+
pred_class = CLASSES_LIST[pred_idx]
|
| 55 |
+
confidence = float(preds[pred_idx])
|
| 56 |
+
return {pred_class: confidence}
|
| 57 |
+
|
| 58 |
+
# Build Gradio UI
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
+
gr.Markdown("# 🎥 Real-Time Bullying Detection\nUpload a video or use your webcam.")
|
| 61 |
+
|
| 62 |
+
with gr.Tab("Upload Video"):
|
| 63 |
+
video_input = gr.Video()
|
| 64 |
+
video_output = gr.Textbox()
|
| 65 |
+
video_button = gr.Button("Analyze Video")
|
| 66 |
+
video_button.click(predict_bullying, inputs=video_input, outputs=video_output)
|
| 67 |
|
| 68 |
with gr.Tab("Webcam"):
|
| 69 |
webcam_output = gr.Label(num_top_classes=1)
|
| 70 |
+
webcam_input = gr.Image(source="webcam", streaming=True)
|
| 71 |
webcam_input.stream(predict_webcam, outputs=webcam_output)
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
if __name__ == "__main__":
|
| 74 |
demo.launch()
|