Improve smartcrop saliency testing/reporting

This commit is contained in:
Lovell Fuller 2018-09-05 22:49:31 +01:00
parent 0144358afb
commit 4cff62258c

View File

@ -7,7 +7,6 @@ const async = require('async');
const sharp = require('../../'); const sharp = require('../../');
const crops = { const crops = {
centre: sharp.gravity.centre,
entropy: sharp.strategy.entropy, entropy: sharp.strategy.entropy,
attention: sharp.strategy.attention attention: sharp.strategy.attention
}; };
@ -34,23 +33,35 @@ async.eachLimit(files, concurrency, function (file, done) {
const salientHeight = userData[file].bottom - userData[file].top; const salientHeight = userData[file].bottom - userData[file].top;
sharp(filename).metadata(function (err, metadata) { sharp(filename).metadata(function (err, metadata) {
if (err) console.log(err); if (err) console.log(err);
const marginWidth = metadata.width - salientWidth;
const marginHeight = metadata.height - salientHeight;
async.each(Object.keys(crops), function (crop, done) { async.each(Object.keys(crops), function (crop, done) {
async.parallel([ async.parallel([
// Left edge accuracy // Left edge accuracy
function (done) { function (done) {
if (marginWidth) {
sharp(filename).resize(salientWidth, metadata.height).crop(crops[crop]).toBuffer(function (err, data, info) { 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); const delta = Math.abs(userData[file].left + info.cropOffsetLeft);
const accuracy = Math.round(marginWidth / (marginWidth + delta) * 100);
incrementScore(accuracy, crop); incrementScore(accuracy, crop);
done(err); done(err);
}); });
} else {
done();
}
}, },
// Top edge accuracy // Top edge accuracy
function (done) { function (done) {
if (marginHeight) {
sharp(filename).resize(metadata.width, salientHeight).crop(crops[crop]).toBuffer(function (err, data, info) { 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); const delta = Math.abs(userData[file].top + info.cropOffsetTop);
const accuracy = Math.round(marginHeight / (marginHeight + delta) * 100);
incrementScore(accuracy, crop); incrementScore(accuracy, crop);
done(err); done(err);
}); });
} else {
done();
}
} }
], done); ], done);
}, done); }, done);
@ -60,7 +71,7 @@ async.eachLimit(files, concurrency, function (file, done) {
Object.keys(scores).forEach(function (accuracy) { Object.keys(scores).forEach(function (accuracy) {
report.push( report.push(
Object.assign({ Object.assign({
accuracy: parseInt(accuracy, 10) accuracy: Number(accuracy)
}, scores[accuracy]) }, scores[accuracy])
); );
}); });