mirror of
https://github.com/ColonelParrot/jscanify.git
synced 2025-12-31 06:31:54 +00:00
Update test.js to process all test images
This commit is contained in:
parent
eff1b752fe
commit
b6ba8178fc
@ -7,7 +7,7 @@
|
|||||||
"doc": "docs"
|
"doc": "docs"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "mocha"
|
"test": "mocha --trace-uncaught"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
@ -6,71 +6,75 @@ console.log("RUNNING JSCANIFY TESTS");
|
|||||||
console.log("Warning: This may take a bit");
|
console.log("Warning: This may take a bit");
|
||||||
|
|
||||||
const { loadImage, createCanvas } = require("canvas");
|
const { loadImage, createCanvas } = require("canvas");
|
||||||
const { mkdirSync, writeFileSync, unlinkSync, existsSync } = require("fs");
|
const { mkdirSync, writeFileSync, unlinkSync, existsSync, readdirSync } = require("fs");
|
||||||
const assert = require("assert");
|
const assert = require("assert");
|
||||||
|
|
||||||
const jscanify = require("../src/jscanify-node");
|
const jscanify = require("../src/jscanify-node");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
const outputPaths = {
|
|
||||||
highlight: __dirname + "/output/highlighted.jpg",
|
|
||||||
extracted: __dirname + "/output/extracted.jpg",
|
|
||||||
cornerPoints: __dirname + "/output/corner_points.jpg",
|
|
||||||
};
|
|
||||||
|
|
||||||
const baseFolder = __dirname.replaceAll("\\", "/") + "/output/";
|
const OUTPUT_FOLDER = __dirname.replaceAll("\\", "/") + "/output/";
|
||||||
|
|
||||||
const TEST_IMAGE_PATH = path.join(
|
const TEST_IMAGE_DIRECTORY = path.join(
|
||||||
__dirname,
|
__dirname,
|
||||||
"..",
|
"..",
|
||||||
"docs",
|
"docs",
|
||||||
"images",
|
"images",
|
||||||
"test",
|
"test"
|
||||||
"test.png"
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* delete previously generated output images
|
||||||
|
*/
|
||||||
function setup() {
|
function setup() {
|
||||||
console.log("=== setting up tests ===");
|
console.log("=== setting up tests ===");
|
||||||
console.log("Deleting previously generated images");
|
console.log("Deleting previously generated images");
|
||||||
Object.values(outputPaths).forEach((path) => {
|
|
||||||
if (existsSync(path)) {
|
|
||||||
unlinkSync(path);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!existsSync(baseFolder)) {
|
if (!existsSync(OUTPUT_FOLDER)) {
|
||||||
mkdirSync(baseFolder);
|
mkdirSync(OUTPUT_FOLDER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
readdirSync(OUTPUT_FOLDER).forEach((file) => {
|
||||||
|
unlinkSync(path.join(OUTPUT_FOLDER, file));
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function test() {
|
console.log("=== beginning tests ===");
|
||||||
const scanner = new jscanify();
|
console.log("loading OpenCV.js...");
|
||||||
|
|
||||||
console.log("=== beginning tests ===");
|
const scanner = new jscanify();
|
||||||
console.log("loading OpenCV.js...");
|
scanner.loadOpenCV(function (cv) {
|
||||||
scanner.loadOpenCV(function (cv) {
|
|
||||||
console.log("Finished loading OpenCV.js");
|
console.log("Finished loading OpenCV.js");
|
||||||
console.log("Writing test images to: " + baseFolder);
|
console.log("Writing test images to: " + OUTPUT_FOLDER);
|
||||||
describe("feature tests", function () {
|
|
||||||
|
/**
|
||||||
|
* tests an individual image
|
||||||
|
*/
|
||||||
|
function test(testImage, imageCount) {
|
||||||
|
describe("image #" + imageCount, function () {
|
||||||
it("should highlight paper", function (done) {
|
it("should highlight paper", function (done) {
|
||||||
const highlighted = scanner.highlightPaper(testImage);
|
const highlighted = scanner.highlightPaper(testImage);
|
||||||
|
const higlightedOutputPath = OUTPUT_FOLDER + "highlighted-" + imageCount + ".jpg";
|
||||||
writeFileSync(
|
writeFileSync(
|
||||||
outputPaths.highlight,
|
higlightedOutputPath,
|
||||||
highlighted.toBuffer("image/jpeg")
|
highlighted.toBuffer("image/jpeg")
|
||||||
);
|
);
|
||||||
|
|
||||||
assert.ok(existsSync(outputPaths.highlight));
|
assert.ok(existsSync(higlightedOutputPath));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should extract paper", function (done) {
|
it("should extract paper", function (done) {
|
||||||
const extracted = scanner.extractPaper(testImage, 386, 500);
|
const extracted = scanner.extractPaper(testImage, 386, 500);
|
||||||
|
const extractedOutputPath = OUTPUT_FOLDER + "extracted-" + imageCount + ".jpg";
|
||||||
|
|
||||||
writeFileSync(
|
writeFileSync(
|
||||||
outputPaths.extracted,
|
extractedOutputPath,
|
||||||
extracted.toBuffer("image/jpeg")
|
extracted.toBuffer("image/jpeg")
|
||||||
);
|
);
|
||||||
|
|
||||||
assert.ok(existsSync(outputPaths.extracted));
|
assert.ok(existsSync(extractedOutputPath));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -103,18 +107,31 @@ function test() {
|
|||||||
ctx.fill();
|
ctx.fill();
|
||||||
});
|
});
|
||||||
|
|
||||||
writeFileSync(outputPaths.cornerPoints, canvas.toBuffer("image/jpeg"));
|
const cornerPointsOutputPath = OUTPUT_FOLDER + "corner_points-" + imageCount + ".jpg";
|
||||||
|
writeFileSync(cornerPointsOutputPath, canvas.toBuffer("image/jpeg"));
|
||||||
|
|
||||||
assert.ok(existsSync(outputPaths.cornerPoints));
|
assert.ok(existsSync(cornerPointsOutputPath));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let testImage;
|
|
||||||
loadImage(TEST_IMAGE_PATH).then(function (image) {
|
|
||||||
testImage = image;
|
|
||||||
setup();
|
setup();
|
||||||
test();
|
|
||||||
|
let imageCount = 1;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* go through all images in test image directory
|
||||||
|
*/
|
||||||
|
readdirSync(TEST_IMAGE_DIRECTORY).forEach((file) => {
|
||||||
|
const TEST_IMAGE_PATH = path.join(TEST_IMAGE_DIRECTORY, file);
|
||||||
|
|
||||||
|
if(!file.endsWith("-sized.png")){ // these images are for the website, not testing
|
||||||
|
let tempCount = imageCount++;
|
||||||
|
|
||||||
|
loadImage(TEST_IMAGE_PATH).then(function (image) {
|
||||||
|
test(image, tempCount);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
});
|
});
|
||||||
Loading…
x
Reference in New Issue
Block a user