feat: SOTA AI photo restoration with multi-step pipeline (#58) (#62)

Add comprehensive photo restoration tool that chains multiple AI models:
- Scratch/tear/spot detection via morphological analysis (top-hat/black-hat transforms)
- Damage inpainting via LaMa ONNX model (reuses existing infrastructure)
- Face enhancement via CodeFormer ONNX (~377MB, from facefusion/models-3.0.0)
- Noise reduction via OpenCV NLMeans in LAB color space
- Optional B&W auto-colorization via DDColor (reuses existing model)

Settings: 3 restoration modes (Light/Auto/Heavy), individual feature toggles
for scratch removal, face enhancement (with fidelity slider), denoising
(with strength slider), and auto-colorize. Before/after comparison view.

Handles HEIC, HEIF, and all standard formats. Batch processing supported.
No new Python dependencies - reuses onnxruntime, cv2, mediapipe, PIL.

Co-authored-by: stirling-image <stirling-image@users.noreply.github.com>
This commit is contained in:
stirling-image
2026-04-13 21:57:51 +08:00
committed by GitHub
co-authored by stirling-image
parent 8071fe61c5
commit 6a43cc1b77
11 changed files with 1224 additions and 0 deletions
+1
View File
@@ -8,5 +8,6 @@ export { inpaint } from "./inpainting.js";
export { noiseRemoval } from "./noise-removal.js";
export { extractText } from "./ocr.js";
export { removeRedEye } from "./red-eye-removal.js";
export { restorePhoto } from "./restoration.js";
export { seamCarve } from "./seam-carving.js";
export { upscale } from "./upscaling.js";
+59
View File
@@ -0,0 +1,59 @@
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
export interface RestorePhotoOptions {
mode?: string;
scratchRemoval?: boolean;
faceEnhancement?: boolean;
fidelity?: number;
denoise?: boolean;
denoiseStrength?: number;
colorize?: boolean;
}
export interface RestorePhotoResult {
buffer: Buffer;
width: number;
height: number;
steps: string[];
scratchCoverage: number;
facesEnhanced: number;
isGrayscale: boolean;
colorized: boolean;
}
export async function restorePhoto(
inputBuffer: Buffer,
outputDir: string,
options: RestorePhotoOptions = {},
onProgress?: ProgressCallback,
): Promise<RestorePhotoResult> {
const inputPath = join(outputDir, "input_restore.png");
const outputPath = join(outputDir, "output_restore.png");
await writeFile(inputPath, inputBuffer);
const { stdout } = await runPythonWithProgress(
"restore.py",
[inputPath, outputPath, JSON.stringify(options)],
{ onProgress },
);
const result = JSON.parse(stdout);
if (!result.success) {
throw new Error(result.error || "Photo restoration failed");
}
const actualOutputPath = result.output_path || outputPath;
const buffer = await readFile(actualOutputPath);
return {
buffer,
width: result.width,
height: result.height,
steps: result.steps ?? [],
scratchCoverage: result.scratchCoverage ?? 0,
facesEnhanced: result.facesEnhanced ?? 0,
isGrayscale: result.isGrayscale ?? false,
colorized: result.colorized ?? false,
};
}