Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Remove object: painted windows start on the frame their mask was tracked on; the open mask preview is SAM 3's matte

Claude47b06454e2dd
diff --git a/src/components/remove-object/remove-object-tool.tsx b/src/components/remove-object/remove-object-tool.tsx
index 1a480ae3..6ec4da01 100644
--- a/src/components/remove-object/remove-object-tool.tsx
+++ b/src/components/remove-object/remove-object-tool.tsx
@@ -278,7 +278,7 @@ export function RemoveObjectTool({ tabs }: { tabs?: React.ReactNode }) {
</div>
)}
<p className="mt-2 text-[12px] text-muted-foreground">{frameHint(mode, engine, points.length, !!paint)}</p>
- {preview && <MaskPreview g={preview} engine={engine} />}
+ {preview && <MaskPreview g={preview} />}
</Panel>
</div>
@@ -498,13 +498,13 @@ function EraserList({ options, value, onChange }: { options: Eraser[]; value: st
);
}
-/** The mask preview: SAM 3.1's mask video (cloud: white = what goes) or the SAM 3 cut-out of it (open). */
-function MaskPreview({ g, engine }: { g: Generation; engine: "cloud" | "open" }) {
+/** The mask preview: SAM 3.1's mask video (cloud) or the SAM 3 matte (open), white = what goes. */
+function MaskPreview({ g }: { g: Generation }) {
const out = g.outputs.find((o) => o.kind === "video");
return (
<div className="mt-3 rounded-xl border p-2.5">
<p className="mb-2 text-[12px] font-medium">
- Mask preview <span className="font-normal text-muted-foreground">· {engine === "open" ? "what goes, cut out" : "white is what goes"}</span>
+ Mask preview <span className="font-normal text-muted-foreground">· white is what goes</span>
</p>
{g.status === "succeeded" && out ? (
<video
@@ -513,7 +513,7 @@ function MaskPreview({ g, engine }: { g: Generation; engine: "cloud" | "open" })
loop
muted
playsInline
- className={cn("max-h-48 w-full rounded-lg object-contain", engine === "open" ? "bg-[repeating-conic-gradient(var(--muted)_0_25%,transparent_0_50%)] bg-[length:16px_16px]" : "bg-media")}
+ className="max-h-48 w-full rounded-lg bg-media object-contain"
/>
) : g.status === "failed" || g.status === "canceled" ? (
<p className="text-[12px] text-destructive">{g.error ?? "The preview didn't finish"}</p>
diff --git a/src/lib/catalog/remove-object.ts b/src/lib/catalog/remove-object.ts
index be80c9fe..ecfd433e 100644
--- a/src/lib/catalog/remove-object.ts
+++ b/src/lib/catalog/remove-object.ts
@@ -67,7 +67,7 @@ export interface Eraser {
/** The shortest clip it takes, in seconds. */
minSeconds?: number;
/** Open: one job per window of frames: at most `cap`, `step`·n + `base` frames (VACE 4n+1 ≤ 81, VOID 8n+5 ≤ 197). */
- window?: { cap: number; step: number; base: number };
+ window?: FrameWindow;
/** The result carries the piece's own sound (otherwise the clip's sound is put back). */
keepsSound: boolean;
/** Also removes what the object causes: shadows, reflections, things it moves. */
@@ -78,8 +78,24 @@ export interface Eraser {
sides?: { min: number; max: number };
}
-const VACE_WINDOW = { cap: 81, step: 4, base: 1 };
-const VOID_WINDOW = { cap: 197, step: 8, base: 5 };
+/**
+ * Windows of `step`·n + `base` frames, at most `cap`. `overlap`: each window after the first starts on the last
+ * `overlap` frames of the one before (dropped again before the join).
+ */
+export interface FrameWindow {
+ cap: number;
+ step: number;
+ base: number;
+ overlap?: number;
+}
+
+const VACE_WINDOW: FrameWindow = { cap: 81, step: 4, base: 1 };
+/**
+ * The painted eraser hands on the tracked mask of a window's last frame, and a mask must be of the window's first
+ * frame: so each window starts on the last frame of the one before (a one-frame overlap, cut off before the join).
+ */
+const VACE_PAINTED_WINDOW: FrameWindow = { ...VACE_WINDOW, overlap: 1 };
+const VOID_WINDOW: FrameWindow = { cap: 197, step: 8, base: 5 };
/** Bria's erasers take clips "shorter than 5 s": pieces stay a little under. */
const BRIA_PIECE = 4.8;
@@ -147,7 +163,7 @@ export const ERASERS: Eraser[] = [
modes: ["paint"],
label: "Wan 2.1 VACE + SAM 3.1",
note: "SAM 3.1 follows what you painted · 24 GB GPU · Apache-2.0",
- window: VACE_WINDOW,
+ window: VACE_PAINTED_WINDOW,
keepsSound: true,
},
{
@@ -255,6 +271,8 @@ export interface Piece {
frames?: number;
startTime?: number;
endTime?: number;
+ /** Open: frames at the window's start that the window before already made (cut off before the join). */
+ skip?: number;
}
const r3 = (n: number) => Math.round(n * 1000) / 1000;
@@ -262,12 +280,14 @@ const r6 = (n: number) => Math.round(n * 1e6) / 1e6;
/**
* Split `total` frames into windows of `step`·n + `base` frames, each at most `cap`, as even as possible
- * (the fewest windows that fit). The windows cover all but fewer than `step` frames at the end.
+ * (the fewest windows that fit). The windows cover all but fewer than `step` frames at the end; with an
+ * overlap, each window after the first also reads the last `overlap` frames of the one before.
*/
-export function frameWindows(total: number, w: { cap: number; step: number; base: number }): number[] {
+export function frameWindows(total: number, w: FrameWindow): number[] {
if (total < w.base) return [];
- const k = Math.max(1, Math.ceil(total / w.cap));
- const units = Math.floor((total - k * w.base) / w.step);
+ const o = w.overlap ?? 0;
+ const k = Math.max(1, Math.ceil((total - o) / (w.cap - o)));
+ const units = Math.floor((total + (k - 1) * o - k * w.base) / w.step);
if (units < 0) return [];
const each = Math.floor(units / k);
const extra = units % k;
@@ -275,19 +295,23 @@ export function frameWindows(total: number, w: { cap: number; step: number; base
}
/**
- * Open engine: back-to-back windows on whole frames at the clip's own rate, so the joined clip has
- * exactly the range's frames (less fewer than `step` at the end) and stays on the original sound.
- * Each window is read from `(f − ¼)/fps` for `n/fps` seconds: the graph's Video Slice then decodes
- * exactly frames f … f + n − 1 (checked on the live ComfyUI with 24, 29.97 and 30 fps clips).
+ * Open engine: windows on whole frames at the clip's own rate, back to back (or overlapping by `overlap`
+ * frames, which are cut off again), so the joined clip has exactly the range's frames (less fewer than
+ * `step` at the end) and stays on the original sound. Each window is read from `(f − ¼)/fps` for `n/fps`
+ * seconds: the graph's Video Slice then decodes exactly frames f … f + n − 1, and a trim from
+ * `(skip − ¼)/fps` drops exactly `skip` frames (both run on the live ComfyUI with 24, 29.97 and 30 fps clips).
*/
-export function planWindows(range: RemoveRange, fps: number, w: { cap: number; step: number; base: number }): Piece[] {
+export function planWindows(range: RemoveRange, fps: number, w: FrameWindow): Piece[] {
const first = Math.round(range.start * fps);
const total = Math.floor((range.end - range.start) * fps + 1e-6);
let at = first;
return frameWindows(total, w).map((frames, index) => {
- const startTime = Math.max(0, (at - 0.25) / fps);
- const piece = { index, start: r3(at / fps), end: r3((at + frames) / fps), frames, startTime: r6(startTime), endTime: r6(startTime + frames / fps) };
- at += frames;
+ const skip = index > 0 ? (w.overlap ?? 0) : 0;
+ const from = at - skip;
+ const startTime = Math.max(0, (from - 0.25) / fps);
+ const piece: Piece = { index, start: r3(at / fps), end: r3((from + frames) / fps), frames, startTime: r6(startTime), endTime: r6(startTime + frames / fps) };
+ if (skip) piece.skip = skip;
+ at = from + frames;
return piece;
});
}
@@ -506,6 +530,8 @@ export function planRemoval(input: RemovalPlanInput): RemovalPlan {
const sound = !!input.source.hasAudio && (n > 1 || !e.keepsSound);
const eraseFeature = (one: boolean) => (one && !sound ? { ...base, title: input.title } : helper);
+ // What each piece's join input is: its erased window, or (an overlapping window) the window less its first frames.
+ const joined = pieces.map((_, i) => `erase${i}`);
if (e.engine === "open") {
pieces.forEach((p, i) => {
const own = eraseRequest(input, p);
@@ -523,6 +549,16 @@ export function planRemoval(input: RemovalPlanInput): RemovalPlan {
// The painted eraser starts each window from where the last one left the object.
...(painted && i > 0 ? { after: [`erase${i - 1}`], chain: [{ slot: "mask" as const }] } : {}),
});
+ if (p.skip && input.source.fps) {
+ // The frames the window before already made are cut off (a trim from (skip − ¼)/fps keeps the rest).
+ steps.push({
+ key: `keep${i}`,
+ request: { ...helper, modelId: t.cut, op: "trim", params: { startTime: r6((p.skip - 0.25) / input.source.fps) }, step: pieceLabel(p, n, "drop the overlap") },
+ after: [`erase${i}`],
+ chain: [{ slot: "video" }],
+ });
+ joined[i] = `keep${i}`;
+ }
});
} else {
// The range itself, when SAM 3.1 has to see it from its first frame (the clicks and paint are on it).
@@ -581,7 +617,7 @@ export function planRemoval(input: RemovalPlanInput): RemovalPlan {
}
// Join the pieces in order (folding when the join takes fewer clips than there are).
- let level = pieces.map((_, i) => `erase${i}`);
+ let level = joined;
let depth = 0;
while (level.length > 1) {
const groups = joinGroups(level.length, t.joinMax);
@@ -650,8 +686,8 @@ export interface PreviewPlanInput {
/**
* A cheap look at the mask before paying for the removal: the first seconds of the range, segmented by
- * SAM 3.1 (cloud: a mask video of what you named, clicked or painted, about $0.02; open: the object cut
- * out of the clip by the SAM 3 matte graph, free). Null when the engine can't make one (open clicks; open
+ * SAM 3.1 (cloud: a mask video of what you named, clicked or painted, about $0.02; open: the SAM 3 matte
+ * graph's black-and-white matte of what you named, free). Null when the engine can't make one (open clicks; open
* paint, whose painted overlay is its own preview).
*/
export function planMaskPreview(input: PreviewPlanInput): PlanStep[] | null {
@@ -672,7 +708,8 @@ export function planMaskPreview(input: PreviewPlanInput): PlanStep[] | null {
modelId: t.preview,
op: "bg-remove-video",
...(words ? { prompt: words } : {}),
- params: { ...(input.points?.length && !words ? { points: input.points } : {}), duration: r3(end - input.range.start) },
+ // The open graph's black-and-white matte (white = what goes), like the cloud's mask video.
+ params: { ...(input.points?.length && !words ? { points: input.points } : {}), duration: r3(end - input.range.start), ...(input.engine === "open" ? { matteOutput: "matte" as const } : {}) },
step: { label: "Mask preview", index: 1, of: 2 },
},
after: ["cut"],
diff --git a/src/server/remove-object.ts b/src/server/remove-object.ts
index 46eb8422..e0a87290 100644
--- a/src/server/remove-object.ts
+++ b/src/server/remove-object.ts
@@ -169,7 +169,7 @@ async function planInput(body: RemovalBody, opts: { probe: boolean; preview?: bo
const per = eraser.window.cap / fps;
if (len > per + 0.01) {
notes.push(`${model?.name ?? eraser.label} works on ${eraser.window.cap} frames at a time (${per.toFixed(1)} s of this clip): the clip runs as ${Math.ceil((len * fps) / eraser.window.cap)} windows, joined with its own sound. Seams between windows aren't tested on a GPU yet`);
- if (eraser.modes.includes("paint")) notes.push("SAM 3.1 hands what you painted on from window to window, so the windows run one after another");
+ if (eraser.modes.includes("paint")) notes.push("SAM 3.1 hands what you painted on from window to window (each starts on the last frame of the one before), so the windows run one after another");
}
notes.push(eraser.effects ? "VOID needs a 24–40 GB GPU (unverified). Its CogVideoX base is licensed for commercial use only after registering, up to 1 million visits a month" : "Wan 2.1 VACE 14B needs a 24 GB GPU · Apache-2.0 (SAM 3.1: Meta's SAM License)");
} else {
diff --git a/tests/remove-object-api.test.ts b/tests/remove-object-api.test.ts
index 44254aff..b62fff9f 100644
--- a/tests/remove-object-api.test.ts
+++ b/tests/remove-object-api.test.ts
@@ -192,6 +192,8 @@ describe("Remove object: runs", () => {
expect(windows[1].depends_on).toBe(windows[0].id);
expect(JSON.parse(windows[2].request_json).chain).toEqual([{ slot: "mask" }]);
expect(rows.map((r) => r.model_id)).toEqual(expect.arrayContaining(["oss-join-clips", "oss-mux"]));
+ // The later windows start on the frame their mask was tracked on, and lose it again before the join.
+ expect(rows.filter((r) => r.model_id === "oss-trim")).toHaveLength(2);
// The first window's last tracked frame starts the second one.
const second = await launched(windows[1].id);
expect(second.request.inputs?.mask).toMatch(/^\/api\/files\//);
@@ -209,12 +211,14 @@ describe("Remove object: runs", () => {
expect(final.request.params).toMatchObject({ startTime: 0, endTime: 2.8125 });
});
- it("mask preview: SAM 3.1 on the first 2 s (cloud), the SAM 3 cut-out (open)", async () => {
+ it("mask preview: SAM 3.1 on the first 2 s (cloud), the SAM 3 matte (open)", async () => {
const cloud = (await (await previewRoute.POST(json(body({ requestId: "preview-cloud-1" })))).json()) as Generation;
expect(cloud).toMatchObject({ feature: REMOVE.previewFeature, modelId: "sam-3-1-video-mask", op: "bg-remove-video", prompt: "the skateboard" });
expect(cloud.request.params?.duration).toBe(2);
const open = (await (await previewRoute.POST(json(body({ engine: "open" })))).json()) as Generation;
expect(open.modelId).toBe("oss-video-matte-sam3");
+ // White = what goes, like the cloud's mask video: the graph saves only its matte.
+ expect(open.request.params?.matteOutput).toBe("matte");
const none = await previewRoute.POST(json(body({ engine: "open", mode: "paint", mask })));
expect(none.status).toBe(422);
expect((await settle(cloud.id)).status).toBe("succeeded");
diff --git a/tests/remove-object.test.ts b/tests/remove-object.test.ts
index 2100a975..2f58d29d 100644
--- a/tests/remove-object.test.ts
+++ b/tests/remove-object.test.ts
@@ -141,6 +141,29 @@ describe("Remove object: windows and pieces", () => {
expect(late).toEqual([{ index: 0, start: 1, end: 2.875, frames: 45, startTime: 0.989583, endTime: 2.864583 }]);
});
+ it("overlapping windows (the painted eraser): each starts on the last frame of the one before, which is cut off again", () => {
+ const painted = { cap: 81, step: 4, base: 1, overlap: 1 };
+ expect(frameWindows(240, painted)).toEqual([81, 81, 77]);
+ expect(frameWindows(81, painted)).toEqual([81]);
+ // 82 frames: two windows, 41 + 41 read, 81 new.
+ expect(frameWindows(82, painted)).toEqual([41, 41]);
+ for (const total of [82, 161, 240, 1799]) {
+ const got = frameWindows(total, painted);
+ const fresh = got.reduce((a, b) => a + b, 0) - (got.length - 1);
+ expect(fresh).toBeLessThanOrEqual(total);
+ expect(total - fresh).toBeLessThan(4);
+ expect(got.every((n) => n <= 81 && (n - 1) % 4 === 0)).toBe(true);
+ }
+ const w = planWindows({ start: 0, end: 8 }, 30, painted);
+ expect(w).toEqual([
+ { index: 0, start: 0, end: 2.7, frames: 81, startTime: 0, endTime: 2.7 },
+ { index: 1, start: 2.7, end: 5.367, frames: 81, startTime: 2.658333, endTime: 5.358333, skip: 1 },
+ { index: 2, start: 5.367, end: 7.9, frames: 77, startTime: 5.325, endTime: 7.891667, skip: 1 },
+ ]);
+ // The new frames are still back to back.
+ for (let i = 1; i < w.length; i++) expect(w[i].start).toBe(w[i - 1].end);
+ });
+
it("cuts cloud clips into equal pieces under each eraser's limit, on whole frames", () => {
expect(planCloudPieces({ start: 0, end: 12 }, 4.8, 30).map((p) => [p.start, p.end])).toEqual([
[0, 4],
@@ -304,6 +327,11 @@ describe("Remove object: open pipelines", () => {
expect(step(plan.steps, "erase0").request.prompt).toBeUndefined();
expect(step(plan.steps, "erase1")).toMatchObject({ after: ["erase0"], chain: [{ slot: "mask" }], request: { inputs: { video: src.url } } });
expect(step(plan.steps, "erase2")).toMatchObject({ after: ["erase1"], chain: [{ slot: "mask" }] });
+ // Each later window starts on the frame its mask was tracked on, and loses that frame before the join.
+ expect(step(plan.steps, "erase1").request.params).toMatchObject({ startTime: 2.658333, endTime: 5.358333 });
+ expect(step(plan.steps, "keep1")).toMatchObject({ after: ["erase1"], chain: [{ slot: "video" }], request: { modelId: "oss-trim", op: "trim", params: { startTime: 0.025 } } });
+ expect(step(plan.steps, "keep0")).toBeUndefined();
+ expect(step(plan.steps, "join").after).toEqual(["erase0", "keep1", "keep2"]);
});
it("VOID windows are up to 197 frames", () => {