jscanify 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/tests.js ADDED
@@ -0,0 +1,120 @@
1
+ /*
2
+ run tests with: npm test
3
+ */
4
+
5
+ console.log("RUNNING JSCANIFY TESTS");
6
+ console.log("Warning: This may take a bit");
7
+
8
+ const { loadImage, createCanvas } = require("canvas");
9
+ const { mkdirSync, writeFileSync, unlinkSync, existsSync } = require("fs");
10
+ const assert = require("assert");
11
+
12
+ const jscanify = require("../src/jscanify-node");
13
+ const path = require("path");
14
+
15
+ const outputPaths = {
16
+ highlight: __dirname + "/output/highlighted.jpg",
17
+ extracted: __dirname + "/output/extracted.jpg",
18
+ cornerPoints: __dirname + "/output/corner_points.jpg",
19
+ };
20
+
21
+ const baseFolder = __dirname.replaceAll("\\", "/") + "/output/";
22
+
23
+ const TEST_IMAGE_PATH = path.join(
24
+ __dirname,
25
+ "..",
26
+ "docs",
27
+ "images",
28
+ "test",
29
+ "test.png"
30
+ );
31
+
32
+ function setup() {
33
+ console.log("=== setting up tests ===");
34
+ console.log("Deleting previously generated images");
35
+ Object.values(outputPaths).forEach((path) => {
36
+ if (existsSync(path)) {
37
+ unlinkSync(path);
38
+ }
39
+ });
40
+
41
+ if (!existsSync(baseFolder)) {
42
+ mkdirSync(baseFolder);
43
+ }
44
+ }
45
+
46
+ function test() {
47
+ const scanner = new jscanify();
48
+
49
+ console.log("=== beginning tests ===");
50
+ console.log("loading OpenCV.js...");
51
+ scanner.loadOpenCV(function (cv) {
52
+ console.log("Finished loading OpenCV.js");
53
+ console.log("Writing test images to: " + baseFolder);
54
+ describe("feature tests", function () {
55
+ it("should highlight paper", function (done) {
56
+ const highlighted = scanner.highlightPaper(testImage);
57
+ writeFileSync(
58
+ outputPaths.highlight,
59
+ highlighted.toBuffer("image/jpeg")
60
+ );
61
+
62
+ assert.ok(existsSync(outputPaths.highlight));
63
+ done();
64
+ });
65
+
66
+ it("should extract paper", function (done) {
67
+ const extracted = scanner.extractPaper(testImage, 386, 500);
68
+ writeFileSync(
69
+ outputPaths.extracted,
70
+ extracted.toBuffer("image/jpeg")
71
+ );
72
+
73
+ assert.ok(existsSync(outputPaths.extracted));
74
+ done();
75
+ });
76
+
77
+ it("should label corner points", function (done) {
78
+ const parsedImage = cv.imread(testImage);
79
+ const paperContour = scanner.findPaperContour(parsedImage);
80
+ const {
81
+ topLeftCorner,
82
+ topRightCorner,
83
+ bottomLeftCorner,
84
+ bottomRightCorner,
85
+ } = scanner.getCornerPoints(paperContour, testImage);
86
+
87
+ const canvas = createCanvas();
88
+
89
+ cv.imshow(canvas, parsedImage);
90
+ const ctx = canvas.getContext("2d");
91
+ const points = [
92
+ { p: topLeftCorner, text: "top left corner" },
93
+ { p: topRightCorner, text: "top right corner" },
94
+ { p: bottomLeftCorner, text: "bottom left corner" },
95
+ { p: bottomRightCorner, text: "bottom right corner" },
96
+ ];
97
+ ctx.fillStyle = "cyan";
98
+ ctx.font = "25px serif";
99
+ points.forEach(({ p: point, text }) => {
100
+ ctx.beginPath();
101
+ ctx.arc(point.x, point.y, 15, 0, 2 * Math.PI, false);
102
+ ctx.fillText(text, point.x + 30, point.y)
103
+ ctx.fill();
104
+ });
105
+
106
+ writeFileSync(outputPaths.cornerPoints, canvas.toBuffer("image/jpeg"));
107
+
108
+ assert.ok(existsSync(outputPaths.cornerPoints));
109
+ done();
110
+ });
111
+ });
112
+ });
113
+ }
114
+
115
+ let testImage;
116
+ loadImage(TEST_IMAGE_PATH).then(function (image) {
117
+ testImage = image;
118
+ setup();
119
+ test();
120
+ });
Binary file