mirror of
https://github.com/lovell/sharp.git
synced 2025-07-13 04:20:12 +02:00
Improve smartcrop saliency testing/reporting
This commit is contained in:
parent
0144358afb
commit
4cff62258c
@ -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) {
|
||||
if (marginWidth) {
|
||||
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);
|
||||
done(err);
|
||||
});
|
||||
} else {
|
||||
done();
|
||||
}
|
||||
},
|
||||
// Top edge accuracy
|
||||
function (done) {
|
||||
if (marginHeight) {
|
||||
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);
|
||||
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])
|
||||
);
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user