mirror of
https://github.com/ashim-hq/ashim
synced 2026-04-21 13:37:52 +00:00
feat: add seam carving AI bridge module
This commit is contained in:
parent
18119166f0
commit
d3b646207d
2 changed files with 45 additions and 0 deletions
|
|
@ -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";
|
||||
|
|
|
|||
44
packages/ai/src/seam-carving.ts
Normal file
44
packages/ai/src/seam-carving.ts
Normal 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,
|
||||
};
|
||||
}
|
||||
Loading…
Reference in a new issue