跳到主要内容

视频换车

收藏0

用 ComfyUI 的 GroundingDINO + SAM2 + VACE 把画面里的车换成另一台,mask 外像素零改动。

让 AI 安装

lrtLV2最新版本 0.1.0更新于 2026-11-14下载 0 次

scripts/verify_replace.py

5.6 KB
1# -*- coding: utf-8 -*-
2"""核验「只改了目标物体」:mask 质量 + 合成越界检查。
3
4用法:
5 python verify_replace.py --original orig.png --result out.png [--mask out_mask.png]
6
7输出两组指标:
8 A. mask 质量(需要 --mask):白像素占比、连通域数量、包围盒、是否贴边、填充率
9 B. 合成越界(需要 --original/--result):mask 外 MAD、改动包围盒、全局 MAD
10
11判定标准:
12 - 连通域应当是 1;多了说明框到了别的东西。
13 - mask 外 MAD 应当是 0.0000,这是「其他地方一个像素都没动」的硬保证。
14 - 改动包围盒中心应当和 mask 包围盒中心基本一致。
15"""
16import argparse
17import json
18import os
19import sys
20
21import numpy as np
22from PIL import Image
23
24
25def mask_quality(path):
26 im = Image.open(path).convert("L")
27 a = np.array(im)
28 binary = a > 127
29 h, w = binary.shape
30 lines = []
31
32 if binary.sum() == 0:
33 lines.append("mask is EMPTY (全黑) —— 分割没命中,检查 seg-prompt 和 threshold")
34 return lines
35
36 lines.append("mask size %dx%d, white %.2f%%" % (w, h, binary.sum() * 100.0 / (w * h)))
37
38 try:
39 from scipy import ndimage
40 lab, n = ndimage.label(binary)
41 sizes = ndimage.sum(binary, lab, range(1, n + 1))
42 order = np.argsort(sizes)[::-1]
43 lines.append("connected components: %d%s" % (
44 n, " <- 应当为 1" if n != 1 else " (ok)"))
45 for i in order[:6]:
46 idx = i + 1
47 ys, xs = np.where(lab == idx)
48 lines.append(" comp%d area=%d (%.2f%%) bbox x[%d,%d] y[%d,%d] aspect=%.2f" % (
49 idx, int(sizes[i]), sizes[i] * 100.0 / (w * h),
50 xs.min(), xs.max(), ys.min(), ys.max(),
51 (xs.max() - xs.min() + 1) / max(1, (ys.max() - ys.min() + 1))))
52 if len(sizes) > 1:
53 lines.append(" largest / total = %.3f" % (sizes[order[0]] / sizes.sum()))
54 except ImportError:
55 lines.append("scipy 不可用,跳过连通域统计(pip install scipy 更准)")
56
57 ys, xs = np.where(binary)
58 bx0, bx1, by0, by1 = int(xs.min()), int(xs.max()), int(ys.min()), int(ys.max())
59 lines.append("bbox x[%d,%d] y[%d,%d] w=%d h=%d" % (bx0, bx1, by0, by1, bx1 - bx0 + 1, by1 - by0 + 1))
60 lines.append("bbox center (%.0f, %.0f) = (%.1f%%, %.1f%%) of frame" % (
61 (bx0 + bx1) / 2, (by0 + by1) / 2,
62 (bx0 + bx1) / 2 * 100.0 / w, (by0 + by1) / 2 * 100.0 / h))
63 lines.append("touches edge: left=%s right=%s top=%s bottom=%s" % (
64 bx0 <= 1, bx1 >= w - 2, by0 <= 1, by1 >= h - 2))
65 lines.append("fill ratio inside bbox = %.3f" % binary[by0:by1 + 1, bx0:bx1 + 1].mean())
66 return lines
67
68
69def composite_check(original, result, mask_path):
70 orig = Image.open(original).convert("RGB")
71 res = Image.open(result).convert("RGB")
72 lines = []
73
74 if orig.size != res.size:
75 lines.append("尺寸不一致 %s vs %s —— 把原图缩到结果尺寸再比" % (orig.size, res.size))
76 orig = orig.resize(res.size, Image.LANCZOS)
77
78 a = np.array(orig).astype(np.int16)
79 b = np.array(res).astype(np.int16)
80 d = np.abs(a - b).mean(axis=2)
81
82 lines.append("global MAD %.4f" % d.mean())
83 changed = d > 3
84 ys, xs = np.where(changed)
85 lines.append("changed pixels (>3): %d (%.3f%%)" % (changed.sum(), changed.mean() * 100))
86 if len(xs):
87 lines.append("changed bbox x[%d,%d] y[%d,%d] center=(%.0f,%.0f) [%.0f%%,%.0f%%]" % (
88 xs.min(), xs.max(), ys.min(), ys.max(),
89 (xs.min() + xs.max()) / 2, (ys.min() + ys.max()) / 2,
90 (xs.min() + xs.max()) / 2 * 100 / res.width,
91 (ys.min() + ys.max()) / 2 * 100 / res.height))
92
93 if mask_path and os.path.exists(mask_path):
94 m = np.array(Image.open(mask_path).convert("L").resize(res.size, Image.NEAREST)) > 127
95 if m.any():
96 outside = d[~m]
97 inside = d[m]
98 lines.append("mask OUTSIDE MAD = %.4f <- 应当为 0.0000" % outside.mean())
99 lines.append("mask INSIDE MAD = %.4f <- 应当明显大于 0" % inside.mean())
100 bad = (outside > 3).sum()
101 lines.append("mask OUTSIDE changed>3: %d px (%.5f%%)" % (
102 bad, bad * 100.0 / outside.size))
103 else:
104 lines.append("mask 全黑,无法比较内外")
105 else:
106 lines.append("未给 --mask,只报全局差异")
107 lines.append("untouched pixels MAD = %.4f" % d[~changed].mean())
108
109 lines.append("identical pixel ratio (<0.5) = %.4f%%" % ((d < 0.5).mean() * 100))
110 return lines
111
112
113def main():
114 p = argparse.ArgumentParser()
115 p.add_argument("--original", help="原图路径")
116 p.add_argument("--result", help="替换后的图路径")
117 p.add_argument("--mask", default="", help="合成用的 mask 图路径")
118 p.add_argument("--json", dest="json_out", default="", help="把结果写成 json")
119 args = p.parse_args()
120
121 out = {}
122 if args.mask:
123 print("=== A. mask 质量 ===")
124 ml = mask_quality(args.mask)
125 for l in ml:
126 print(l)
127 out["mask"] = ml
128
129 if args.original and args.result:
130 print("\n=== B. 合成越界检查 ===")
131 cl = composite_check(args.original, args.result, args.mask)
132 for l in cl:
133 print(l)
134 out["composite"] = cl
135
136 if not args.mask and not (args.original and args.result):
137 print("至少给 --mask 或 --original/--result 之一")
138
139 if args.json_out:
140 with open(args.json_out, "w", encoding="utf-8") as f:
141 json.dump(out, f, ensure_ascii=False, indent=1)
142 print("\nwritten", args.json_out)
143
144
145if __name__ == "__main__":
146 main()

文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。