Files
Recorder-Tabber/src/app/api/recognize/route.ts
T
Cursor Agentandchen 59d65333e3 Add a soprano recorder tab converter as a Linux AppImage.
The app turns a lead line into Baroque fingering tabs with rhythm kept, ships Twinkle and Ode to Joy, and reads photos locally with oemer when the bundled Python is present.

Co-authored-by: catpersonisme <catpersonisme@gmail.com>
2026-10-09 03:35:16 +00:00

53 lines
1.6 KiB
TypeScript

import { connection } from "next/server";
import { recognizeImage, recognizerInfo } from "@/lib/recognize";
const TYPES = new Set(["image/png", "image/jpeg", "image/webp"]);
export async function GET() {
await connection();
return Response.json(recognizerInfo());
}
export async function POST(request: Request) {
let form: FormData;
try {
form = await request.formData();
} catch {
return Response.json({ error: "The upload could not be read. No notes were invented." }, { status: 400 });
}
const image = form.get("image");
if (!(image instanceof File)) {
return Response.json({ error: "Choose a PNG, JPG, or WEBP image first." }, { status: 400 });
}
if (image.size === 0) {
return Response.json(
{ error: "That file is empty. No notes were invented." },
{ status: 400 },
);
}
const named = /\.(png|jpe?g|webp)$/i.test(image.name);
if (image.type && !TYPES.has(image.type) && !named) {
return Response.json(
{ error: "Use a PNG, JPG, or WEBP photo. That file is not one of those. No notes were invented." },
{ status: 415 },
);
}
if (image.size > 8 * 1024 * 1024) {
return Response.json(
{ error: "That image is over 8 MB. Crop the line or export a smaller JPEG." },
{ status: 413 },
);
}
const result = await recognizeImage(image);
if (!result.ok) {
return Response.json({ error: result.error, recognizer: result.recognizer }, { status: result.status });
}
return Response.json({
score: result.score,
warnings: result.warnings,
recognizer: result.recognizer,
});
}