jscanify 1.1.0 → 1.3.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 CHANGED
@@ -1,74 +1,137 @@
1
+ /*
2
+ run tests with: npm test
3
+ */
4
+
1
5
  console.log("RUNNING JSCANIFY TESTS");
2
6
  console.log("Warning: This may take a bit");
3
7
 
4
- const { Canvas, createCanvas, Image, ImageData, loadImage } = require("canvas");
5
- const { writeFileSync, unlinkSync, existsSync } = require("fs");
8
+ const { loadImage, createCanvas } = require("canvas");
9
+ const { mkdirSync, writeFileSync, unlinkSync, existsSync, readdirSync } = require("fs");
6
10
  const assert = require("assert");
7
11
 
8
12
  const jscanify = require("../src/jscanify-node");
9
13
  const path = require("path");
10
14
 
11
- const outputPaths = {
12
- highlight: __dirname + "/output/highlighted.jpg",
13
- extracted: __dirname + "/output/extracted.jpg",
14
- };
15
15
 
16
- const TEST_IMAGE_PATH = path.join(
16
+ const OUTPUT_FOLDER = __dirname.replaceAll("\\", "/") + "/output/";
17
+
18
+ const TEST_IMAGE_DIRECTORY = path.join(
17
19
  __dirname,
18
20
  "..",
19
21
  "docs",
20
22
  "images",
21
- "test",
22
- "test.png"
23
+ "test"
23
24
  );
24
25
 
26
+ /**
27
+ * delete previously generated output images
28
+ */
25
29
  function setup() {
26
30
  console.log("=== setting up tests ===");
27
31
  console.log("Deleting previously generated images");
28
- Object.values(outputPaths).forEach((path) => {
29
- if (existsSync(path)) {
30
- unlinkSync(path);
31
- }
32
- });
32
+
33
+ if (!existsSync(OUTPUT_FOLDER)) {
34
+ mkdirSync(OUTPUT_FOLDER);
35
+ }
36
+
37
+ readdirSync(OUTPUT_FOLDER).forEach((file) => {
38
+ unlinkSync(path.join(OUTPUT_FOLDER, file));
39
+ })
33
40
  }
34
41
 
35
- function test() {
36
- const scanner = new jscanify();
42
+ console.log("=== beginning tests ===");
43
+ console.log("loading OpenCV.js...");
44
+
45
+ const scanner = new jscanify();
46
+ scanner.loadOpenCV(function (cv) {
37
47
 
38
- console.log("=== beginning tests ===");
39
- console.log("loading OpenCV.js...");
40
- scanner.loadOpenCV(function (cv) {
41
- console.log("Finished loading OpenCV.js");
42
- describe("feature tests", function (done) {
48
+ console.log("Finished loading OpenCV.js");
49
+ console.log("Writing test images to: " + OUTPUT_FOLDER);
50
+
51
+ /**
52
+ * tests an individual image
53
+ */
54
+ function test(testImage, imageCount) {
55
+ describe("image #" + imageCount, function () {
43
56
  it("should highlight paper", function (done) {
44
57
  const highlighted = scanner.highlightPaper(testImage);
58
+ const higlightedOutputPath = OUTPUT_FOLDER + "highlighted-" + imageCount + ".jpg";
45
59
  writeFileSync(
46
- outputPaths.highlight,
60
+ higlightedOutputPath,
47
61
  highlighted.toBuffer("image/jpeg")
48
62
  );
49
63
 
50
- assert.ok(existsSync(outputPaths.highlight));
51
- done()
64
+ assert.ok(existsSync(higlightedOutputPath));
65
+ done();
52
66
  });
53
67
 
54
68
  it("should extract paper", function (done) {
55
- scanner.extractPaper(testImage, 386, 500, function (extracted) {
56
- writeFileSync(
57
- outputPaths.extracted,
58
- extracted.toBuffer("image/jpeg")
59
- );
60
-
61
- assert.ok(existsSync(outputPaths.extracted));
62
- done();
69
+ const extracted = scanner.extractPaper(testImage, 386, 500);
70
+ const extractedOutputPath = OUTPUT_FOLDER + "extracted-" + imageCount + ".jpg";
71
+
72
+ writeFileSync(
73
+ extractedOutputPath,
74
+ extracted.toBuffer("image/jpeg")
75
+ );
76
+
77
+ assert.ok(existsSync(extractedOutputPath));
78
+ done();
79
+ });
80
+
81
+ it("should label corner points", function (done) {
82
+ const parsedImage = cv.imread(testImage);
83
+ const paperContour = scanner.findPaperContour(parsedImage);
84
+ const {
85
+ topLeftCorner,
86
+ topRightCorner,
87
+ bottomLeftCorner,
88
+ bottomRightCorner,
89
+ } = scanner.getCornerPoints(paperContour, testImage);
90
+
91
+ const canvas = createCanvas();
92
+
93
+ cv.imshow(canvas, parsedImage);
94
+ const ctx = canvas.getContext("2d");
95
+ const points = [
96
+ { p: topLeftCorner, text: "top left corner" },
97
+ { p: topRightCorner, text: "top right corner" },
98
+ { p: bottomLeftCorner, text: "bottom left corner" },
99
+ { p: bottomRightCorner, text: "bottom right corner" },
100
+ ];
101
+ ctx.fillStyle = "cyan";
102
+ ctx.font = "25px serif";
103
+ points.forEach(({ p: point, text }) => {
104
+ ctx.beginPath();
105
+ ctx.arc(point.x, point.y, 15, 0, 2 * Math.PI, false);
106
+ ctx.fillText(text, point.x + 30, point.y)
107
+ ctx.fill();
63
108
  });
109
+
110
+ const cornerPointsOutputPath = OUTPUT_FOLDER + "corner_points-" + imageCount + ".jpg";
111
+ writeFileSync(cornerPointsOutputPath, canvas.toBuffer("image/jpeg"));
112
+
113
+ assert.ok(existsSync(cornerPointsOutputPath));
114
+ done();
64
115
  });
65
116
  });
66
- });
67
- }
117
+ }
68
118
 
69
- let testImage;
70
- loadImage(TEST_IMAGE_PATH).then(function (image) {
71
- testImage = image;
72
119
  setup();
73
- test();
74
- });
120
+
121
+ let imageCount = 1;
122
+
123
+ /*
124
+ * go through all images in test image directory
125
+ */
126
+ readdirSync(TEST_IMAGE_DIRECTORY).forEach((file) => {
127
+ const TEST_IMAGE_PATH = path.join(TEST_IMAGE_DIRECTORY, file);
128
+
129
+ if(!file.endsWith("-sized.png")){ // these images are for the website, not testing
130
+ let tempCount = imageCount++;
131
+
132
+ loadImage(TEST_IMAGE_PATH).then(function (image) {
133
+ test(image, tempCount);
134
+ });
135
+ }
136
+ })
137
+ });
package/install.md DELETED
@@ -1,5 +0,0 @@
1
- run
2
-
3
- ```
4
- npm run install_arm64
5
- ```