-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
66 lines (49 loc) · 2.31 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import cv2
import numpy as np
from tqdm import tqdm
from collections import deque
def extract_motion(input_video_path, output_video_path, delay):
cap = cv2.VideoCapture(input_video_path)
if not cap.isOpened():
raise ValueError("Error opening video file")
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
delay_frames = max(1, int(delay * fps)) # Ensure at least 1 frame delay
frames_to_process = max(0, total_frames - delay_frames)
delayed_frames = deque(maxlen=delay_frames)
# Pre-fill the deque with initial frames
for _ in range(delay_frames):
ret, frame = cap.read()
if not ret:
raise ValueError("Video is too short for the specified delay")
delayed_frames.append(frame)
with tqdm(total=frames_to_process, desc="Processing frames", unit="frame") as pbar:
for _ in range(frames_to_process):
ret, current_frame = cap.read()
if not ret:
break
delayed_frame = delayed_frames.popleft()
delayed_frames.append(current_frame)
# Ensure frames have the same dimensions
current_frame = cv2.resize(current_frame, (width, height))
delayed_frame = cv2.resize(delayed_frame, (width, height))
# Calculate the absolute difference
result_frame = cv2.absdiff(current_frame, delayed_frame)
# Write the result frame to the output video
out.write(result_frame)
pbar.update(1)
cap.release()
out.release()
print(f"Processed {frames_to_process} frames. Output saved to {output_video_path}")
if __name__ == "__main__":
input_video_path = "path/to/your/input/video.mp4" # Change this to your input video path
output_video_path = "path/to/your/output/video.mp4" # Change this to your desired output video path
delay_seconds = 1 # Change this to your desired delay in seconds
try:
extract_motion(input_video_path, output_video_path, delay_seconds)
except Exception as e:
print(f"An error occurred: {str(e)}")