-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_keyframes.py
206 lines (156 loc) · 5.57 KB
/
find_keyframes.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python
import argparse
import glob
import os
import cv2
import multiprocessing
import sys
import json
from dotenv import load_dotenv
load_dotenv()
template_path = os.getenv('TEMPLATE_PATH', './files/templates/')
process_count = int(os.getenv('PROCESS_COUNT', 4))
similarity_ratio = float(os.getenv('SIMILARITY_RATIO', 0.85))
kickback_ratio = float(os.getenv('KICKBACK_RATIO', 0.1))
skip_frames = int(os.getenv('SKIP_FRAMES_COUNT', 30))
template_in_file_pattern = os.getenv('TEMPLATE_IN_FILE_PATTERN', 'in*')
template_out_file_pattern = os.getenv('TEMPLATE_OUT_FILE_PATTERN', 'out*')
parser = argparse.ArgumentParser(
prog='python find_keyframes.py',
description='Find keyframes into a video based on template images.'
)
parser.add_argument('filename', help="Path to the video file to edit", type=str)
parser.add_argument('--silent', action=argparse.BooleanOptionalAction)
def main():
args = parser.parse_args()
silent = args.silent
if not silent:
print('find_keyframes 1.0 by wushaolin')
path = os.path.abspath(args.filename)
if not silent:
print(' file to process: ' + path)
templates_in = find_templates_files(template_in_file_pattern)
templates_out = find_templates_files(template_out_file_pattern)
cv2.setUseOptimized(True)
in_grays = get_grays(templates_in)
out_grays = get_grays(templates_out)
cap = cv2.VideoCapture(path)
fps = cap.get(cv2.CAP_PROP_FPS)
total_frames = round(cap.get(cv2.CAP_PROP_FRAME_COUNT))
cap.release()
split_total_frames = round(total_frames / process_count)
split_frames_group = split_frames_for_threads(total_frames, split_total_frames)
manager = multiprocessing.Manager()
timecodes = manager.dict()
processes = []
for i in range(process_count):
t = multiprocessing.Process(
target=process_segment,
args=(i, timecodes, split_frames_group[i], path, in_grays, out_grays)
)
processes.append(t)
t.start()
for t in processes:
t.join()
cv2.destroyAllWindows()
keyframes = []
for proc in timecodes.values():
for frame in proc:
keyframes.append(frame)
sys.stdout.write(json.dumps({
"file": path,
"end_time": total_frames / fps,
"keyframes": keyframes
}))
def find_templates_files(pattern):
files = glob.glob(f'{template_path}/{pattern}')
out = []
for file in files:
out.append(os.path.basename(file))
return out
def process_segment(thread_id, timecodes, segment, path, in_templates, out_templates):
tc = []
cap = cv2.VideoCapture(path)
fps = cap.get(cv2.CAP_PROP_FPS)
kickback = [None, None, 1]
i = segment[0]
last_frame_ok = False
while cap.isOpened():
if i > segment[1]:
i = segment[1] - 1
last_frame_ok = True
if last_frame_ok:
break
cap.set(cv2.CAP_PROP_POS_FRAMES, i)
ret, frame = cap.read()
cframe = cap.get(cv2.CAP_PROP_POS_FRAMES) # retrieves the current frame number
# print('Reading : ' + str(cframe) + ' in thread id : ' + str(thread_id))
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
templates = [
['in', in_templates],
['out', out_templates],
]
kickback = process_templates(tc, gray_frame, templates, kickback, cframe, fps)
i = i + skip_frames
cap.release()
timecodes[i] = tc
def process_templates(timecodes, frame, g_templates, kickback, cframe, fps):
if kickback[0] is None or kickback[1] is None:
find = False
i = 0
for g_template in g_templates:
j = 0
for template in g_template[1]:
max_val = get_similarity_ratio(frame, template)
if max_val > similarity_ratio:
timecodes.append({
"frame": cframe,
"type": g_template[0],
"time": cframe / fps
})
kickback[0] = i
kickback[1] = j
kickback[2] = max_val - kickback_ratio
find = True
break
j += 1
if find:
break
i += 1
else:
max_val = get_similarity_ratio(frame, g_templates[kickback[0]][1][kickback[1]])
# skip next frame until we get under the kickback value again
if max_val < kickback[2]:
kickback[0] = None
kickback[1] = None
kickback[2] = 1
return kickback
def split_frames_for_threads(total_frames, split_total_frames):
index = 0
remaining_frames = total_frames
out = []
while remaining_frames > 0:
start = index * split_total_frames
end = (index + 1) * split_total_frames
if abs(remaining_frames - split_total_frames) < split_total_frames:
remaining_frames = 0
end = total_frames
out.append([
start, end
])
remaining_frames -= split_total_frames
index += 1
return out
def get_grays(files):
out = []
for file in files:
search_for = cv2.imread(template_path + file, cv2.IMREAD_UNCHANGED)
search_for_gray = cv2.cvtColor(search_for, cv2.COLOR_BGR2GRAY)
out.append(search_for_gray)
return out
def get_similarity_ratio(frame, template):
result = cv2.matchTemplate(frame, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
return max_val
if __name__ == '__main__':
main()