From 4cff62258c9a1bfa11480080880cbbc639f4ba58 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Wed, 5 Sep 2018 22:49:31 +0100 Subject: [PATCH] Improve smartcrop saliency testing/reporting --- test/saliency/report.js | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/test/saliency/report.js b/test/saliency/report.js index 42c64ad3..c1daf4f7 100644 --- a/test/saliency/report.js +++ b/test/saliency/report.js @@ -7,7 +7,6 @@ const async = require('async'); const sharp = require('../../'); const crops = { - centre: sharp.gravity.centre, entropy: sharp.strategy.entropy, attention: sharp.strategy.attention }; @@ -34,23 +33,35 @@ async.eachLimit(files, concurrency, function (file, done) { const salientHeight = userData[file].bottom - userData[file].top; sharp(filename).metadata(function (err, metadata) { if (err) console.log(err); + const marginWidth = metadata.width - salientWidth; + const marginHeight = metadata.height - salientHeight; async.each(Object.keys(crops), function (crop, done) { async.parallel([ // Left edge accuracy function (done) { - sharp(filename).resize(salientWidth, metadata.height).crop(crops[crop]).toBuffer(function (err, data, info) { - const accuracy = Math.round(Math.abs(userData[file].left - info.cropCalcLeft) / (metadata.width - salientWidth) * 100); - incrementScore(accuracy, crop); - done(err); - }); + if (marginWidth) { + sharp(filename).resize(salientWidth, metadata.height).crop(crops[crop]).toBuffer(function (err, data, info) { + const delta = Math.abs(userData[file].left + info.cropOffsetLeft); + const accuracy = Math.round(marginWidth / (marginWidth + delta) * 100); + incrementScore(accuracy, crop); + done(err); + }); + } else { + done(); + } }, // Top edge accuracy function (done) { - sharp(filename).resize(metadata.width, salientHeight).crop(crops[crop]).toBuffer(function (err, data, info) { - const accuracy = Math.round(Math.abs(userData[file].top - info.cropCalcTop) / (metadata.height - salientHeight) * 100); - incrementScore(accuracy, crop); - done(err); - }); + if (marginHeight) { + sharp(filename).resize(metadata.width, salientHeight).crop(crops[crop]).toBuffer(function (err, data, info) { + const delta = Math.abs(userData[file].top + info.cropOffsetTop); + const accuracy = Math.round(marginHeight / (marginHeight + delta) * 100); + incrementScore(accuracy, crop); + done(err); + }); + } else { + done(); + } } ], done); }, done); @@ -60,7 +71,7 @@ async.eachLimit(files, concurrency, function (file, done) { Object.keys(scores).forEach(function (accuracy) { report.push( Object.assign({ - accuracy: parseInt(accuracy, 10) + accuracy: Number(accuracy) }, scores[accuracy]) ); });