Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,11 @@ 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")
|
|
@@ -17,17 +22,12 @@ def process_video_with_mask(video_path, mask_path, text="Joker"):
|
|
| 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 |
-
total_frames = int(cap_video.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 21 |
-
|
| 22 |
-
# 第一阶段:计算所有帧的平均角度
|
| 23 |
-
angles = []
|
| 24 |
-
frame_positions = []
|
| 25 |
|
| 26 |
-
#
|
|
|
|
| 27 |
current_pos = cap_video.get(cv2.CAP_PROP_POS_FRAMES)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
for _ in range(total_frames):
|
| 31 |
ret_video, frame_video = cap_video.read()
|
| 32 |
ret_mask, frame_mask = cap_mask.read()
|
| 33 |
|
|
@@ -50,21 +50,45 @@ def process_video_with_mask(video_path, mask_path, text="Joker"):
|
|
| 50 |
|
| 51 |
# 获取旋转矩形
|
| 52 |
rect = cv2.minAreaRect(max_contour)
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
# 重置视频位置
|
| 60 |
-
cap_video.set(cv2.CAP_PROP_POS_FRAMES,
|
| 61 |
-
cap_mask.set(cv2.CAP_PROP_POS_FRAMES,
|
| 62 |
|
| 63 |
# 创建视频写入器
|
| 64 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 65 |
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 66 |
|
| 67 |
-
#
|
| 68 |
while True:
|
| 69 |
ret_video, frame_video = cap_video.read()
|
| 70 |
ret_mask, frame_mask = cap_mask.read()
|
|
@@ -120,8 +144,8 @@ def process_video_with_mask(video_path, mask_path, text="Joker"):
|
|
| 120 |
# 绘制文字
|
| 121 |
text_draw.text((pos_x, pos_y), text, fill=(255, 255, 0, 255), font=font)
|
| 122 |
|
| 123 |
-
#
|
| 124 |
-
rotated_text = text_image.rotate(
|
| 125 |
|
| 126 |
# 计算粘贴位置
|
| 127 |
paste_x = int(center[0] - rotated_text.width / 2)
|
|
|
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
def is_horizontal_angle(angle, threshold=15):
|
| 9 |
+
"""判断角度是否接近水平(0度或180度)"""
|
| 10 |
+
angle = angle % 180 # 将角度归一化到0-180度范围
|
| 11 |
+
return abs(angle) < threshold or abs(angle - 180) < threshold
|
| 12 |
+
|
| 13 |
def process_video_with_mask(video_path, mask_path, text="Joker"):
|
| 14 |
# 创建临时输出文件
|
| 15 |
output_path = os.path.join(tempfile.gettempdir(), "output.mp4")
|
|
|
|
| 22 |
fps = cap_video.get(cv2.CAP_PROP_FPS)
|
| 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()
|
| 33 |
|
|
|
|
| 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, current_pos)
|
| 62 |
+
cap_mask.set(cv2.CAP_PROP_POS_FRAMES, current_pos)
|
| 63 |
+
|
| 64 |
+
ret_video, frame_video = cap_video.read()
|
| 65 |
+
ret_mask, frame_mask = cap_mask.read()
|
| 66 |
+
|
| 67 |
+
if ret_video and ret_mask:
|
| 68 |
+
if len(frame_mask.shape) == 3:
|
| 69 |
+
frame_mask = cv2.cvtColor(frame_mask, cv2.COLOR_BGR2GRAY)
|
| 70 |
+
|
| 71 |
+
_, binary = cv2.threshold(frame_mask, 200, 255, cv2.THRESH_BINARY)
|
| 72 |
+
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 73 |
+
|
| 74 |
+
if contours:
|
| 75 |
+
max_contour = max(contours, key=cv2.contourArea)
|
| 76 |
+
rect = cv2.minAreaRect(max_contour)
|
| 77 |
+
fixed_angle = rect[2]
|
| 78 |
|
| 79 |
+
# 如果还是没有角度,默认使用0度
|
| 80 |
+
if fixed_angle is None:
|
| 81 |
+
fixed_angle = 0
|
| 82 |
|
| 83 |
# 重置视频位置
|
| 84 |
+
cap_video.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
| 85 |
+
cap_mask.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
| 86 |
|
| 87 |
# 创建视频写入器
|
| 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()
|
|
|
|
| 144 |
# 绘制文字
|
| 145 |
text_draw.text((pos_x, pos_y), text, fill=(255, 255, 0, 255), font=font)
|
| 146 |
|
| 147 |
+
# 使用固定角度旋转文字
|
| 148 |
+
rotated_text = text_image.rotate(fixed_angle, expand=True, center=(size[0]/2, size[1]/2))
|
| 149 |
|
| 150 |
# 计算粘贴位置
|
| 151 |
paste_x = int(center[0] - rotated_text.width / 2)
|