ssim icon indicating copy to clipboard operation
ssim copied to clipboard

Can't use the package

Open dev-bun opened this issue 2 years ago β€’ 3 comments

/root/domain-images/node_modules/ssim.js/dist/matlab/rgb2gray.js:48
    var array = new Array(width * height);
                ^

RangeError: Invalid array length
    at Object.rgb2grayInteger (/root/domain-images/node_modules/ssim.js/dist/matlab/rgb2gray.js:48:17)
    at toGrayScale (/root/domain-images/node_modules/ssim.js/dist/index.js:69:26)
    at ssim (/root/domain-images/node_modules/ssim.js/dist/index.js:91:39)
    at /root/domain-images/app.js:21:36```

dev-bun avatar May 29 '22 16:05 dev-bun

That's not much detail to go on. My guess is that the image is not converted correctly to an array for some reason but hard to tell without more info

obartra avatar May 29 '22 16:05 obartra

    const browser = await puppeteer.launch({
        args:[
            " --no-sandbox"
        ]
    });
    const page = await browser.newPage();
    await page.goto(`https://${domain}`);
    await page.screenshot({ path: `screenshots/${domain.replace(".", "-")}.png` });
    const img1 = loadImage(`known/hype.png`);
    const img2 = loadImage(`/screenshots/${domain.replace(".", "-")}.png`);
    const { mssim, performance } = ssim(img1, img2);
    console.log(`SSIM: ${mssim} (${performance}ms)`);

this is my code - its takes a screenshot of a domain then compares it to a known scam domain screenshot

dev-bun avatar May 29 '22 16:05 dev-bun

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 13 '22 05:08 stale[bot]

My code is also throwing same error.

const ssim = require("ssim.js");
const { loadImage } = require("canvas");
try{
    const img1 = loadImage("./WIN_20221017_16_49_44_Pro.jpg");
    const img2 = loadImage("./WIN_20221017_16_50_34_Pro.jpg");
    const result = ssim.ssim(img1, img2);    
    console.log('SSIM: ${result.mssim}, \n Time to compute : (${performance}ms')
}
catch(err) {
    console.error('Error on SSIM : ', err)
}

Error generating SSIM RangeError: Invalid array length at Object.rgb2grayInteger (C:\Users\Paavan Reddy\Desktop\CJPL\NestJS\biometric\node_modules\ssim.js\dist\matlab\rgb2gray.js:48:17) at toGrayScale (C:\Users\Paavan Reddy\Desktop\CJPL\NestJS\biometric\node_modules\ssim.js\dist\index.js:69:26) at Object.ssim (C:\Users\Paavan Reddy\Desktop\CJPL\NestJS\biometric\node_modules\ssim.js\dist\index.js:91:39) at Object. (C:\Users\Paavan Reddy\Desktop\CJPL\NestJS\biometric\ssim_face_recong.js:7:25) at Module._compile (node:internal/modules/cjs/loader:1126:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10) at Module.load (node:internal/modules/cjs/loader:1004:32) at Function.Module._load (node:internal/modules/cjs/loader:839:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:17:47

paavanredd avatar Oct 21 '22 04:10 paavanredd

If it fails there it's failing to get the width or height (or both). I would log the data before passing it to ssim to make sure it has the right shape

obartra avatar Oct 30 '22 17:10 obartra

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 08 '23 03:01 stale[bot]

Workaround:

const { ssim } = require("ssim.js");
const { createCanvas, loadImage } = require("canvas");

async function generateImageData(file) {
    const image = await loadImage(file);
    const canvas = createCanvas(image.width, image.height);
    const context = canvas.getContext('2d');
    context.drawImage(image, 0, 0);
    return {
        data: context.getImageData(0, 0, image.width, image.height).data,
        width: image.width,
        height: image.height
    };
}

async function init() {
    console.log(ssim(await generateImageData("C:/img1.jpg"), await generateImageData("C:/img2.jpg")));
}

init();

TalkLounge avatar Jun 24 '23 18:06 TalkLounge