Youuus commited on
Commit
07ece63
·
verified ·
1 Parent(s): 396fcef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -28
app.py CHANGED
@@ -1,7 +1,12 @@
 
 
 
 
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.keras")
@@ -11,13 +16,29 @@ 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
- # Function for uploaded video
20
- def predict_bullying(video):
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  cap = cv2.VideoCapture(video)
22
  frames = []
23
  while True:
@@ -26,49 +47,33 @@ def predict_bullying(video):
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()
 
1
+ # Force install correct Gradio version first
2
+ import os
3
+ os.system("pip install gradio==4.44.0")
4
+
5
  import gradio as gr
6
  import numpy as np
7
  import cv2
8
  from keras.models import load_model
9
+ from collections import deque
10
 
11
  # Load the trained model
12
  model = load_model("bullying_detection_model.keras")
 
16
  SEQUENCE_LENGTH = 16
17
  CLASSES_LIST = ["NonBullying", "Bullying"]
18
 
19
+ # Queue for real-time webcam frames
20
+ frame_queue = deque(maxlen=SEQUENCE_LENGTH)
21
+
22
  # Helper to preprocess frames
23
  def preprocess_frames(frames):
24
  processed = [cv2.resize(f, (IMAGE_HEIGHT, IMAGE_WIDTH)) / 255.0 for f in frames]
25
  return np.array(processed)
26
 
27
+ # Function for webcam real-time prediction
28
+ def predict_webcam(frame):
29
+ frame_queue.append(frame)
30
+ if len(frame_queue) < SEQUENCE_LENGTH:
31
+ return "Collecting frames..."
32
+ input_frames = preprocess_frames(list(frame_queue))
33
+ input_frames = np.expand_dims(input_frames, axis=0)
34
+ preds = model.predict(input_frames, verbose=0)[0]
35
+ pred_idx = np.argmax(preds)
36
+ pred_class = CLASSES_LIST[pred_idx]
37
+ confidence = float(preds[pred_idx])
38
+ return f"Prediction: {pred_class} (Confidence: {confidence:.2f})"
39
+
40
+ # Function for uploaded video prediction
41
+ def predict_video(video):
42
  cap = cv2.VideoCapture(video)
43
  frames = []
44
  while True:
 
47
  break
48
  frames.append(frame)
49
  cap.release()
 
50
  if len(frames) < SEQUENCE_LENGTH:
51
  return "Video too short"
 
52
  # Sample SEQUENCE_LENGTH evenly spaced frames
53
  idxs = np.linspace(0, len(frames) - 1, SEQUENCE_LENGTH).astype(int)
54
  sampled_frames = [frames[i] for i in idxs]
55
  input_frames = preprocess_frames(sampled_frames)
56
  input_frames = np.expand_dims(input_frames, axis=0)
57
+ preds = model.predict(input_frames, verbose=0)[0]
 
58
  pred_idx = np.argmax(preds)
59
  pred_class = CLASSES_LIST[pred_idx]
60
  confidence = float(preds[pred_idx])
61
  return f"Prediction: {pred_class} (Confidence: {confidence:.2f})"
62
 
63
+ # Gradio interface with tabs
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  with gr.Blocks() as demo:
65
+ gr.Markdown("## Bullying Detection: Webcam & Video Upload Demo")
 
 
 
 
 
 
66
 
67
  with gr.Tab("Webcam"):
68
  webcam_output = gr.Label(num_top_classes=1)
69
+ webcam_input = gr.Image(source="webcam", type="numpy", streaming=True)
70
  webcam_input.stream(predict_webcam, outputs=webcam_output)
71
 
72
+ with gr.Tab("Video Upload"):
73
+ video_input = gr.Video()
74
+ video_output = gr.Label(num_top_classes=1)
75
+ video_button = gr.Button("Predict")
76
+ video_button.click(predict_video, inputs=video_input, outputs=video_output)
77
+
78
  if __name__ == "__main__":
79
  demo.launch()