rsmbl / Resemble.js

Image analysis and comparison

Home Page:http://rsmbl.github.io/Resemble.js/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

data.misMatchPercentage returning undefined

csudarshan259 opened this issue · comments

Hi, In below code data.misMatchPercentage is returning undefined. I'm taking image url links from a csv file and comparing them by using for loop and resemblejs. Thanks.

const express = require('express');
const bodyParser = require('body-parser');
const { response } = require('express');
const csv = require('csvtojson');
const resemble = require('resemblejs');

const app = express();
const port = process.env.PORT || 4041;

resemble.outputSettings({ useCrossOrigin: false });

async function imageSimilarityManipulation(req, res) {

    var jsonarray = await csvtojsn();
    var jsonarray2 = jsonarray;
    let count =0;

    var obj = [{}];
    for (let index = 0; index < 100; index++) {
        const element = jsonarray[index];

        for (let j = index + 1; j < 100; j++) {
            const element2 = jsonarray2[j];
            
            var diff = await resemble(element['Image'])
                .compareTo(element2['Image'])
                .onComplete(function (data) {
                    console.log(element['Image'],element2['Image'],data.misMatchPercentage);

                   let img_comp = {
                       "image1" : element['Image'],
                       "image2": element2['Image'],
                       "misMatchPercentage":data.misMatchPercentage
                   };
                    obj.unshift(img_comp);

                    if(data.misMatchPercentage < 30){
                        console.log("near to match");

                       
                    }
                    //console.log(data.misMatchPercentage);
                   // console.log('count:',obj[count]);
                    count +=1;
                });

                
        }
       
    }



    return res.json(obj);
}
async function csvtojsn() {
    const csvFilePath = './hotel_images.csv'

    csv().fromFile(csvFilePath)
        .then((jsonObj) => {
            //  console.log(jsonObj);
        });

    const jsonArray = await csv().fromFile(csvFilePath);
    //console.log(jsonArray);
    return jsonArray;

}


app.get('/', (_, res) => {
    res.send("Express app");
});

app.get('/images', imageSimilarityManipulation);

app.listen(port, () => {
    console.log(`server is running on port: ${port}`)
});

You need to pass Resemble the image data. E.g. (I've not testing this but perhaps this will work.)

let image1 = await axios.get( element['Image'], {responseType: 'arraybuffer'});
let image2 = await axios.get( element2['Image'], {responseType: 'arraybuffer'});
resemble(image1.data).compareTo(image2.data);