Files
SnapOtter/packages/ai/src/inpainting.ts
T
Siddharth Kumar Sah 5524939b6f feat: add Phase 4 AI tools with Python bridge and 6 new tools
Add Python bridge (packages/ai/src/bridge.ts) that calls Python scripts
via child_process with venv-first fallback to system python3. Implements
6 AI-powered tools:

- Remove Background: rembg-based with U2-Net/IS-Net models
- Image Upscaling: Real-ESRGAN with Lanczos fallback
- OCR/Text Extraction: Tesseract + PaddleOCR engines
- Face/PII Blur: MediaPipe face detection with configurable blur
- Object Eraser: LaMa inpainting with mask-based input
- Smart Crop: Sharp attention-based entropy cropping (no Python needed)

Each tool includes: Python script, TypeScript wrapper, API route,
and React settings component. All Python scripts handle ImportError
gracefully with clear installation messages.
2026-03-22 04:31:49 +08:00

30 lines
785 B
TypeScript

import { runPythonScript } from "./bridge.js";
import { writeFile, readFile } from "node:fs/promises";
import { join } from "node:path";
export async function inpaint(
inputBuffer: Buffer,
maskBuffer: Buffer,
outputDir: string,
): Promise<Buffer> {
const inputPath = join(outputDir, "input_inpaint.png");
const maskPath = join(outputDir, "mask_inpaint.png");
const outputPath = join(outputDir, "output_inpaint.png");
await writeFile(inputPath, inputBuffer);
await writeFile(maskPath, maskBuffer);
const { stdout } = await runPythonScript("inpaint.py", [
inputPath,
maskPath,
outputPath,
]);
const result = JSON.parse(stdout);
if (!result.success) {
throw new Error(result.error || "Inpainting failed");
}
return readFile(outputPath);
}