Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image, ImageDraw, ImageFont
|
5 |
+
import tempfile
|
6 |
+
import os
|
7 |
+
|
8 |
+
def process_video_with_mask(video_path, mask_path, text="Joker"):
|
9 |
+
# 创建临时输出文件
|
10 |
+
output_path = os.path.join(tempfile.gettempdir(), "output.mp4")
|
11 |
+
|
12 |
+
# 打开视频和mask视频
|
13 |
+
cap_video = cv2.VideoCapture(video_path)
|
14 |
+
cap_mask = cv2.VideoCapture(mask_path)
|
15 |
+
|
16 |
+
# 获取视频参数
|
17 |
+
fps = cap_video.get(cv2.CAP_PROP_FPS)
|
18 |
+
width = int(cap_video.get(cv2.CAP_PROP_FRAME_WIDTH))
|
19 |
+
height = int(cap_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
20 |
+
|
21 |
+
# 创建视频写入器
|
22 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
23 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
24 |
+
|
25 |
+
while True:
|
26 |
+
ret_video, frame_video = cap_video.read()
|
27 |
+
ret_mask, frame_mask = cap_mask.read()
|
28 |
+
|
29 |
+
if not ret_video or not ret_mask:
|
30 |
+
break
|
31 |
+
|
32 |
+
# 处理mask为单通道
|
33 |
+
if len(frame_mask.shape) == 3:
|
34 |
+
frame_mask = cv2.cvtColor(frame_mask, cv2.COLOR_BGR2GRAY)
|
35 |
+
|
36 |
+
# 找到mask中的白色区域
|
37 |
+
_, binary = cv2.threshold(frame_mask, 200, 255, cv2.THRESH_BINARY)
|
38 |
+
|
39 |
+
# 查找轮廓
|
40 |
+
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
41 |
+
|
42 |
+
# 转换为PIL图像处理文字
|
43 |
+
pil_image = Image.fromarray(cv2.cvtColor(frame_video, cv2.COLOR_BGR2RGB))
|
44 |
+
draw = ImageDraw.Draw(pil_image)
|
45 |
+
|
46 |
+
for contour in contours:
|
47 |
+
# 获取旋转矩形
|
48 |
+
rect = cv2.minAreaRect(contour)
|
49 |
+
box = cv2.boxPoints(rect)
|
50 |
+
box = np.int0(box)
|
51 |
+
|
52 |
+
# 计算中心点和尺寸
|
53 |
+
center = rect[0]
|
54 |
+
size = rect[1]
|
55 |
+
angle = rect[2]
|
56 |
+
|
57 |
+
# 创建透明画布绘制文字
|
58 |
+
text_image = Image.new("RGBA", (int(size[0]), int(size[1])), (0, 0, 0, 0))
|
59 |
+
text_draw = ImageDraw.Draw(text_image)
|
60 |
+
|
61 |
+
try:
|
62 |
+
# 自动调整字体大小
|
63 |
+
font_size = int(min(size[0] * 0.8 / max(1, len(text)), size[1] * 0.8))
|
64 |
+
font = ImageFont.truetype("华文琥珀.ttf", font_size)
|
65 |
+
except:
|
66 |
+
font = ImageFont.load_default()
|
67 |
+
|
68 |
+
# 计算文字位置
|
69 |
+
bbox = text_draw.textbbox((0, 0), text, font=font)
|
70 |
+
text_w = bbox[2] - bbox[0]
|
71 |
+
text_h = bbox[3] - bbox[1]
|
72 |
+
pos_x = (size[0] - text_w) // 2
|
73 |
+
pos_y = (size[1] - text_h) // 2
|
74 |
+
|
75 |
+
# 绘制文字
|
76 |
+
text_draw.text((pos_x, pos_y), text, fill=(255, 255, 0, 255), font=font)
|
77 |
+
|
78 |
+
# 旋转文字
|
79 |
+
rotated_text = text_image.rotate(angle, expand=True, center=(size[0]/2, size[1]/2))
|
80 |
+
|
81 |
+
# 计算粘贴位置
|
82 |
+
paste_x = int(center[0] - rotated_text.width / 2)
|
83 |
+
paste_y = int(center[1] - rotated_text.height / 2)
|
84 |
+
|
85 |
+
# 合并到原图
|
86 |
+
pil_image.paste(rotated_text, (paste_x, paste_y), rotated_text)
|
87 |
+
|
88 |
+
# 转换回OpenCV格式
|
89 |
+
frame_processed = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
|
90 |
+
out.write(frame_processed)
|
91 |
+
|
92 |
+
# 释放资源
|
93 |
+
cap_video.release()
|
94 |
+
cap_mask.release()
|
95 |
+
out.release()
|
96 |
+
|
97 |
+
return output_path
|
98 |
+
|
99 |
+
# 创建Gradio界面
|
100 |
+
with gr.Blocks() as demo:
|
101 |
+
gr.Markdown("## 视频文字替换工具")
|
102 |
+
with gr.Row():
|
103 |
+
with gr.Column():
|
104 |
+
video_input = gr.Video(label="原始视频")
|
105 |
+
mask_input = gr.Video(label="Mask视频")
|
106 |
+
text_input = gr.Textbox(label="要添加的文字", value="示例文字")
|
107 |
+
submit_btn = gr.Button("处理视频")
|
108 |
+
with gr.Column():
|
109 |
+
video_output = gr.Video(label="处理结果")
|
110 |
+
|
111 |
+
submit_btn.click(
|
112 |
+
fn=process_video_with_mask,
|
113 |
+
inputs=[video_input, mask_input, text_input],
|
114 |
+
outputs=video_output,
|
115 |
+
examples = [
|
116 |
+
["examples/20250511-013442_3331156457.mp4", "examples/20250511-013442_3331156457__temp.mp4_alpha_temp.mp4", "今天天气怎么样?"]
|
117 |
+
]
|
118 |
+
)
|
119 |
+
|
120 |
+
if __name__ == "__main__":
|
121 |
+
demo.launch(share = True)
|