-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathtemplate-matching-finder.class.spec.ts
110 lines (95 loc) · 5.05 KB
/
template-matching-finder.class.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import * as path from "path";
import {Image} from "../../image.class";
import {MatchRequest} from "../../match-request.class";
import {Region} from "../../region.class";
import {ImageReader} from "./image-reader.class";
import TemplateMatchingFinder from "./template-matching-finder.class";
describe("Template-matching finder", () => {
it("findMatch should return a match when present in image", async () => {
// GIVEN
const imageLoader = new ImageReader();
const SUT = new TemplateMatchingFinder();
const haystackPath = path.resolve(__dirname, "./__mocks__/mouse.png");
const needlePath = path.resolve(__dirname, "./__mocks__/needle.png");
const haystack = await imageLoader.load(haystackPath);
const needle = await imageLoader.load(needlePath);
const minConfidence = 0.99;
const searchRegion = new Region(0, 0, haystack.width, haystack.height);
const matchRequest = new MatchRequest(haystack, needlePath, searchRegion, minConfidence);
const expectedResult = new Region(16, 31, needle.width, needle.height);
// WHEN
const result = await SUT.findMatch(matchRequest);
// THEN
expect(result.confidence).toBeGreaterThanOrEqual(minConfidence);
expect(result.location).toEqual(expectedResult);
});
it("findMatch should return a match within a search region when present in image", async () => {
// GIVEN
const imageLoader = new ImageReader();
const SUT = new TemplateMatchingFinder();
const haystackPath = path.resolve(__dirname, "./__mocks__/mouse.png");
const needlePath = path.resolve(__dirname, "./__mocks__/needle.png");
const haystack = await imageLoader.load(haystackPath);
const needle = await imageLoader.load(needlePath);
const minConfidence = 0.99;
const searchRegion = new Region(10, 20, 140, 100);
const matchRequest = new MatchRequest(haystack, needlePath, searchRegion, minConfidence);
const expectedResult = new Region(6, 11, needle.width, needle.height);
// WHEN
const result = await SUT.findMatch(matchRequest);
// THEN
expect(result.confidence).toBeGreaterThanOrEqual(minConfidence);
expect(result.location).toEqual(expectedResult);
});
it("findMatch should return confidence and location of best match if no match with sufficient confidence is found", async () => {
// GIVEN
const imageLoader = new ImageReader();
const SUT = new TemplateMatchingFinder();
const haystackPath = path.resolve(__dirname, "./__mocks__/downloads.png");
const needlePath = path.resolve(__dirname, "./__mocks__/coverage.png");
const haystack = await imageLoader.load(haystackPath);
const minConfidence = 0.99;
const searchRegion = new Region(0, 0, 320, 72);
const matchRequest = new MatchRequest(haystack, needlePath, searchRegion, minConfidence);
const expectedRejection = new RegExp(`^No match with required confidence ${minConfidence}. Best match: \\d.\\d* at \\(\\d*, \\d*, \\d*, \\d*\\)$`)
// WHEN
// THEN
await expect(SUT.findMatch(matchRequest))
.rejects
.toThrowError(expectedRejection);
});
it("findMatch should throw on invalid image paths", async () => {
// GIVEN
const imageLoader = new ImageReader();
const SUT = new TemplateMatchingFinder();
const pathToNeedle = path.resolve(__dirname, "./__mocks__/mouse.png");
const pathToHaystack = "./__mocks__/foo.png";
const needle = await imageLoader.load(pathToNeedle);
const minConfidence = 0.99;
const searchRegion = new Region(0, 0, 100, 100);
const haystack = new Image(needle.width, needle.height, needle.data, 3);
const matchRequest = new MatchRequest(haystack, pathToHaystack, searchRegion, minConfidence);
// WHEN
const result = SUT.findMatch(matchRequest);
// THEN
await expect(result)
.rejects
.toThrowError(`Failed to load ${pathToHaystack}. Reason: 'Failed to load image from '${pathToHaystack}''.`);
});
it("findMatch should reject, if needle was way lager than the haystack", async () => {
// GIVEN
const imageLoader = new ImageReader();
const SUT = new TemplateMatchingFinder();
const haystackPath = path.resolve(__dirname, "./__mocks__/mouse.png");
const needlePath = path.resolve(__dirname, "./__mocks__/fat-needle.png");
const haystack = await imageLoader.load(haystackPath);
const minConfidence = 0.99;
const searchRegion = new Region(0, 0, haystack.width, haystack.height);
const matchRequest = new MatchRequest(haystack, needlePath, searchRegion, minConfidence);
const expectedRejection = new Error("The provided image sample is larger than the provided search region")
// WHEN
const findMatchPromise = SUT.findMatch(matchRequest);
// THEN
await expect(findMatchPromise).rejects.toEqual(expectedRejection)
});
});