224 lines
8.5 KiB
Markdown
224 lines
8.5 KiB
Markdown
---
|
||
title: ComfyUI 视频分割系统 - 界面搭建操作指南
|
||
date: 2026-04-24
|
||
tags:
|
||
- comfyUI
|
||
- 操作指南
|
||
- SAM2
|
||
- 工作流搭建
|
||
---
|
||
|
||
# ComfyUI 视频分割系统 - 界面搭建操作指南
|
||
|
||
> 本指南基于 `2026_4_24_视频分割工作流构建.json` 初稿编写,描述当前已实现的工作流,并标注后续扩展方向。
|
||
> 扩展参考资料见 [[参考内容/安装步骤.md]]、[[参考内容/插件功能大全.md]]。
|
||
|
||
---
|
||
|
||
## 前置要求
|
||
|
||
请确保已通过 **ComfyUI Manager** 安装以下节点包:
|
||
|
||
| 节点包 | 用途 | JSON 中使用的节点 |
|
||
|:---|:---|:---|
|
||
| `ComfyUI-VideoHelperSuite` | 视频加载、帧提取、视频合成 | `VHS_LoadVideo`, `VHS_SelectImages`, `VHS_VideoCombine`, `PreviewAnimation` |
|
||
| `ComfyUI-segment-anything-2` | SAM2 模型加载与分割 | `DownloadAndLoadSAM2Model`, `Sam2Segmentation` |
|
||
| `ComfyUI-KJNodes` | 交互式坐标编辑器 | `PointsEditor` |
|
||
|
||
---
|
||
|
||
## 当前工作流拓扑
|
||
|
||
```
|
||
[VHS_LoadVideo] ───┬──→ [VHS_SelectImages] ──→ [PointsEditor] ──→ [Sam2Segmentation]
|
||
│ │ (index=0) (positive_coords) ↑
|
||
│ │ (negative_coords) │
|
||
│ │ │
|
||
│ └───────────────────────────────────────────────────┘
|
||
│ ↑
|
||
│ [DownloadAndLoadSAM2Model]
|
||
│
|
||
└──→ [PreviewAnimation] ←── [Sam2Segmentation(mask)]
|
||
↓
|
||
[MaskToImage] ──→ [VHS_VideoCombine]
|
||
```
|
||
|
||
**当前状态**:对视频每一帧使用 **相同的交互式坐标** 进行独立分割(`Sam2Segmentation` 接收完整视频批次)。
|
||
|
||
**扩展目标**:接入 `Sam2VideoSegmentationAddPoints` + `Sam2VideoSegmentation`,实现真正的**跨帧追踪**。
|
||
|
||
---
|
||
|
||
## 第一步:加载视频
|
||
|
||
1. 右键/双击空白处打开节点菜单
|
||
2. 找到 **Video Helper Suite**(紫色摄像机图标)
|
||
3. 选择 `Load Video` 或 `Load Video (Path)`
|
||
4. 参数说明:
|
||
- `video`: 选择你的视频文件
|
||
- `force_rate`: `0`(保持原帧率)
|
||
- `frame_load_cap`: `0`(加载全部帧)
|
||
- `select_every_nth`: `10`(每 10 帧取 1 帧,降低处理量)
|
||
- `format`: `AnimateDiff`
|
||
|
||
---
|
||
|
||
## 第二步:提取第一帧作为交互背景
|
||
|
||
1. 回到 **Video Helper Suite**,选择 `图像` → **`Select Images`**
|
||
2. 连接:`VHS_LoadVideo` 的 `IMAGE` → `Select Images` 的 `image`
|
||
3. 参数:`indexes` = `0`(取第 0 帧)
|
||
4. 该帧将作为 `PointsEditor` 的背景图,方便你在原图上点击标注
|
||
|
||
---
|
||
|
||
## 第三步:交互式坐标生成(PointsEditor)
|
||
|
||
1. 搜索:`KJNodes` → `PointsEditor`
|
||
2. 连接:`Select Images` 的 `IMAGE` → `PointsEditor` 的 `bg_image`
|
||
3. 节点功能:
|
||
- 在画布上显示第一帧图像
|
||
- **点击图像**生成正选点(positive),输出到 `positive_coords`
|
||
- **按住 Shift 点击**生成反选点(negative),输出到 `negative_coords`
|
||
- 支持框选(bbox)和裁剪预览
|
||
4. 参数说明:
|
||
- `width` / `height`: 与视频分辨率一致(如 `2560`, `1440`)
|
||
- `xyxy`: 坐标格式
|
||
5. 输出:`positive_coords`(正选坐标 JSON 字符串)、`negative_coords`(反选坐标 JSON 字符串)
|
||
|
||
> **这是解决"手动填坐标不直观"问题的关键节点。** 你可以反复点击、调整,每次修改后直接运行,无需重新连线。
|
||
|
||
---
|
||
|
||
## 第四步:加载 SAM2 模型
|
||
|
||
1. 搜索:`SAM2` → `DownloadAndLoadSAM2Model`
|
||
2. 参数:
|
||
- `model`: `sam2.1_hiera_base_plus.safetensors`
|
||
- `segmentor`: `video`(选择视频模式,为后续扩展做准备)
|
||
- `device`: `cuda`
|
||
- `precision`: `bf16`
|
||
3. 输出:`sam2_model`
|
||
|
||
---
|
||
|
||
## 第五步:单帧分割广播到全视频(Sam2Segmentation)
|
||
|
||
1. 搜索:`SAM2` → `Sam2Segmentation`
|
||
2. 连接:
|
||
- `DownloadAndLoadSAM2Model` 的 `sam2_model` → `Sam2Segmentation` 的 `sam2_model`
|
||
- `VHS_LoadVideo` 的 `IMAGE`(完整视频批次)→ `Sam2Segmentation` 的 `image`
|
||
- `PointsEditor` 的 `positive_coords` → `Sam2Segmentation` 的 `coordinates_positive`
|
||
- `PointsEditor` 的 `negative_coords` → `Sam2Segmentation` 的 `coordinates_negative`
|
||
3. 参数:
|
||
- `keep_model_loaded`: `true`
|
||
- `individual_objects`: `true`
|
||
4. 输出:`mask`
|
||
|
||
> **当前行为**:`Sam2Segmentation` 对视频批次中的**每一帧**都使用 `PointsEditor` 生成的同一组坐标进行独立分割。如果目标在视频中有大幅位移,后续帧可能分割不准。
|
||
>
|
||
> **这正是下一步需要扩展视频追踪的原因。**
|
||
|
||
---
|
||
|
||
## 第六步:输出与预览
|
||
|
||
### 6.1 Mask 转图像
|
||
|
||
1. 搜索:`mask` → `MaskToImage`
|
||
2. 连接:`Sam2Segmentation` 的 `mask` → `MaskToImage` 的 `mask`
|
||
3. 输出:`IMAGE`(可视化的 mask 图像批次)
|
||
|
||
### 6.2 合成视频
|
||
|
||
1. 搜索:`video` → `VHS_VideoCombine`
|
||
2. 连接:`MaskToImage` 的 `IMAGE` → `VHS_VideoCombine` 的 `images`
|
||
3. 参数:
|
||
- `frame_rate`: `10`(与抽帧间隔匹配)
|
||
- `format`: `video/nvenc_h264-mp4`
|
||
- `filename_prefix`: `AnimateDiff`
|
||
- `save_output`: `true`
|
||
|
||
### 6.3 实时预览
|
||
|
||
1. 搜索:`VHS` → `PreviewAnimation`
|
||
2. 连接:
|
||
- `VHS_LoadVideo` 的 `IMAGE` → `PreviewAnimation` 的 `images`(原视频)
|
||
- `Sam2Segmentation` 的 `mask` → `PreviewAnimation` 的 `masks`(分割 mask)
|
||
3. 功能:同时播放原视频和 mask,便于逐帧检查分割效果
|
||
|
||
---
|
||
|
||
## 第七步:后续扩展方向
|
||
|
||
当前 JSON 只实现了**逐帧独立分割**。下一步扩展为**真正的视频追踪**:
|
||
|
||
### 扩展 1:接入 SAM2 视频追踪核心
|
||
|
||
在现有链路基础上,将 `Sam2Segmentation` 替换或并行为:
|
||
|
||
```
|
||
[Sam2VideoSegmentationAddPoints] ──→ [Sam2VideoSegmentation] ──→ [MaskToImage]
|
||
↑ ↑
|
||
[(Down)Load SAM2Model] [(Down)Load SAM2Model]
|
||
↑
|
||
[PointsEditor(positive_coords)]
|
||
```
|
||
|
||
| 步骤 | 操作 |
|
||
|:---|:---|
|
||
| 1 | 添加 `Sam2VideoSegmentationAddPoints`,`image` 接入完整视频批次,`coordinates_positive` 接入 `PointsEditor` |
|
||
| 2 | 添加 `Sam2VideoSegmentation`,`inference_state` 接入上一步输出 |
|
||
| 3 | `Sam2VideoSegmentation` 的 `mask` → `MaskToImage` |
|
||
| 4 | 此时 `PointsEditor` 的坐标只在第 0 帧生效,SAM2 会自动追踪目标运动到后续帧 |
|
||
|
||
### 扩展 2:人工纠正分支(Fast Muter)
|
||
|
||
当追踪漂移时,在 `prev_inference_state` 路径上链式追加修正节点:
|
||
|
||
```
|
||
[Sam2VideoSegmentationAddPoints(第0帧)]
|
||
↓ inference_state
|
||
[Sam2VideoSegmentationAddPoints(第N帧修正, prev_inference_state接入)]
|
||
↓ inference_state
|
||
[Sam2VideoSegmentation]
|
||
```
|
||
|
||
实现方式:
|
||
1. 安装 `rgthree-comfy`
|
||
2. 创建第二个 `Sam2VideoSegmentationAddPoints`(修正节点)
|
||
3. 默认对该修正节点执行 **Bypass**(右键 → Bypass),使其不参与运算
|
||
4. 需要修正时,取消 Bypass,修改 `frame_index` 和坐标,重新运行
|
||
|
||
### 扩展 3:语义提示自动化
|
||
|
||
当交互式点击不方便时,接入全自动链路:
|
||
|
||
```
|
||
[Text Prompt] ──→ [Florence2 / Grounding DINO] ──→ [Florence2 Coordinates]
|
||
──→ [Sam2Segmentation / Sam2VideoSegmentationAddPoints]
|
||
```
|
||
|
||
---
|
||
|
||
## 运行前检查清单
|
||
|
||
- [ ] SAM2 模型已下载到 `ComfyUI/models/sam2/`(`sam2.1_hiera_base_plus.safetensors`)
|
||
- [ ] `VHS_LoadVideo` 的视频路径有效
|
||
- [ ] `Select Images` 的 `indexes` = `0`
|
||
- [ ] `PointsEditor` 已生成至少一个 `positive_coords`(点击图像)
|
||
- [ ] `Sam2Segmentation` 的 `image` 接入的是**视频批次**(来自 `VHS_LoadVideo`)
|
||
- [ ] `VHS_VideoCombine` 的 `frame_rate` 与抽帧间隔匹配
|
||
|
||
---
|
||
|
||
## 常见问题
|
||
|
||
| 问题 | 排查方向 |
|
||
|:---|:---|
|
||
| `PointsEditor` 点击无反应 | 确保 `bg_image` 已连接且有图像输出;检查节点是否支持当前浏览器 |
|
||
| 分割结果全黑/全白 | 检查 `coordinates_positive` 是否传入;确认坐标在图像范围内 |
|
||
| 后续帧分割漂移 | **正常现象**(当前是逐帧独立分割,非追踪)。解决:扩展接入 `Sam2VideoSegmentation` |
|
||
| 显存不足 | `DownloadAndLoadSAM2Model` 的 `precision` 改为 `fp16`,或降低视频分辨率 |
|
||
| 视频合成帧率不对 | 确保 `select_every_nth` 与 `VideoCombine` 的 `frame_rate` 匹配 |
|