feat: add seam carving AI bridge module

This commit is contained in:
Siddharth Kumar Sah 2026-04-07 23:26:47 +08:00
parent 18119166f0
commit d3b646207d
2 changed files with 45 additions and 0 deletions

View file

@ -3,4 +3,5 @@ export { isGpuAvailable, shutdownDispatcher } from "./bridge.js";
export { blurFaces } from "./face-detection.js";
export { inpaint } from "./inpainting.js";
export { extractText } from "./ocr.js";
export { seamCarve } from "./seam-carving.js";
export { upscale } from "./upscaling.js";

View file

@ -0,0 +1,44 @@
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
export interface SeamCarveOptions {
width?: number;
height?: number;
protectFaces?: boolean;
}
export interface SeamCarveResult {
buffer: Buffer;
width: number;
height: number;
}
export async function seamCarve(
inputBuffer: Buffer,
outputDir: string,
options: SeamCarveOptions = {},
onProgress?: ProgressCallback,
): Promise<SeamCarveResult> {
const inputPath = join(outputDir, "input_seam_carve.png");
const outputPath = join(outputDir, "output_seam_carve.png");
await writeFile(inputPath, inputBuffer);
const { stdout } = await runPythonWithProgress(
"seam_carve.py",
[inputPath, outputPath, JSON.stringify(options)],
{ onProgress },
);
const result = JSON.parse(stdout);
if (!result.success) {
throw new Error(result.error || "Content-aware resize failed");
}
const buffer = await readFile(outputPath);
return {
buffer,
width: result.width,
height: result.height,
};
}