fix(canvas): quarantine unstable detailed inspection behind explicit opt-in

This commit is contained in:
134 2026-10-20 02:29:33 +08:00 committed by root
parent 79d10cf1ac
commit 3504141be5
3 changed files with 9 additions and 3 deletions

View File

@ -41,7 +41,7 @@ async function stageImage(image: Omit<PreparedImage, "url">, role: "template" |
return { ...image, url: await entry.url }; return { ...image, url: await entry.url };
} }
export async function prepareLayoutReplaceInputs(sourceUrl: string, referenceUrl: string) { export async function prepareLayoutReplaceInputs(sourceUrl: string, referenceUrl: string, options?: { checkText?: boolean }) {
if (sourceUrl === referenceUrl) throw new Error("新商品图和目标模版不能是同一张图片"); if (sourceUrl === referenceUrl) throw new Error("新商品图和目标模版不能是同一张图片");
const [originalSource, originalTemplate] = await Promise.all([ const [originalSource, originalTemplate] = await Promise.all([
readImage(sourceUrl), readImage(referenceUrl), readImage(sourceUrl), readImage(referenceUrl),
@ -52,7 +52,7 @@ export async function prepareLayoutReplaceInputs(sourceUrl: string, referenceUrl
// source so an incorrect crop cannot silently redefine the product identity. // source so an incorrect crop cannot silently redefine the product identity.
const [{ box, identity }, textBoundingBoxes] = await Promise.all([ const [{ box, identity }, textBoundingBoxes] = await Promise.all([
locateLayoutReplaceProduct(originalSource.buffer, originalTemplate.buffer), locateLayoutReplaceProduct(originalSource.buffer, originalTemplate.buffer),
locateLayoutReplaceTemplateText(originalTemplate.buffer), options?.checkText === false ? Promise.resolve([]) : locateLayoutReplaceTemplateText(originalTemplate.buffer),
]); ]);
const crop = layoutReplaceProductCrop(box, originalSource.width, originalSource.height); const crop = layoutReplaceProductCrop(box, originalSource.width, originalSource.height);
const productBuffer = await sharp(originalSource.buffer).extract(crop).png().toBuffer(); const productBuffer = await sharp(originalSource.buffer).extract(crop).png().toBuffer();

View File

@ -135,7 +135,8 @@ export async function executeCanvasLayoutReplaceTask(params: {
throw new Error("AI 模版替换未配置可用的非 FAL 多图编辑模型"); throw new Error("AI 模版替换未配置可用的非 FAL 多图编辑模型");
} }
const inputs = await prepareLayoutReplaceInputs(sourceImageUrl, referenceImageUrl); const detailedChecks = process.env.CANVAS_LAYOUT_REPLACE_DETAILED_CHECKS === "1";
const inputs = await prepareLayoutReplaceInputs(sourceImageUrl, referenceImageUrl, { checkText: detailedChecks });
const referenceGeometry = inputs.referenceGeometry; const referenceGeometry = inputs.referenceGeometry;
const sourceProfile = await resolveCanvasLayoutReplaceSourceProfile(sourceImageUrl, inputs.source.buffer); const sourceProfile = await resolveCanvasLayoutReplaceSourceProfile(sourceImageUrl, inputs.source.buffer);
const sourceProductAspectRatio = sourceProfile?.silhouetteAspectRatio || inputs.productAspectRatio; const sourceProductAspectRatio = sourceProfile?.silhouetteAspectRatio || inputs.productAspectRatio;
@ -200,6 +201,7 @@ export async function executeCanvasLayoutReplaceTask(params: {
verify: result => verifyCanvasLayoutReplacement({ verify: result => verifyCanvasLayoutReplacement({
sourceBuffer: inputs.sourceOriginalBuffer, templateBuffer: inputs.template.buffer, sourceBuffer: inputs.sourceOriginalBuffer, templateBuffer: inputs.template.buffer,
sourceIdentity: inputs.sourceIdentity, sourceIdentity: inputs.sourceIdentity,
detailedChecks,
productBuffer: inputs.source.buffer, productBuffer: inputs.source.buffer,
textBoundingBoxes: inputs.textBoundingBoxes, textBoundingBoxes: inputs.textBoundingBoxes,
resultUrl: result.images[0].originalUrl, resultUrl: result.images[0].originalUrl,

View File

@ -112,6 +112,7 @@ export async function verifyCanvasLayoutReplacement(params: {
sourceIdentity?: string; sourceIdentity?: string;
productBuffer?: Buffer; productBuffer?: Buffer;
textBoundingBoxes?: NormalizedReplacementBox[]; textBoundingBoxes?: NormalizedReplacementBox[];
detailedChecks?: boolean;
}) { }) {
const resultBuffer = params.resultBuffer || await fetchImageBufferFromUrl(params.resultUrl); const resultBuffer = params.resultBuffer || await fetchImageBufferFromUrl(params.resultUrl);
const prompt = [LAYOUT_REPLACE_QUALITY_PROMPT, params.sourceIdentity const prompt = [LAYOUT_REPLACE_QUALITY_PROMPT, params.sourceIdentity
@ -119,6 +120,9 @@ export async function verifyCanvasLayoutReplacement(params: {
const board = await makeLayoutReplaceQualityBoard({ template: params.templateBuffer, source: params.sourceBuffer, const board = await makeLayoutReplaceQualityBoard({ template: params.templateBuffer, source: params.sourceBuffer,
product: params.productBuffer || params.sourceBuffer, result: resultBuffer }); product: params.productBuffer || params.sourceBuffer, result: resultBuffer });
const quality = await inspectLayoutImages([board], prompt, parseLayoutReplaceQuality); const quality = await inspectLayoutImages([board], prompt, parseLayoutReplaceQuality);
// Fine-grained OCR/support classification is experimental: real fixtures
// showed provider-dependent false rejections. Keep it opt-in until stable.
if (params.detailedChecks === false) return quality;
if (quality.sourceProductPreserved) { if (quality.sourceProductPreserved) {
// Read the actual result product at the same scale as the source crop. // Read the actual result product at the same scale as the source crop.
// Poster copy and floor shadows must not obscure its support structure. // Poster copy and floor shadows must not obscure its support structure.