-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_video.py
65 lines (50 loc) · 1.89 KB
/
make_video.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
import numpy as np
import cv2
from MixtureOfGaussiansOptimized import MixtureOfGaussians, rgb2gray
K = 3 # liczba Gaussów
alpha = 0.5 # współczynnik uczenia
T = 0.85 # próg wag tła
# Open the input video file
cap = cv2.VideoCapture('rickroll.mkv')
# Get the frame dimensions and check if the file is loaded correctly
original_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
original_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
if not cap.isOpened():
print("Error: Could not open video file.")
exit()
# Set scale factor (e.g., 0.5 for 50% reduction)
scale_factor = 0.33
width = 640
height = 360
print(f"Original Dimensions: {original_width}x{original_height}, FPS: {fps}")
print(f"Scaled Dimensions: {width}x{height}")
# Initialize background subtractor
fgbg = cv2.createBackgroundSubtractorMOG2()
# Define the codec and create a VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID') # Use 'XVID' for better compatibility on Linux
out = cv2.VideoWriter('rickroll-opencv.avi', fourcc, fps, (width * 2, height), isColor=False)
mog = MixtureOfGaussians(height, width, K=K)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("End of video or error reading frame.")
break
# Scale down the frame
scaled_frame = cv2.resize(frame, (width, height), interpolation=cv2.INTER_AREA)
frame_gray = rgb2gray(scaled_frame)
mymask = mog.update(frame_gray, alpha, T)
# Apply the background subtractor
fgmask = fgbg.apply(scaled_frame)
# Write the processed frame to the output file
combined = np.hstack((fgmask, mymask))
cv2.imshow('tut', combined)
out.write(combined)
# Optional: Display the scaled frame (uncomment if needed)
# cv2.imshow('Foreground Mask', fgmask)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cap.release()
out.release()
cv2.destroyAllWindows()