Skip to content

Commit

Permalink
Improve smartcrop saliency testing/reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Sep 5, 2018
1 parent 0144358 commit 4cff622
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions test/saliency/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand All @@ -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);
Expand All @@ -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])
);
});
Expand Down

0 comments on commit 4cff622

Please sign in to comment.