Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- ProcessVideo.py +57 -0
- deployement.py +18 -0
ProcessVideo.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
from Prediction import predict_fight # Assuming you have this
|
3 |
+
|
4 |
+
# Process video: read, predict, and output labeled video
|
5 |
+
def process_video(video_path):
|
6 |
+
cap = cv2.VideoCapture(video_path)
|
7 |
+
sequence_length = 40 # Number of frames for one prediction
|
8 |
+
all_frames = []
|
9 |
+
predictions = []
|
10 |
+
|
11 |
+
# Step 1: Read all frames from the video
|
12 |
+
while cap.isOpened():
|
13 |
+
ret, frame = cap.read()
|
14 |
+
if not ret:
|
15 |
+
break
|
16 |
+
all_frames.append(frame)
|
17 |
+
cap.release()
|
18 |
+
|
19 |
+
# Step 2: Process frames in chunks of 40 to make predictions
|
20 |
+
for i in range(0, len(all_frames), sequence_length):
|
21 |
+
frames_buffer = all_frames[i:i + sequence_length] # Get a batch of 40 frames
|
22 |
+
|
23 |
+
# If the number of frames is less than 40 at the end, pad it with the last frame
|
24 |
+
if len(frames_buffer) < sequence_length:
|
25 |
+
frames_buffer += [frames_buffer[-1]] * (sequence_length - len(frames_buffer))
|
26 |
+
|
27 |
+
# Perform the prediction on the current batch of frames
|
28 |
+
fight_detected = predict_fight(frames_buffer)
|
29 |
+
|
30 |
+
# Store the prediction for this batch
|
31 |
+
predictions.append(fight_detected)
|
32 |
+
|
33 |
+
# Step 3: Create output video with labels
|
34 |
+
output_video_path = "output_labeled.mp4"
|
35 |
+
height, width, _ = all_frames[0].shape
|
36 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
37 |
+
out = cv2.VideoWriter(output_video_path, fourcc, 30, (width, height)) # Adjust frame rate if needed
|
38 |
+
|
39 |
+
frame_idx = 0
|
40 |
+
for pred in predictions:
|
41 |
+
label = "Violence Detected!" if pred else "No Violence"
|
42 |
+
color = (0, 0, 255) if pred else (0, 255, 0)
|
43 |
+
|
44 |
+
# For the next 40 frames, show the same label
|
45 |
+
for _ in range(sequence_length):
|
46 |
+
if frame_idx >= len(all_frames):
|
47 |
+
break
|
48 |
+
|
49 |
+
frame = all_frames[frame_idx]
|
50 |
+
cv2.putText(frame, label, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2)
|
51 |
+
out.write(frame)
|
52 |
+
frame_idx += 1
|
53 |
+
|
54 |
+
out.release()
|
55 |
+
|
56 |
+
return output_video_path
|
57 |
+
|
deployement.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import gradio as gr
|
5 |
+
from ProcessVideo import process_video
|
6 |
+
|
7 |
+
# Gradio function to handle the video upload and processing
|
8 |
+
def gradio_video_processing(video):
|
9 |
+
video_path = video.name # Gradio provides the uploaded video file path
|
10 |
+
output_video_path = process_video(video_path) # Process the video and get the output path
|
11 |
+
return output_video_path
|
12 |
+
|
13 |
+
# Gradio Interface
|
14 |
+
video_input = gr.inputs.Video(label="Upload a Video")
|
15 |
+
video_output = gr.outputs.Video(label="Processed Video with Labels")
|
16 |
+
|
17 |
+
# Create and launch Gradio interface
|
18 |
+
gr.Interface(fn=gradio_video_processing, inputs=video_input, outputs=video_output, title="Fight Detection in Video").launch()
|