obartra / ssim

🖼🔬 JavaScript Image Comparison
http://obartra.github.io/ssim
MIT License
298 stars 14 forks source link

Can't use the package #313

Closed dev-bun closed 1 year ago

dev-bun commented 2 years ago

/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```
obartra commented 2 years ago

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

dev-bun commented 2 years ago
    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

stale[bot] commented 2 years ago

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.

paavanredd commented 2 years ago

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

obartra commented 2 years ago

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

stale[bot] commented 1 year ago

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.

TalkLounge commented 1 year ago

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();