sdafd commited on
Commit
ae0b2a7
·
verified ·
1 Parent(s): a36a1d6

Update ProcessVideo.py

Browse files
Files changed (1) hide show
  1. ProcessVideo.py +75 -55
ProcessVideo.py CHANGED
@@ -1,57 +1,77 @@
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import cv2
2
+ import numpy as np
3
+ import time
4
+ from FeatureExtraction import FeatureExtractor
5
+ from Prediction import predict_fight
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ def process_video(video_path, sequence_length=40, threshold=0.8, output_frame_rate=30, debug=False):
8
+ try:
9
+ start_time = time.time()
10
+ cap = cv2.VideoCapture(video_path)
11
+ if not cap.isOpened():
12
+ raise ValueError("Could not open video file")
13
+
14
+ all_frames = []
15
+ while cap.isOpened():
16
+ ret, frame = cap.read()
17
+ if not ret:
18
+ break
19
+ all_frames.append(frame)
20
+ cap.release()
21
+ total_frames = len(all_frames)
22
+
23
+ feature_extractor = FeatureExtractor(img_shape=(224, 224), channels=3, seq_length=sequence_length)
24
+
25
+ predictions_list = []
26
+ predictions = []
27
+ for i in range(0, total_frames, sequence_length):
28
+ frames_buffer = all_frames[i:i + sequence_length]
29
+ if len(frames_buffer) < sequence_length:
30
+ frames_buffer += [frames_buffer[-1]] * (sequence_length - len(frames_buffer))
31
+
32
+ fight_detected, fight_prob = predict_fight(frames_buffer, threshold, feature_extractor)
33
+ predictions.append(fight_detected)
34
+ predictions_list.append({
35
+ 'chunk_start_frame': i,
36
+ 'chunk_end_frame': i + sequence_length - 1,
37
+ 'fight_probability': float(fight_prob),
38
+ 'fight_detected': bool(fight_detected)
39
+ })
40
+
41
+ output_video_path = "output_labeled.mp4"
42
+ height, width, _ = all_frames[0].shape
43
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
44
+ out = cv2.VideoWriter(output_video_path, fourcc, output_frame_rate, (width, height))
45
+
46
+ frame_idx = 0
47
+ for pred in predictions:
48
+ label = "Violence Detected!" if pred else "No Violence"
49
+ color = (0, 0, 255) if pred else (0, 255, 0)
50
+ for _ in range(sequence_length):
51
+ if frame_idx >= total_frames:
52
+ break
53
+ frame = all_frames[frame_idx].copy()
54
+ cv2.putText(frame, label, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2)
55
+ out.write(frame)
56
+ frame_idx += 1
57
+ out.release()
58
+
59
+ processing_time = time.time() - start_time
60
+ json_response = {
61
+ 'output_video_path': output_video_path,
62
+ 'total_frames': total_frames,
63
+ 'sequence_length': sequence_length,
64
+ 'threshold': threshold,
65
+ 'output_frame_rate': output_frame_rate,
66
+ 'processing_time_seconds': processing_time,
67
+ 'predictions': predictions_list,
68
+ 'error': None
69
+ }
70
+ return output_video_path, json_response
71
+ except Exception as e:
72
+ error_message = f"Error processing video: {str(e)}"
73
+ json_response = {
74
+ 'output_video_path': None,
75
+ 'error': error_message
76
+ }
77
+ return None, json_response