mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat: replace Python seam carving with caire Go binary
Replace the Python seam-carving library with caire (esimov/caire v1.5.0), a Go-based content-aware resize engine that is faster and supports both shrinking and enlarging via seam insertion. - Add Go builder stage in Dockerfile to compile caire from source - Rewrite seam-carving.ts to call caire via execFile (no Python sidecar) - Remove content-aware-resize from PYTHON_SIDECAR_TOOLS (60s timeout) - Add new options: blur radius, edge sensitivity, square mode, face detection - Move content-aware toggle below standard resize in UI (subtler placement) - Rename "Don't enlarge" to "Limit to original size" with hover tooltip - Add smooth progress bar for medium-duration tools - Delete seam_carve.py and remove seam-carving pip dependency - Update integration tests and visual regression screenshots
This commit is contained in:
@@ -1,11 +1,19 @@
|
||||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { execFile } from "node:child_process";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
|
||||
import { promisify } from "node:util";
|
||||
import sharp from "sharp";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
export interface SeamCarveOptions {
|
||||
width?: number;
|
||||
height?: number;
|
||||
protectFaces?: boolean;
|
||||
blurRadius?: number;
|
||||
sobelThreshold?: number;
|
||||
square?: boolean;
|
||||
}
|
||||
|
||||
export interface SeamCarveResult {
|
||||
@@ -14,31 +22,77 @@ export interface SeamCarveResult {
|
||||
height: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Discover the caire binary. Checks PATH (Docker installs to /usr/local/bin)
|
||||
* and the CAIRE_PATH env var for local development.
|
||||
*/
|
||||
let cachedCairePath: string | null = null;
|
||||
|
||||
async function findCaire(): Promise<string> {
|
||||
if (cachedCairePath) return cachedCairePath;
|
||||
|
||||
const candidates = process.env.CAIRE_PATH ? [process.env.CAIRE_PATH, "caire"] : ["caire"];
|
||||
|
||||
for (const cmd of candidates) {
|
||||
try {
|
||||
await execFileAsync(cmd, ["-help"], { timeout: 5_000 });
|
||||
cachedCairePath = cmd;
|
||||
return cmd;
|
||||
} catch {
|
||||
// try next
|
||||
}
|
||||
}
|
||||
throw new Error(
|
||||
"caire binary not found. Install via: go install github.com/esimov/caire/cmd/caire@v1.5.0",
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Content-aware resize using caire (Go seam carving engine).
|
||||
* Supports both shrinking and enlarging via seam removal/insertion.
|
||||
*/
|
||||
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");
|
||||
const cairePath = await findCaire();
|
||||
const id = randomUUID();
|
||||
const inputPath = join(outputDir, `caire-in-${id}.png`);
|
||||
const outputPath = join(outputDir, `caire-out-${id}.png`);
|
||||
|
||||
await writeFile(inputPath, inputBuffer);
|
||||
const { stdout } = await runPythonWithProgress(
|
||||
"seam_carve.py",
|
||||
[inputPath, outputPath, JSON.stringify(options)],
|
||||
{ onProgress },
|
||||
);
|
||||
try {
|
||||
await writeFile(inputPath, inputBuffer);
|
||||
|
||||
const result = JSON.parse(stdout);
|
||||
if (!result.success) {
|
||||
throw new Error(result.error || "Content-aware resize failed");
|
||||
// Build caire arguments
|
||||
const args = ["-in", inputPath, "-out", outputPath, "-preview=false"];
|
||||
|
||||
if (options.square) {
|
||||
// Caire -square requires -width and -height set to the shortest edge
|
||||
const meta = await sharp(inputBuffer).metadata();
|
||||
const shortest = Math.min(meta.width ?? 0, meta.height ?? 0);
|
||||
args.push("-square", "-width", String(shortest), "-height", String(shortest));
|
||||
} else {
|
||||
if (options.width) args.push("-width", String(options.width));
|
||||
if (options.height) args.push("-height", String(options.height));
|
||||
}
|
||||
|
||||
if (options.protectFaces) args.push("-face");
|
||||
if (options.blurRadius !== undefined) args.push("-blur", String(options.blurRadius));
|
||||
if (options.sobelThreshold !== undefined) args.push("-sobel", String(options.sobelThreshold));
|
||||
|
||||
await execFileAsync(cairePath, args, { timeout: 60_000 });
|
||||
|
||||
const buffer = await readFile(outputPath);
|
||||
const meta = await sharp(buffer).metadata();
|
||||
|
||||
return {
|
||||
buffer,
|
||||
width: meta.width ?? 0,
|
||||
height: meta.height ?? 0,
|
||||
};
|
||||
} finally {
|
||||
await rm(inputPath, { force: true }).catch(() => {});
|
||||
await rm(outputPath, { force: true }).catch(() => {});
|
||||
}
|
||||
|
||||
const buffer = await readFile(outputPath);
|
||||
return {
|
||||
buffer,
|
||||
width: result.width,
|
||||
height: result.height,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user