Fix bug and adds text mask

This commit is contained in:
Thomas Forgione 2023-05-25 11:28:53 +02:00
parent 51a0f26d97
commit 90dbd18b26
1 changed files with 33 additions and 2 deletions

View File

@ -207,10 +207,11 @@ async function main() {
await fs.mkdir(outputDir); await fs.mkdir(outputDir);
} }
let lock = await fs.open(outputDir + "/.locator", 'a');
await lock.close();
} }
let lock = await fs.open(outputDir + "/.locator", 'a');
await lock.close();
} }
// Path to the HTML file to analyse (given as relative path from current directory) // Path to the HTML file to analyse (given as relative path from current directory)
@ -291,6 +292,36 @@ async function main() {
} }
} }
if (outputDir !== null) {
// Produce a screenshot without text
// Adds a style that makes text invisible
await page.evaluate(() => {
let style = document.createElement('style');
style.innerHTML = "* { color: rgba(0, 0, 0, 0) !important;";
style.id = "no-text-style";
document.head.appendChild(style);
});
// Perform the screenshot / mask computation
await page.screenshot({path: outputDir + '/' + 'no-text-' + pageIndex + '.png'});
await image.segmentationMask(
outputDir + '/screenshot2.png',
outputDir + '/no-text-' + pageIndex + '.png',
outputDir + '/no-text-mask-' + pageIndex + '.png',
threshold,
);
// Restore original element
await page.evaluate(() => {
let style = document.getElementById('no-text-style');
document.head.removeChild(style);
});
}
// Analyse the root and output the result // Analyse the root and output the result
info("performing analysis"); info("performing analysis");
let analyse = await analyseElement(root, page, outputDir, threshold); let analyse = await analyseElement(root, page, outputDir, threshold);