-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.py
More file actions
299 lines (238 loc) · 12 KB
/
renderer.py
File metadata and controls
299 lines (238 loc) · 12 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import os
import cv2
import pickle
import glob
import numpy as np
from tqdm import tqdm
from stt_engine import create_subtitles
import subprocess
# ArgumentParser 대신 간단한 설정 변수를 사용합니다.
class Args:
def __init__(self):
# TODO: 1단계에서 사용한 videoName과 videoFolder에 맞게 수정하세요.
self.videoName = 'sample'
self.videoFolder = 'demo'
self.savePath = os.path.join(self.videoFolder, self.videoName)
self.pyframesPath = os.path.join(self.savePath, 'pyframes')
self.pyworkPath = os.path.join(self.savePath, 'pywork')
self.pyaviPath = os.path.join(self.savePath, 'pyavi')
self.nDataLoaderThread = 4
args = Args()
# --- 헬퍼 함수 ---
def is_overlap(bbox1, bbox2):
"""
두 바운딩 박스가 겹치는지 확인합니다.
bbox = [x_center, y_center, side]
"""
x1, y1, s1 = bbox1
x2, y2, s2 = bbox2
# 박스 경계선 계산
left1, right1 = x1 - s1, x1 + s1
top1, bottom1 = y1 - s1, y1 + s1
left2, right2 = x2 - s2, x2 + s2
top2, bottom2 = y2 - s2, y2 + s2
# 겹치지 않는 경우
if right1 < left2 or right2 < left1 or bottom1 < top2 or bottom2 < top1:
return False
return True
def render_subtitles(tracks, scores, args, subtitles_data,
font_size,
font_color,
thickness,
bubble_color,
bubble_alpha,
padding):
flist = glob.glob(os.path.join(args.pyframesPath, '*.jpg'))
flist.sort()
faces = [[] for _ in range(len(flist))]
for tidx, track in enumerate(tracks):
# print(f"Track ID: {tidx}, First frame: {track['track']['frame'].tolist()[0]}")
score = scores[tidx]
for fidx, frame in enumerate(track['track']['frame'].tolist()):
s_smoothed = np.mean(score[max(fidx - 2, 0): min(fidx + 3, len(score) - 1)])
faces[frame].append({
'track': tidx,
'score': float(s_smoothed),
's': track['proc_track']['s'][fidx],
'x': track['proc_track']['x'][fidx],
'y': track['proc_track']['y'][fidx]
})
firstImage = cv2.imread(flist[0])
fw, fh = firstImage.shape[1], firstImage.shape[0]
vOut = cv2.VideoWriter(os.path.join(args.pyaviPath, 'video_rendered.avi'), cv2.VideoWriter_fourcc(*'XVID'), 25, (fw, fh))
# === 안정화 로직에 필요한 변수들 ===
last_direction = 'bottom' # 초기값 설정
direction_hold_duration = 0 # 현재 방향이 유지된 프레임 수
STABILITY_THRESHOLD = 12 # 0.5초 (25fps 기준 12프레임)
SAFE_MARGIN = 50 # 화면 끝에서 최소 20px 여유
safe_left, safe_top = SAFE_MARGIN, SAFE_MARGIN
safe_right, safe_bottom = fw - SAFE_MARGIN, fh - SAFE_MARGIN
for fidx, fname in tqdm(enumerate(flist), total=len(flist)):
image = cv2.imread(fname)
current_faces = faces[fidx]
if not current_faces:
vOut.write(image)
continue
best_speaker = max(current_faces, key=lambda f: f['score'], default=None)
if best_speaker and best_speaker['score'] >= 0.5 and fidx in subtitles_data and best_speaker['track'] in subtitles_data[fidx]:
speaker_bbox = (best_speaker['x'], best_speaker['y'], best_speaker['s'])
subtitle_text = subtitles_data[fidx][best_speaker['track']]
# --- 말풍선 위치 계산 (가장 넓은 여백 찾기) ---
other_bboxes = [
(face['x'], face['y'], face['s'])
for face in current_faces if face['track'] != best_speaker['track']
]
(text_w, text_h), _ = cv2.getTextSize(subtitle_text, cv2.FONT_HERSHEY_SIMPLEX, font_size, thickness)
bubble_width = text_w + padding * 2
bubble_height = text_h + padding * 2
x_center, y_center, s = speaker_bbox
top_dist = (y_center - s) - safe_top
top_space = top_dist - bubble_height
for obbox in other_bboxes:
if is_overlap([x_center, y_center - s - bubble_height/2, bubble_width/2], obbox):
top_space = -1
break
bottom_dist = safe_bottom - (y_center + s)
bottom_space = bottom_dist - bubble_height
for obbox in other_bboxes:
if is_overlap([x_center, y_center + s + bubble_height/2, bubble_width/2], obbox):
bottom_space = -1
break
left_dist = (x_center - s) - safe_left
left_space = left_dist - bubble_width
for obbox in other_bboxes:
if is_overlap([x_center - s - bubble_width/2, y_center, bubble_width/2], obbox):
left_space = -1
break
right_dist = safe_right - (x_center + s)
right_space = right_dist - bubble_width
for obbox in other_bboxes:
if is_overlap([x_center + s + bubble_width/2, y_center, bubble_width/2], obbox):
right_space = -1
break
# 5. 가장 넓은 여백 선택
spaces = {'top': top_space, 'bottom': bottom_space, 'left': left_space, 'right': right_space}
valid_spaces = {k: v for k, v in spaces.items() if v >= 0}
if not valid_spaces:
current_direction = 'bottom'
else:
current_direction = max(valid_spaces, key=valid_spaces.get)
# === 안정화 로직 ===
if current_direction == last_direction:
direction_hold_duration += 1
else:
direction_hold_duration = 0
best_direction_to_render = last_direction
if direction_hold_duration >= STABILITY_THRESHOLD:
best_direction_to_render = current_direction
last_direction = current_direction
direction_hold_duration = 0
best_direction = best_direction_to_render
# --- 말풍선 및 연결선 위치 계산 ---
line_start_x, line_start_y = int(x_center), int(y_center)
bubble_top_left, bubble_bottom_right = (0, 0), (0, 0)
text_pos = (0, 0)
bubble_margin = 30
if best_direction == 'top':
# 연결선 시작점을 바운딩 박스 상단 중앙으로 설정
line_start_y = int(y_center - s)
bubble_x = x_center
bubble_y = y_center - s - bubble_height // 2 - bubble_margin
line_end = (int(bubble_x), int(bubble_y + bubble_height // 2))
elif best_direction == 'bottom':
# 연결선 시작점을 바운딩 박스 하단 중앙으로 설정
line_start_y = int(y_center + s)
bubble_x = x_center
bubble_y = y_center + s + bubble_height // 2 + bubble_margin
line_end = (int(bubble_x), int(bubble_y - bubble_height // 2))
elif best_direction == 'left':
# 연결선 시작점을 바운딩 박스 좌측 중앙으로 설정
line_start_x = int(x_center - s)
bubble_x = x_center - s - bubble_width // 2 - bubble_margin
bubble_y = y_center
line_end = (int(bubble_x + bubble_width // 2), int(bubble_y))
elif best_direction == 'right':
# 연결선 시작점을 바운딩 박스 우측 중앙으로 설정
line_start_x = int(x_center + s)
bubble_x = x_center + s + bubble_width // 2 + bubble_margin
bubble_y = y_center
line_end = (int(bubble_x - bubble_width // 2), int(bubble_y))
# 말풍선 좌표 계산
bubble_x = max(SAFE_MARGIN + bubble_width // 2, min(fw - SAFE_MARGIN - bubble_width // 2, bubble_x))
bubble_y = max(SAFE_MARGIN + bubble_height // 2, min(fh - SAFE_MARGIN - bubble_height // 2, bubble_y))
bubble_top_left = (int(bubble_x - bubble_width / 2), int(bubble_y - bubble_height / 2))
bubble_bottom_right = (int(bubble_x + bubble_width / 2), int(bubble_y + bubble_height / 2))
text_pos = (bubble_top_left[0] + padding, bubble_top_left[1] + padding + text_h)
# 6. 렌더링
# 연결선 그리기
cv2.line(image, (line_start_x, line_start_y), line_end, (255, 255, 255), 2)
# 반투명 사각형 그리기
overlay = image.copy()
cv2.rectangle(overlay, bubble_top_left, bubble_bottom_right, bubble_color, -1)
cv2.addWeighted(overlay, bubble_alpha, image, 1-bubble_alpha, 0, image)
# 텍스트 그리기
cv2.putText(image, subtitle_text, text_pos, cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, thickness)
# 바운딩 박스 그리기 (주석 처리 또는 삭제)
# for face in current_faces:
# clr_box = (0, 255, 0) if face == best_speaker and face['score'] >= 0.5 else (0, 0, 255)
# cv2.rectangle(image, (int(face['x']-face['s']), int(face['y']-face['s'])), (int(face['x']+face['s']), int(face['y']+face['s'])), clr_box, 10)
vOut.write(image)
vOut.release()
command = ("ffmpeg -y -i %s -i %s -threads %d -c:v copy -c:a copy %s -loglevel panic" %
(os.path.join(args.pyaviPath, 'video_rendered.avi'), os.path.join(args.pyaviPath, 'audio.wav'),
args.nDataLoaderThread, os.path.join(args.pyaviPath, 'final_output.avi')))
os.system(command)
subprocess.run([
'ffmpeg',
'-y',
'-i', os.path.join(args.pyaviPath, 'final_output.avi'),
'-c:v', 'libx264',
'-c:a', 'aac',
'-strict', 'experimental',
os.path.join(args.pyaviPath, 'final_output.mp4')
])
def execute_renderer(font_size,
font_color,
thickness,
bubble_color,
bubble_alpha,
padding):
tracks_path = os.path.join(args.pyworkPath, 'tracks.pckl')
scores_path = os.path.join(args.pyworkPath, 'scores.pckl')
if not os.path.exists(tracks_path) or not os.path.exists(scores_path):
print(f"Error: {tracks_path} or {scores_path} not found. Please run the 1st step first.")
return
with open(tracks_path, 'rb') as f:
tracks = pickle.load(f)
with open(scores_path, 'rb') as f:
scores = pickle.load(f)
print("Successfully loaded tracks and scores. Starting rendering...")
print("STT engine - creating subtitles data START...")
subtitles_data = create_subtitles(args, len(tracks))
print("STT engine - creating subtitles data COMPLETED !")
# ---------------------------
render_subtitles(tracks, scores, args, subtitles_data, font_size, font_color, thickness, bubble_color, bubble_alpha, padding)
print("===========================================================")
print("Rendering complete. Final video saved as 'final_output.avi'")
def main():
print("renderer main")
# tracks_path = os.path.join(args.pyworkPath, 'tracks.pckl')
# scores_path = os.path.join(args.pyworkPath, 'scores.pckl')
# if not os.path.exists(tracks_path) or not os.path.exists(scores_path):
# print(f"Error: {tracks_path} or {scores_path} not found. Please run the 1st step first.")
# return
# with open(tracks_path, 'rb') as f:
# tracks = pickle.load(f)
# with open(scores_path, 'rb') as f:
# scores = pickle.load(f)
# print("Successfully loaded tracks and scores. Starting rendering...")
# # --- 이 부분이 수정됩니다. ---
# print("STT 자막 데이터 생성 시작...")
# # stt_engine.py의 함수를 호출하여 자막 데이터를 생성합니다.
# subtitles_data = create_subtitles(args, len(tracks))
# print("STT 자막 데이터 생성 완료.")
# # ---------------------------
# render_subtitles(tracks, scores, args, subtitles_data)
# print("Rendering complete. Final video saved as 'final_output.avi'")
if __name__ == '__main__':
main()