diff --git a/video-ai-system/engines/subtitle-pipeline/reference-analysis/reference-subtitle-analyzer.py b/video-ai-system/engines/subtitle-pipeline/reference-analysis/reference-subtitle-analyzer.py index 68052ec..721cea8 100644 --- a/video-ai-system/engines/subtitle-pipeline/reference-analysis/reference-subtitle-analyzer.py +++ b/video-ai-system/engines/subtitle-pipeline/reference-analysis/reference-subtitle-analyzer.py @@ -111,6 +111,7 @@ def analyze_subtitle_region(image_path: str, interactive: bool = False) -> dict: h = int(height * 0.12) else: # 合并所有轮廓的边界框 + # 正确算式:找到所有轮廓的最小外接矩形 all_x = [] all_y = [] all_w = [] @@ -123,20 +124,28 @@ def analyze_subtitle_region(image_path: str, interactive: bool = False) -> dict: all_w.append(cw) all_h.append(ch) - # 计算合并后的边界框 - x = min(all_x) - y = min(all_y) + int(height * 0.85) # 加上裁剪偏移 - w = max(all_x) + max(all_w) - x - h = max(all_y) + max(all_h) - y - + # 正确合并:先在 bottom_region 坐标系里算完,最后再加偏移 + x_bottom = min(all_x) + y_bottom = min(all_y) + max_x_bottom = max([all_x[i] + all_w[i] for i in range(len(all_x))]) + max_y_bottom = max([all_y[i] + all_h[i] for i in range(len(all_y))]) + w = max_x_bottom - x_bottom + h = max_y_bottom - y_bottom + # 扩展边界框(包含描边) padding = 10 - x = max(0, x - padding) - y = max(0, y - padding) + x = max(0, x_bottom - padding) + y = max(0, y_bottom - padding) + int(height * 0.85) # 偏移只加一次,在最后 w = min(width - x, w + 2 * padding) h = min(height - y, h + 2 * padding) # 计算字幕参数 + # 先做边界校验,防止裁剪出空数组 + x = max(0, min(x, width - 1)) + y = max(0, min(y, height - 1)) + w = max(1, min(w, width - x)) + h = max(1, min(h, height - y)) + subtitle_box = {"x": x, "y": y, "width": w, "height": h} subtitle_height_px = h bottom_distance_px = height - (y + h)