feat: add Deep Enhance AI tier to image enhancement

When deepEnhance is true and the upscale-enhance bundle is installed,
runs SCUNet (quality tier) after the Sharp pipeline for noise/artifact
cleanup. Falls back gracefully to Sharp-only if sidecar is unavailable.
This commit is contained in:
SnapOtter
2026-05-09 11:29:27 +08:00
parent bdcfb6647c
commit 40522ebe1a
2 changed files with 44 additions and 1 deletions
+26 -1
View File
@@ -1,12 +1,18 @@
import { randomUUID } from "node:crypto";
import { mkdir } from "node:fs/promises";
import { join } from "node:path";
import { noiseRemoval } from "@snapotter/ai";
import { analyzeImage, applyCorrections } from "@snapotter/image-engine";
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import sharp from "sharp";
import { z } from "zod";
import { autoOrient } from "../../lib/auto-orient.js";
import { isToolInstalled } from "../../lib/feature-status.js";
import { validateImageBuffer } from "../../lib/file-validation.js";
import { decodeToSharpCompat, needsCliDecode } from "../../lib/format-decoders.js";
import { decodeHeic } from "../../lib/heic-converter.js";
import { resolveOutputFormat } from "../../lib/output-format.js";
import { createWorkspace } from "../../lib/workspace.js";
import { createToolRoute } from "../tool-factory.js";
const settingsSchema = z.object({
@@ -22,6 +28,7 @@ const settingsSchema = z.object({
denoise: z.boolean().default(true),
})
.default({}),
deepEnhance: z.boolean().default(false),
});
type EnhancementSettings = z.infer<typeof settingsSchema>;
@@ -45,10 +52,28 @@ async function processImageEnhancement(
{ width: meta.width ?? 1, height: meta.height ?? 1 },
);
const buffer = await image
let buffer = await image
.toFormat(outputFormat.format, { quality: outputFormat.quality })
.toBuffer();
if (settings.deepEnhance && isToolInstalled("noise-removal")) {
try {
const jobId = randomUUID();
const workspacePath = await createWorkspace(jobId);
const outputDir = join(workspacePath, "output");
await mkdir(outputDir, { recursive: true });
const result = await noiseRemoval(buffer, outputDir, {
tier: "quality",
strength: 35,
detailPreservation: 70,
colorNoise: 20,
});
buffer = result.buffer;
} catch {
// SCUNet unavailable -- fall back to Sharp-only result
}
}
return { buffer, filename, contentType: outputFormat.contentType };
}
@@ -900,3 +900,21 @@ describe("Large file with modes", () => {
expect(result.processedSize).toBeGreaterThan(0);
});
});
// ── Deep Enhance ───────────────────────────────────────────────
describe("Deep Enhance", () => {
it("accepts deepEnhance setting and returns 200", async () => {
const res = await postTool({ deepEnhance: true });
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
expect(result.downloadUrl).toBeDefined();
expect(result.processedSize).toBeGreaterThan(0);
});
it("works without deepEnhance (default false)", async () => {
const res = await postTool({});
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
expect(result.downloadUrl).toBeDefined();
});
});