Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -23,10 +23,11 @@ def process_video_with_mask(video_path, mask_path, text="Joker"):
|
|
23 |
width = int(cap_video.get(cv2.CAP_PROP_FRAME_WIDTH))
|
24 |
height = int(cap_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
25 |
|
26 |
-
#
|
|
|
27 |
fixed_angle = None
|
28 |
-
current_pos = cap_video.get(cv2.CAP_PROP_POS_FRAMES)
|
29 |
|
|
|
30 |
while fixed_angle is None:
|
31 |
ret_video, frame_video = cap_video.read()
|
32 |
ret_mask, frame_mask = cap_mask.read()
|
@@ -50,16 +51,21 @@ def process_video_with_mask(video_path, mask_path, text="Joker"):
|
|
50 |
|
51 |
# 获取旋转矩形
|
52 |
rect = cv2.minAreaRect(max_contour)
|
|
|
53 |
current_angle = rect[2]
|
54 |
|
|
|
|
|
|
|
|
|
55 |
# 检查是否接近水平
|
56 |
if is_horizontal_angle(current_angle):
|
57 |
fixed_angle = current_angle
|
58 |
|
59 |
# 如果没有找到接近水平的角度,使用第一个找到的角度
|
60 |
if fixed_angle is None:
|
61 |
-
cap_video.set(cv2.CAP_PROP_POS_FRAMES,
|
62 |
-
cap_mask.set(cv2.CAP_PROP_POS_FRAMES,
|
63 |
|
64 |
ret_video, frame_video = cap_video.read()
|
65 |
ret_mask, frame_mask = cap_mask.read()
|
@@ -88,7 +94,7 @@ def process_video_with_mask(video_path, mask_path, text="Joker"):
|
|
88 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
89 |
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
90 |
|
91 |
-
#
|
92 |
while True:
|
93 |
ret_video, frame_video = cap_video.read()
|
94 |
ret_mask, frame_mask = cap_mask.read()
|
@@ -116,10 +122,6 @@ def process_video_with_mask(video_path, mask_path, text="Joker"):
|
|
116 |
|
117 |
# 获取旋转矩形
|
118 |
rect = cv2.minAreaRect(max_contour)
|
119 |
-
box = cv2.boxPoints(rect)
|
120 |
-
box = np.int0(box)
|
121 |
-
|
122 |
-
# 计算中心点和尺寸
|
123 |
center = rect[0]
|
124 |
size = rect[1]
|
125 |
|
@@ -128,9 +130,7 @@ def process_video_with_mask(video_path, mask_path, text="Joker"):
|
|
128 |
text_draw = ImageDraw.Draw(text_image)
|
129 |
|
130 |
try:
|
131 |
-
|
132 |
-
font_size = int(min(size[0] * 0.8 / max(1, len(text)), size[1] * 0.8))
|
133 |
-
font = ImageFont.truetype("华文琥珀.ttf", font_size)
|
134 |
except:
|
135 |
font = ImageFont.load_default()
|
136 |
|
@@ -185,7 +185,9 @@ with gr.Blocks() as demo:
|
|
185 |
|
186 |
gr.Examples(
|
187 |
[
|
188 |
-
["examples/20250511-013442_3331156457_.mp4", "examples/20250511-013442_3331156457__temp.mp4_alpha_temp.mp4", "今天天气怎么样?"]
|
|
|
|
|
189 |
],
|
190 |
[video_input, mask_input, text_input]
|
191 |
)
|
|
|
23 |
width = int(cap_video.get(cv2.CAP_PROP_FRAME_WIDTH))
|
24 |
height = int(cap_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
25 |
|
26 |
+
# 初始化固定参数
|
27 |
+
fixed_font_size = None
|
28 |
fixed_angle = None
|
|
|
29 |
|
30 |
+
# 第一阶段:寻找第一个接近水平的角度和计算字体大小
|
31 |
while fixed_angle is None:
|
32 |
ret_video, frame_video = cap_video.read()
|
33 |
ret_mask, frame_mask = cap_mask.read()
|
|
|
51 |
|
52 |
# 获取旋转矩形
|
53 |
rect = cv2.minAreaRect(max_contour)
|
54 |
+
size = rect[1]
|
55 |
current_angle = rect[2]
|
56 |
|
57 |
+
# 计算基于mask大小的字体大小
|
58 |
+
if fixed_font_size is None:
|
59 |
+
fixed_font_size = int(min(size[0] * 0.8 / max(1, len(text)), size[1] * 0.8))
|
60 |
+
|
61 |
# 检查是否接近水平
|
62 |
if is_horizontal_angle(current_angle):
|
63 |
fixed_angle = current_angle
|
64 |
|
65 |
# 如果没有找到接近水平的角度,使用第一个找到的角度
|
66 |
if fixed_angle is None:
|
67 |
+
cap_video.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
68 |
+
cap_mask.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
69 |
|
70 |
ret_video, frame_video = cap_video.read()
|
71 |
ret_mask, frame_mask = cap_mask.read()
|
|
|
94 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
95 |
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
96 |
|
97 |
+
# 第二阶段:使用固定角度和固定字体大小处理视频
|
98 |
while True:
|
99 |
ret_video, frame_video = cap_video.read()
|
100 |
ret_mask, frame_mask = cap_mask.read()
|
|
|
122 |
|
123 |
# 获取旋转矩形
|
124 |
rect = cv2.minAreaRect(max_contour)
|
|
|
|
|
|
|
|
|
125 |
center = rect[0]
|
126 |
size = rect[1]
|
127 |
|
|
|
130 |
text_draw = ImageDraw.Draw(text_image)
|
131 |
|
132 |
try:
|
133 |
+
font = ImageFont.truetype("华文琥珀.ttf", fixed_font_size)
|
|
|
|
|
134 |
except:
|
135 |
font = ImageFont.load_default()
|
136 |
|
|
|
185 |
|
186 |
gr.Examples(
|
187 |
[
|
188 |
+
["examples/20250511-013442_3331156457_.mp4", "examples/20250511-013442_3331156457__temp.mp4_alpha_temp.mp4", "今天天气怎么样?"],
|
189 |
+
["examples/20250510-123229_332745243_.mp4", "examples/20250510-123229_332745243__temp.mp4_alpha_temp.mp4", "今天天气怎么样?"],
|
190 |
+
["examples/20250510-121949_3109246688_.mp4", "examples/20250510-121949_3109246688__temp.mp4_alpha_temp.mp4", "今天天气怎么样?"],
|
191 |
],
|
192 |
[video_input, mask_input, text_input]
|
193 |
)
|