Skip to content

Commit

Permalink
Update hamming distance algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewquang512 committed Nov 19, 2023
1 parent 6dc9f3c commit 8b8e0d7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
53 changes: 50 additions & 3 deletions src/resolvers/Common/compareImages.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import Jimp from 'Jimp';

export function compareImages(image1hash, image2hash) {
// Perceived distance
const hammingDistance = findHammingDistance(image1hash, image2hash);
console.log(`compareImages: hamming distance: ${hammingDistance.toFixed(3)}`);
const vihammingDistance = findHammingDistance(image1hash, image2hash);
const jimphammingDistance = Jimp.compareHashes(image1hash, image2hash);

console.log(
`compareImages: vihammingDistance distance: ${vihammingDistance}`,
);
console.log(
`compareImages: jimphammingDistance distance: ${jimphammingDistance}`,
);

if (hammingDistance <= 10) {
if (vihammingDistance <= 10 && jimphammingDistance < 0.15) {
console.log('compareImages: Images match!');
return true;
}
Expand All @@ -23,3 +32,41 @@ const findHammingDistance = (str1 = '', str2 = '') => {
}
return 0;
};
//
// const findLevenDistance = (a, b) => {
// if (a.length == 0) return b.length;
// if (b.length == 0) return a.length;
//
// var matrix = [];
//
// // increment along the first column of each row
// var i;
// for (i = 0; i <= b.length; i++) {
// matrix[i] = [i];
// }
//
// // increment each column in the first row
// var j;
// for (j = 0; j <= a.length; j++) {
// matrix[0][j] = j;
// }
//
// // Fill in the rest of the matrix
// for (i = 1; i <= b.length; i++) {
// for (j = 1; j <= a.length; j++) {
// if (b.charAt(i - 1) == a.charAt(j - 1)) {
// matrix[i][j] = matrix[i - 1][j - 1];
// } else {
// matrix[i][j] = Math.min(
// matrix[i - 1][j - 1] + 1, // substitution
// Math.min(
// matrix[i][j - 1] + 1, // insertion
// matrix[i - 1][j] + 1,
// ),
// ); // deletion
// }
// }
// }
//
// return matrix[b.length][a.length];
// };
2 changes: 1 addition & 1 deletion src/resolvers/Common/hashImage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Jimp from 'jimp';
import Jimp from 'Jimp';

export async function hashImage(imageUrl) {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/Mutation/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const utilityMutation = {
const hash1 = images[0].hash;
const hash2 = images[1].hash;

const result = compareImages(hash1, hash2);
const result = await compareImages(hash1, hash2);
return {
isSimilar: result,
post1Imageurl: images[0].url,
Expand Down

0 comments on commit 8b8e0d7

Please sign in to comment.