// deep_diag.js - deep diagnostic using the actual PaperRecognizer internal data const { createCanvas, loadImage } = require('canvas'); const fs = require('fs'); // Patch paper-recognizer to capture internal state const orig = require('./paper-recognizer.js'); // We'll hook into the module by reading the source and injecting debug const code = fs.readFileSync('./paper-recognizer.js', 'utf8'); // Write a version with debug logging at key points const debugCode = code .replace( 'const bigCands = candidates.filter(c => c.isBig && c.area >= GP.MIN_BIG_LOCATOR_AREA && c.area <= GP.MAX_BIG_LOCATOR_AREA);', `console.log(' [DEBUG] totalCandidates=' + candidates.length); for (const c of candidates) console.log(' ' + (c.isBig?'BIG':'SMALL') + ' color=' + c.color + ' area=' + c.area + ' ratio=' + c.aspectRatio.toFixed(2) + ' x=' + c.center.x.toFixed(0) + ',y=' + c.center.y.toFixed(0)); const bigCands = candidates.filter(c => c.isBig && c.area >= GP.MIN_BIG_LOCATOR_AREA && c.area <= GP.MAX_BIG_LOCATOR_AREA);` ) .replace( 'const smallCands = candidates.filter(c => c.isSmall && c.area >= GP.MIN_SMALL_LOCATOR_AREA && c.area <= GP.MAX_SMALL_LOCATOR_AREA);', `const smallCands = candidates.filter(c => c.isSmall && c.area >= GP.MIN_SMALL_LOCATOR_AREA && c.area <= GP.MAX_SMALL_LOCATOR_AREA); console.log(' [DEBUG] bigCands=' + bigCands.length + ' smallCands=' + smallCands.length); for (const c of bigCands) console.log(' BIG: area=' + c.area + ' color=' + c.color + ' x=' + c.center.x.toFixed(0)); for (const c of smallCands) console.log(' SMALL: area=' + c.area + ' color=' + c.color + ' x=' + c.center.x.toFixed(0));` ) .replace( 'if (bigCands.length === 0 || smallCands.length === 0) {', `console.log(' [DEBUG] entering fallback check: bigs=' + bigCands.length + ' smalls=' + smallCands.length); if (bigCands.length === 0 || smallCands.length === 0) {` ) .replace( 'return { success: false, errorCode: -3, errorMessage: \'No locators found\' };', `console.log(' [DEBUG] RETURN -3: no locators'); return { success: false, errorCode: -3, errorMessage: 'No locators found' };` ) .replace( 'return { success: false, errorCode: -4, errorMessage: \'No matching locator pair\' };', `console.log(' [DEBUG] RETURN -4: no matching pair'); return { success: false, errorCode: -4, errorMessage: 'No matching locator pair' };` ) .replace( 'const paperType = matchConfig(distRatio, areaRatio2, bigType);', `const paperType = matchConfig(distRatio, areaRatio2, bigType); console.log(' [DEBUG] matchConfig: distRatio=' + distRatio.toFixed(2) + ' areaRatio=' + areaRatio2.toFixed(2) + ' bigType=' + bigType + ' paperType=' + paperType);` ); // Write temp file fs.writeFileSync('./paper-recognizer-debug.js', debugCode); const { PaperRecognizer } = require('./paper-recognizer-debug.js'); async function test(f) { const img = await loadImage(f); const c = createCanvas(img.width, img.height); const ctx = c.getContext('2d'); ctx.drawImage(img, 0, 0); const id = ctx.getImageData(0, 0, img.width, img.height); const r = new PaperRecognizer(); const result = r.recognize(new Uint8Array(id.data.buffer), img.width, img.height); console.log(' RESULT: ' + result.errorCode + ' ' + result.errorMessage); r.free(); } (async () => { const dir = '/home/ubuntu/nanocode/web-opencv/cpp/images/'; await test(dir + '4b85f78723d00b4fb4ef06cc4ca6f1e8.jpg'); })();