sharp-phash
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -61,3 +61,2 @@ 'use strict'; | ||
.greyscale() | ||
//.normalise() | ||
.resize(SAMPLE_SIZE, SAMPLE_SIZE) | ||
@@ -88,3 +87,3 @@ .ignoreAspectRatio() | ||
for(let y = 0; y < LOW_SIZE; y++) { | ||
totalSum += dct[x][y]; | ||
totalSum += dct[x + 1][y + 1]; | ||
} | ||
@@ -100,3 +99,3 @@ } | ||
for(let y = 0; y < LOW_SIZE; y++) { | ||
fingerprint += dct[x][y] > avg ? "1" : "0"; | ||
fingerprint += dct[x + 1][y + 1] > avg ? "1" : "0"; | ||
} | ||
@@ -103,0 +102,0 @@ } |
{ | ||
"name": "sharp-phash", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "sharp based perceptual hash implementation", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
85
test.js
'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const Promise = require('bluebird'); | ||
@@ -8,23 +9,75 @@ | ||
const phash = require('./'); | ||
const phash = require('./index'); | ||
const dist = require('./distance'); | ||
const img1 = fs.readFileSync('./Lenna.png'); | ||
const img2 = fs.readFileSync('./Lenna.jpg'); | ||
const img3 = fs.readFileSync('./Lenna-sepia.jpg'); | ||
const img4 = fs.readFileSync('./Lenna_exif-orientation-8.jpg'); | ||
const readFile = (fileName) => { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(fileName, (err, res) => { | ||
if(err) { | ||
return reject(err); | ||
} | ||
resolve(res); | ||
}); | ||
}); | ||
}; | ||
const lenna_png = 'Lenna.png'; | ||
const lenna_jpg = 'Lenna.jpg'; | ||
const lenna_sepia = 'Lenna-sepia.jpg'; | ||
const lenna_exif = 'Lenna_exif-orientation-8.jpg'; | ||
const fb = 'fb.jpg'; | ||
const xing = 'xing.jpg'; | ||
const fb1 = 'fb1.jpg'; | ||
const fb2 = 'fb2.jpg'; | ||
const fb3 = 'fb3.jpg'; | ||
const fb4 = 'fb4.jpg'; | ||
const fb5 = 'fb5.jpg'; | ||
const fb6 = 'fb6.jpg'; | ||
function getPHash(img) { | ||
return readFile(path.join('.', 'img', img)) | ||
.then(buf => phash(buf)); | ||
} | ||
function bitCount(hash) { | ||
return hash.replace(/0/g, '').length; | ||
} | ||
function testCase(img1, img2, cond) { | ||
return Promise.all([ | ||
getPHash(img1), | ||
getPHash(img2) | ||
]) | ||
.then(([hash1, hash2]) => { | ||
const d = dist(hash1, hash2); | ||
const text = `${img1} vs ${img2} | ||
hash1: ${hash1} (${bitCount(hash1)}) | ||
hash2: ${hash2} (${bitCount(hash2)}) | ||
distance: ${d} | ||
`; | ||
assert.ok(cond(d), text); | ||
}); | ||
} | ||
const SIMILAR = (d) => d <= 5; | ||
const LIKELY_SIMILAR = (d) => d <= 10; | ||
const NOT_SIMILAR = (d) => d > 10; | ||
Promise.all([ | ||
phash(img1), | ||
phash(img2), | ||
phash(img3), | ||
phash(img4) | ||
testCase(lenna_png, lenna_jpg, SIMILAR), | ||
testCase(lenna_jpg, lenna_sepia, SIMILAR), | ||
testCase(lenna_jpg, lenna_exif, SIMILAR), | ||
testCase(fb, xing, LIKELY_SIMILAR), | ||
testCase(fb1, fb2, NOT_SIMILAR), | ||
testCase(fb1, lenna_jpg, NOT_SIMILAR), | ||
testCase(fb3, fb4, NOT_SIMILAR), | ||
testCase(fb5, fb6, NOT_SIMILAR), | ||
]) | ||
.then(([hash1, hash2, hash3, hash4]) => { | ||
assert(dist(hash1, hash2) < 5); | ||
assert(dist(hash2, hash3) < 5); | ||
assert(dist(hash3, hash1) < 5); | ||
assert(dist(hash4, hash1) < 5); | ||
assert(dist(hash4, hash2) < 5); | ||
assert(dist(hash4, hash3) < 5); | ||
.catch(err => { | ||
console.log('Test fail'); | ||
console.log(err.message); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6639
163