Files
my-vault/自动语义分割系统/安装步骤.md

186 lines
7.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: ComfyUI 视频分割系统 - 安装与搭建步骤
date: 2026-04-22
tags:
- comfyUI
- 安装步骤
- SAM2
- 工作流搭建
---
# ComfyUI 视频分割系统 - 安装与搭建步骤
## 前置要求
在 ComfyUI 界面中操作前,请确保已通过 **ComfyUI Manager管理扩展功能** 安装以下节点包:
| 节点包 | 作用 |
|-------|------|
| `ComfyUI-VideoHelperSuite` | 视频加载、帧提取、视频合成 |
| `ComfyUI-segment-anything-2` | SAM2 模型加载、图像/视频分割与追踪 |
| `ComfyUI-Impact-Pack` | Grounding DINO 语义检测 |
| `rgthree-comfy` | Fast Muter / Bypass 节点 |
安装后**重启 ComfyUI**。
---
## 搭建步骤(共 10 步)
以下所有节点均通过 **双击画布空白处****右键 → 添加节点** 创建。
### 第 1 步:选择加载节点
1. 右键/双击空白处打开节点菜单
2. 在第二级菜单中找到 **Video Helper Suite**(带有紫色摄像机图标的那一项)
3. 在右侧弹出的第三级菜单中,根据你的需求选择以下任意一个节点:
- **Load Video (Upload)**:如果你想从电脑本地上传一个视频文件
- **Load Video (Path)**:如果你想通过输入电脑上的文件夹路径来读取视频
4. 点击创建节点
5. 节点出现在画布上,把它拖到左上角区域
### 第 2 步:提取第一帧(关键帧)
1. 回到 **Video Helper Suite**(紫色摄像机图标)的第三级菜单
2. 找到 `图像` 分类下的 **`Select Images`**
- 在较新版本的 Video Helper Suite 中,它取代了旧版的 `VHS_SelectFrames` 名称功能完全一致都是通过索引Index从视频流中挑选特定帧
3. (可选抽帧)如果你不想逐帧处理,也可以使用它上方的 **`Select Every Nth Image`**,每隔几帧取一张
4. 连接:`Load Video``IMAGE` 输出 → `Select Images``image` 输入
5. 设置参数:`index = 0`(表示取第一帧)
> 这就是你的关键帧,后续所有分割以此为基础。
### 第 3 步SAM2 模型加载器
- 搜索:`SAM2``(Down)Load SAM2Model`
- 拖到画布**中上方**,作为公共模型源
- 参数设置:
- `model`: `sam2.1_hiera_base_plus.safetensors`
- `segmentor`: 根据用途选择(单图或视频)
- `device`: `cuda`
- `precision`: `fp16`(省显存)
### 第 4 步单帧分割测试Sam2Segmentation
- 搜索:`SAM2``Sam2Segmentation`
- 连接:
- `Select Images`(第一帧)→ `image`
- `(Down)Load SAM2Model``sam2_model`
**选择以下任意一种提示方式填入:**
- **方式 A手绘 Mask最直观强烈推荐**
- 将图像连到 `Preview Image`,右键预览图 → `Open in Mask Editor`
- 用画笔涂抹目标区域Save 后得到 `mask`,连到 `Sam2Segmentation``mask` 输入
- 无需精确坐标,手绘粗略区域即可引导 SAM2 分割
- **方式 B点坐标适合精确控制**
- `coordinates_positive`: 填入正选点坐标(如 `[[0.45, 0.52]]`
- `coordinates_negative`: 填入反选点坐标(可选)
- 坐标获取技巧:先用 `Sam2AutoSegmentation` 跑一下看 `bbox` 参考;或在画图软件中读像素坐标再归一化
- **方式 C检测框**
- `bboxes`: 填入边界框 `[x1, y1, x2, y2]`,可通过 Florence-2 / Grounding DINO 自动生成
- 再加一个 `image``Preview Image`,连到 `Sam2Segmentation``mask` 输出
- **点击右上角「运行」**,先确认第一帧分割正确
- **效果不佳时**:直接修改 mask重画或坐标数值再次运行即可快速迭代
### 第 5 步:视频追踪核心链路
**5.1 Sam2VideoSegmentationAddPoints注入提示**
- 搜索:`SAM2``Sam2VideoSegmentationAddPoints`
- 连接:
- `(Down)Load SAM2Model``sam2_model`
- `Load Video` 的完整图像批次 → `image`
- `coordinates_positive`: 第一帧的正选点坐标
- `coordinates_negative`: 反选点坐标(可选)
- 参数:
- `frame_index`: `0`(在第 0 帧添加标注)
- `object_index`: `0`(单目标默认)
- `prev_inference_state`: 首次追踪时留空
**5.2 Sam2VideoSegmentation执行追踪**
- 搜索:`SAM2``Sam2VideoSegmentation`
- 连接:
- `(Down)Load SAM2Model``sam2_model`
- `Sam2VideoSegmentationAddPoints``inference_state``inference_state`
- 参数:`keep_model_loaded` 推荐开启
- 输出:`mask`(完整视频的分割结果)
### 第 6 步:可视化与保存
| 节点 | 路径 | 用途 |
|------|------|------|
| `MaskToImage` | `image``MaskToImage` | 将 mask 转为可视图像 |
| `Preview Image` | `image``Preview Image` | 预览效果 |
| `Save Image` | `image``Save Image` | 保存 mask 序列 |
| `VHS_VideoCombine` | `video``VHS_VideoCombine` | 合成带遮罩的视频 |
### 第 7 步人工纠正分支RGThree Fast Muter
- 搜索:`rgthree``Fast Muter`
- **核心思路**:利用 `Sam2VideoSegmentationAddPoints``prev_inference_state` 链式追加修正,默认通过 Fast Muter / Bypass 跳过修正节点
- 具体搭建:
1. 新建第二个 `Sam2VideoSegmentationAddPoints`,命名为 "Fix_AddPoints"
2. `prev_inference_state` 接入主链路第一个 AddPoints 的 `inference_state`
3. `image`: 接入 `Load Video` 的完整批次
4. `frame_index`: 问题帧号(如 `45`
5. `coordinates_positive`: 修正后的点坐标
6. 该节点的 `inference_state``Sam2VideoSegmentation`
7. 对该修正节点使用 **Bypass** 或在其坐标输入前加 **`Fast Muter`**,默认关闭
- **默认状态**:修正节点被跳过(红色/关闭),不参与运算
- **需要修正时**:取消 Bypass / Fast Muter 变绿(启用),修改帧号和坐标,重新运行
### 第 8 步:总连线检查
确保以下四点:
- `Sam2VideoSegmentationAddPoints``image` 接入的是**完整视频批次**,不是单帧
- `Sam2VideoSegmentationAddPoints``frame_index` 指向了正确的标注帧
- `Sam2Segmentation`(单帧预览)只吃了 `index=0` 的单帧
- Fast Muter / 修正节点 Bypass 状态符合预期
---
## 建议的执行顺序
不要一次性连完所有节点再运行!按以下顺序验证:
1. **只连第 1~4 步**(到 Preview Image→ 运行 → 确认第一帧分割正确
2. **接入第 5~6 步**VideoSegmentationAddPoints + VideoSegmentation + 输出)→ 运行 → 确认整段视频能跑通
3. **最后加第 7 步**Fast Muter 修正分支)→ 测试一次纠正流程
---
## Fast Muter 实际操作流程
```
首次运行:
Fix_AddPoints 被 Bypass / Fast Muter 关闭
→ 点击「运行」→ 检查 Preview Image
发现第 N 帧错了:
1. 修改 "Fix_AddPoints" 的 frame_index = N
2. 修改 "Fix_AddPoints" 的 coordinates_positive 为正确坐标
3. 取消 Bypass / Fast Muter = 绿色(打开)
4. 再次点击「运行」
5. 系统以第 0 帧 + 第 N 帧为锚点,综合追踪整个视频
修好了:
恢复 Bypass / Fast Muter = 红色(关闭)
→ 保存最终工作流模板
```
---
## 常见问题速查
| 问题 | 排查方向 |
|------|---------|
| 节点呈红色/无法创建 | 节点包未安装或加载失败,检查 ComfyUI 启动日志 |
| SAM2 节点报错显存不足 | 换 `sam2.1_hiera_tiny.safetensors`,或降低视频分辨率,或启用 fp16 |
| 追踪结果全黑/全白 | 检查 `coordinates_positive` 是否正确传入检查坐标格式0~1 vs 像素);确认 `frame_index` 与标注帧一致 |
| Florence-2 / Grounding DINO 无检测结果 | 降低 `threshold`,或简化 prompt如只写 `"person"` |
| 修正后重新追踪无变化 | 确认修正节点已取消 Bypass / Fast Muter 已启用;确认修正节点的 `prev_inference_state` 正确接入上游 |
| 视频输出帧率不对 | 在 `Load Video``VideoCombine` 中统一设置 fps 参数 |