-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle_thread.py
More file actions
95 lines (68 loc) · 2.29 KB
/
single_thread.py
File metadata and controls
95 lines (68 loc) · 2.29 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import cv2
import time
input_path = "1.mp4"
output_path = "output.mp4"
selected_filters = ["grayscale", "edges"]
# Filter functions
def grayscale(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
return cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
def blur(frame):
return cv2.GaussianBlur(frame, (15, 15), 0)
def edges(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edges = cv2.Canny(blurred, 50, 150)
return cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
def invert(frame):
return cv2.bitwise_not(frame)
FILTERS = {
'grayscale': grayscale,
'blur': blur,
'edges': edges,
'invert': invert,
}
def apply_filters(frame, filters):
for filter_name in filters:
if filter_name in FILTERS:
frame = FILTERS[filter_name](frame)
else:
print(f"Filter '{filter_name}' not found. Skipping.")
return frame
def process_video(input_path, output_path, selected_filters):
cap = cv2.VideoCapture(input_path)
if not cap.isOpened():
print("Error opening video file.")
return
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
processed_frame = apply_filters(frame, selected_filters)
out.write(processed_frame)
# cv2_imshow ( processed_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
durations=[]
def find_mp4_files(directory):
mp4_files = []
for filename in os.listdir(directory):
if filename.endswith(".mp4"):
mp4_files.append(directory+"/"+filename)
return mp4_files
mp4_files=find_mp4_files("./videos")
for input_path in mp4_files:
print(f"Processing file: '{input_path}', to output_path/{input_path}")
now=time.time()
process_video(input_path, f"output_path/{input_path.split("/")[-1]}", selected_filters)
durations.append(time.time() - now)
print(f"Processing Duration: {sum(durations)}(s)")