-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepass.py
185 lines (139 loc) · 6.22 KB
/
prepass.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import os
import sys
import threading
import time
from queue import Queue
import debug
import cv2
import numpy as np
import plotting
import processing
import table_print
is_finished = False
progress_callback = None
thread: threading.Thread = None
stop_thread_event: threading.Event = None
first_frame_brightness: np.double = None
def calculate_avg_brightness(frame):
# return frame.sum() // frame.size
return np.mean(frame)
def normalized_frame(frame, b_value):
delta_brightness = b_value / calculate_avg_brightness(frame)
new_frame = cv2.convertScaleAbs(frame, alpha=delta_brightness, beta=0)
return new_frame
def print_as_table_row(i, curr, first, delta, to_debug=False):
# print(
# f"[{i}] {curr} (Difference: {curr - first} | {delta})")
index_space = 4 - len(str(i))
curr_space = 22 - len(str(curr))
diff_space = 22 - len(str(curr - first))
delta_space = 22 - len(str(delta))
line = (" | " + " " * index_space + str(i) +
" | " + " " * curr_space + str(curr) +
" | " + " " * diff_space + str(curr - first) +
" | " + " " * delta_space + str(delta))
if to_debug:
color = "blue"
if i % 2 == 0:
color = "magenta"
debug.log(line, text_color=color)
else:
print(line)
def preprocess(path, to_plot):
global is_finished, stop_thread_event, progress_callback
p_callback = progress_callback
stop_thread_event = threading.Event()
is_finished = False
if not is_finished:
debug.log("[Preprocessing] Entered preprocess method\n")
start_time = time.time()
cap = cv2.VideoCapture(path)
new_path = path[:-4] + '_prepass.mp4'
ret, first_frame = cap.read()
frame_height, frame_width = first_frame.shape[:2]
resized_frame_height, resized_frame_width = frame_width // 2, frame_height // 2
resized_first_frame = cv2.resize(first_frame, (resized_frame_width, resized_frame_height))
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
codec = cv2.VideoWriter_fourcc(*'H264')
output = cv2.VideoWriter(new_path, codec, 95, (frame_width, frame_height))
curr_index = 0
first_frame_brightness = calculate_avg_brightness(resized_first_frame)
b_list, prepass_b_list = [], []
if not stop_thread_event.is_set():
debug.log("[Preprocessing] Preprocessing started")
table_print.prepass_table_print("Frame", "Brightness value", "Brightness ratio")
frame_since_callback = 0
while not stop_thread_event.is_set():
ret, frame = cap.read()
if ret:
resized_frame = cv2.resize(frame, (resized_frame_width, resized_frame_height))
else:
break
curr_index += 1
frame_since_callback += 1
curr_brightness = calculate_avg_brightness(resized_frame)
b_list.append(curr_brightness)
# delta_brightness = 1 - ((curr_brightness - first_frame_brightness) / curr_brightness)
delta_brightness = first_frame_brightness / curr_brightness
if curr_brightness - first_frame_brightness == 0:
delta_brightness = 1
table_print.prepass_table_print(curr_index, curr_brightness, delta_brightness)
# table_print.table_print([curr_index, curr_brightness, first_frame_brightness, delta_brightness])
frame = cv2.convertScaleAbs(frame, alpha=delta_brightness, beta=0)
prepass_b_list.append(calculate_avg_brightness(frame))
output.write(frame)
if frame_since_callback == 2:
processing.callback_queue.put(
lambda: p_callback("preprocessing", int("{:.0f}".format((curr_index * 100) / total_frames))))
# p_callback("preprocessing", int("{:.0f}".format((curr_index * 100) / total_frames)))
# debug.log("{:.0f}".format((curr_index * 100) / total_frames))
frame_since_callback = 0
cap.release()
output.release()
if to_plot:
debug.log("[Preprocessing] Creating graph for brightness regulation...", text_color="yellow")
plotting.plot_average_brightness(before_list=b_list,
after_list=prepass_b_list,
path=path)
debug.log("[Preprocessing] Plot created!", text_color="yellow")
debug.log(f"[Preprocessing] Preprocessing finished in {"{:.2f}s".format(time.time() - start_time)}\n",
text_color="cyan")
processing.callback_queue.put(lambda: p_callback("preprocessing", 100))
is_finished = True
def set_progress_callback(callback):
"""
Sets the progress callback function.
This function sets the callback function that will be called during video processing
to report progress.
Parameters:
callback (function): The callback function to report progress.
"""
global progress_callback
progress_callback = callback
def stop_prepass_thread():
global thread, stop_thread_event, is_finished, force_stopped
if stop_thread_event is not None:
stop_thread_event.set()
force_stopped = True
time.sleep(0.5) # To wait for the current cycle to finish
debug.log("[Preprocessing] Preprocessing Thread event set!")
if thread is not None:
debug.log("[Preprocessing] Joining preprocessing thread...")
if stop_thread_event is not None and stop_thread_event.is_set():
thread.join()
debug.log("[Preprocessing] Preprocessing thread joined!")
is_finished = True
debug.log("[Preprocessing] Stopped preprocessing thread!", text_color="blue")
def preprocess_video_thread(path, to_plot):
"""
Creates a thread for video processing.
This function creates a separate thread to process the video specified by `path`.
Parameters:
path (str): The path to the video file.
"""
global progress_callback, thread
# if progress_callback is None:
# debug.log("Progress callback not set. Aborting video processing.", text_color="red")
# return
thread = threading.Thread(target=preprocess, args=(path, to_plot))
thread.start()