@rickcedwhat/playwright-smart-vision 0.1.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/dist/configure.d.ts +21 -0
- package/dist/configure.d.ts.map +1 -0
- package/dist/configure.js +77 -0
- package/dist/configure.js.map +1 -0
- package/dist/element.d.ts +177 -0
- package/dist/element.d.ts.map +1 -0
- package/dist/element.js +433 -0
- package/dist/element.js.map +1 -0
- package/dist/element.test.d.ts +2 -0
- package/dist/element.test.d.ts.map +1 -0
- package/dist/element.test.js +207 -0
- package/dist/element.test.js.map +1 -0
- package/dist/field-extractor.d.ts +63 -0
- package/dist/field-extractor.d.ts.map +1 -0
- package/dist/field-extractor.js +438 -0
- package/dist/field-extractor.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/ocr-overlay.d.ts +21 -0
- package/dist/ocr-overlay.d.ts.map +1 -0
- package/dist/ocr-overlay.js +102 -0
- package/dist/ocr-overlay.js.map +1 -0
- package/dist/ocr-step.d.ts +15 -0
- package/dist/ocr-step.d.ts.map +1 -0
- package/dist/ocr-step.js +41 -0
- package/dist/ocr-step.js.map +1 -0
- package/dist/ocr-test.d.ts +11 -0
- package/dist/ocr-test.d.ts.map +1 -0
- package/dist/ocr-test.js +35 -0
- package/dist/ocr-test.js.map +1 -0
- package/dist/playwright-helper.d.ts +64 -0
- package/dist/playwright-helper.d.ts.map +1 -0
- package/dist/playwright-helper.js +142 -0
- package/dist/playwright-helper.js.map +1 -0
- package/dist/screen-config.d.ts +91 -0
- package/dist/screen-config.d.ts.map +1 -0
- package/dist/screen-config.js +203 -0
- package/dist/screen-config.js.map +1 -0
- package/dist/screen-result.d.ts +103 -0
- package/dist/screen-result.d.ts.map +1 -0
- package/dist/screen-result.js +313 -0
- package/dist/screen-result.js.map +1 -0
- package/dist/typed-screen.d.ts +44 -0
- package/dist/typed-screen.d.ts.map +1 -0
- package/dist/typed-screen.js +72 -0
- package/dist/typed-screen.js.map +1 -0
- package/dist/types.d.ts +204 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +19 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/ocr.d.ts +52 -0
- package/dist/utils/ocr.d.ts.map +1 -0
- package/dist/utils/ocr.js +252 -0
- package/dist/utils/ocr.js.map +1 -0
- package/dist/utils/ocr.test.d.ts +2 -0
- package/dist/utils/ocr.test.d.ts.map +1 -0
- package/dist/utils/ocr.test.js +145 -0
- package/dist/utils/ocr.test.js.map +1 -0
- package/dist/utils/vision.d.ts +116 -0
- package/dist/utils/vision.d.ts.map +1 -0
- package/dist/utils/vision.js +340 -0
- package/dist/utils/vision.js.map +1 -0
- package/package.json +59 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ocr.js","sourceRoot":"","sources":["../../src/utils/ocr.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAG5C,MAAM,KAAK,GAAG,4BAA4B,CAAC;AAC3C,MAAM,KAAK,GAAG,KAAK,CAAC,WAAW,EAAE,CAAC;AAClC,MAAM,MAAM,GAAG,YAAY,CAAC;AAE5B,MAAM,CAAC,MAAM,eAAe,GAA2B;IACrD,IAAI,EAAE,GAAG,KAAK,GAAG,KAAK,GAAG,MAAM,OAAO;IACtC,MAAM,EAAE,GAAG,MAAM,KAAK;IACtB,KAAK,EAAE,GAAG,KAAK,GAAG,KAAK,GAAG,MAAM,EAAE;IAClC,KAAK,EAAE,GAAG,KAAK,GAAG,KAAK,GAAG,MAAM,MAAM;IACtC,GAAG,EAAE,GAAG,KAAK,GAAG,KAAK,GAAG,MAAM,EAAE;CACjC,CAAC;AAEF,MAAM,UAAU,eAAe,CAAC,IAAI,GAAG,EAAE,EAAE,IAAI,GAAG,EAAE,EAAE,MAAM,GAAG,MAAM;IACnE,IAAI,IAAI,KAAK,UAAU;QAAE,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@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { ocrTextMatches, normalizeOcrText, charsetForField } from './ocr.js';
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
// ocrTextMatches
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
describe('ocrTextMatches — RegExp', () => {
|
|
7
|
+
it('matches when the pattern tests true', () => {
|
|
8
|
+
expect(ocrTextMatches('ABC123', /^ABC/)).toBe(true);
|
|
9
|
+
});
|
|
10
|
+
it('does not match when the pattern tests false', () => {
|
|
11
|
+
expect(ocrTextMatches('XYZ', /^ABC/)).toBe(false);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
describe('ocrTextMatches — plain string (default: substring / swap-aware)', () => {
|
|
15
|
+
it('matches an identical string', () => {
|
|
16
|
+
expect(ocrTextMatches('hello', 'hello')).toBe(true);
|
|
17
|
+
});
|
|
18
|
+
it('matches when expected is a substring of actual', () => {
|
|
19
|
+
expect(ocrTextMatches('hello world', 'hello')).toBe(true);
|
|
20
|
+
});
|
|
21
|
+
it('does not match unrelated strings', () => {
|
|
22
|
+
expect(ocrTextMatches('world', 'hello')).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
it('does not match when expected is longer than actual', () => {
|
|
25
|
+
expect(ocrTextMatches('hi', 'hello')).toBe(false);
|
|
26
|
+
});
|
|
27
|
+
it('matches an empty expected against any actual', () => {
|
|
28
|
+
expect(ocrTextMatches('anything', '')).toBe(true);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
describe('ocrTextMatches — swaps', () => {
|
|
32
|
+
it('accepts an allowed swap glyph', () => {
|
|
33
|
+
// OCR reads '@' as 'Q'
|
|
34
|
+
expect(ocrTextMatches('USER Q EXAMPLE.COM', 'USER @ EXAMPLE.COM', { swaps: { '@': 'Q' } })).toBe(true);
|
|
35
|
+
});
|
|
36
|
+
it('accepts one of multiple allowed swap glyphs', () => {
|
|
37
|
+
expect(ocrTextMatches('5NPDH4AE1DH', '5NPDH4AE1DH', { swaps: { '0': ['O', 'D'] } })).toBe(true);
|
|
38
|
+
expect(ocrTextMatches('SNPDH4AE1DH', '5NPDH4AE1DH', { swaps: { '5': 'S' } })).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
it('rejects a glyph that is not in the swap list', () => {
|
|
41
|
+
expect(ocrTextMatches('USER Z EXAMPLE.COM', 'USER @ EXAMPLE.COM', { swaps: { '@': 'Q' } })).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
it('falls back to substring match when no swap matches', () => {
|
|
44
|
+
// no swaps needed — direct substring match wins
|
|
45
|
+
expect(ocrTextMatches('HELLO WORLD', 'HELLO', {})).toBe(true);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
describe('ocrTextMatches — exact mode', () => {
|
|
49
|
+
it('requires the full string to match char by char', () => {
|
|
50
|
+
expect(ocrTextMatches('hello', 'hello', { exact: true })).toBe(true);
|
|
51
|
+
});
|
|
52
|
+
it('rejects a longer actual with trailing content', () => {
|
|
53
|
+
expect(ocrTextMatches('hello world', 'hello', { exact: true })).toBe(false);
|
|
54
|
+
});
|
|
55
|
+
it('accepts exact match with swaps', () => {
|
|
56
|
+
expect(ocrTextMatches('SNPDH', '5NPDH', { exact: true, swaps: { '5': 'S' } })).toBe(true);
|
|
57
|
+
});
|
|
58
|
+
it('rejects mismatched length even with swaps', () => {
|
|
59
|
+
expect(ocrTextMatches('SN', '5NPDH', { exact: true, swaps: { '5': 'S' } })).toBe(false);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
describe('ocrTextMatches — overflow: end (value clips at the right)', () => {
|
|
63
|
+
it('matches when actual is a left-anchored prefix of expected', () => {
|
|
64
|
+
// Field shows "760 54" instead of "760 543 2987" (clipped at right)
|
|
65
|
+
expect(ocrTextMatches('760 54', '760 543 2987', { overflow: 'end' })).toBe(true);
|
|
66
|
+
});
|
|
67
|
+
it('requires a minimum number of chars to be present', () => {
|
|
68
|
+
// 'keep' for a 12-char expected = max(3, ceil(12*0.5)) = 6, so fewer than 6 leading chars fails
|
|
69
|
+
expect(ocrTextMatches('76', '760 543 2987', { overflow: 'end' })).toBe(false);
|
|
70
|
+
});
|
|
71
|
+
it('rejects if the actual chars do not match the start of expected', () => {
|
|
72
|
+
expect(ocrTextMatches('XXX 543', '760 543 2987', { overflow: 'end' })).toBe(false);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
describe('ocrTextMatches — overflow: start (value clips at the left)', () => {
|
|
76
|
+
// minKeep for a 5-char expected = max(3, ceil(5*0.5)) = 3, so ≥3 suffix chars are required.
|
|
77
|
+
it('matches when actual is a right-anchored suffix of expected', () => {
|
|
78
|
+
expect(ocrTextMatches('LLO', 'HELLO', { overflow: 'start' })).toBe(true);
|
|
79
|
+
});
|
|
80
|
+
it('rejects if tail chars do not match end of expected', () => {
|
|
81
|
+
expect(ocrTextMatches('XXX', 'HELLO', { overflow: 'start' })).toBe(false);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
describe('ocrTextMatches — overflow: both (clipped on either side)', () => {
|
|
85
|
+
// minKeep for a 5-char expected = 3, so ≥3 interior chars (after slop trim) are required.
|
|
86
|
+
it('matches a middle slice of expected', () => {
|
|
87
|
+
expect(ocrTextMatches('ELL', 'HELLO', { overflow: 'both' })).toBe(true);
|
|
88
|
+
});
|
|
89
|
+
it('rejects a string that does not appear in expected', () => {
|
|
90
|
+
expect(ocrTextMatches('XYZ', 'HELLO', { overflow: 'both' })).toBe(false);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
// ---------------------------------------------------------------------------
|
|
94
|
+
// normalizeOcrText
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
describe('normalizeOcrText', () => {
|
|
97
|
+
it('trims leading and trailing whitespace', () => {
|
|
98
|
+
expect(normalizeOcrText(' hello ')).toBe('hello');
|
|
99
|
+
});
|
|
100
|
+
it('collapses newlines to a single space', () => {
|
|
101
|
+
expect(normalizeOcrText('line1\nline2')).toBe('line1 line2');
|
|
102
|
+
});
|
|
103
|
+
it('collapses multiple newlines', () => {
|
|
104
|
+
expect(normalizeOcrText('a\n\n\nb')).toBe('a b');
|
|
105
|
+
});
|
|
106
|
+
it('handles an empty string', () => {
|
|
107
|
+
expect(normalizeOcrText('')).toBe('');
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
// ---------------------------------------------------------------------------
|
|
111
|
+
// charsetForField
|
|
112
|
+
// ---------------------------------------------------------------------------
|
|
113
|
+
describe('charsetForField', () => {
|
|
114
|
+
it('returns undefined for checkbox type', () => {
|
|
115
|
+
expect(charsetForField('active', 'checkbox')).toBeUndefined();
|
|
116
|
+
});
|
|
117
|
+
it('returns email charset when name includes "email"', () => {
|
|
118
|
+
const charset = charsetForField('email');
|
|
119
|
+
expect(charset).toContain('@');
|
|
120
|
+
});
|
|
121
|
+
it('returns digits charset for phone fields', () => {
|
|
122
|
+
const charset = charsetForField('homePhone');
|
|
123
|
+
expect(charset).not.toContain('A');
|
|
124
|
+
});
|
|
125
|
+
it('returns digits charset for year fields', () => {
|
|
126
|
+
const charset = charsetForField('year');
|
|
127
|
+
expect(charset).not.toContain('A');
|
|
128
|
+
});
|
|
129
|
+
it('returns vin charset when name includes "vin"', () => {
|
|
130
|
+
const charset = charsetForField('vin');
|
|
131
|
+
expect(charset).toContain('A');
|
|
132
|
+
expect(charset).toContain('0');
|
|
133
|
+
});
|
|
134
|
+
it('honours an explicit preset over the name heuristic', () => {
|
|
135
|
+
const digits = charsetForField('email', '', 'digits');
|
|
136
|
+
expect(digits).not.toContain('@');
|
|
137
|
+
});
|
|
138
|
+
it('returns text charset as the default', () => {
|
|
139
|
+
const charset = charsetForField('customerNumber');
|
|
140
|
+
expect(charset).toContain('A');
|
|
141
|
+
expect(charset).toContain('0');
|
|
142
|
+
expect(charset).toContain(' ');
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
//# sourceMappingURL=ocr.test.js.map
|
|
@@ -0,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AAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IACpC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,qCAAqC,EAAE,GAAG,EAAE;QAC7C,MAAM,OAAO,GAAG,eAAe,CAAC,gBAAgB,CAAC,CAAC;QAClD,MAAM,CAAC,OAAO,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;QAC/B,MAAM,CAAC,OAAO,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;QAC/B,MAAM,CAAC,OAAO,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IACjC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/** Await this before constructing a VisionUtil or calling any cv API. */
|
|
2
|
+
export declare function ensureCvReady(): Promise<void>;
|
|
3
|
+
export interface Point {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
}
|
|
7
|
+
export interface Rect {
|
|
8
|
+
x: number;
|
|
9
|
+
y: number;
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
}
|
|
13
|
+
export interface MatchResult {
|
|
14
|
+
location: Point;
|
|
15
|
+
confidence: number;
|
|
16
|
+
rect: Rect;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Computer vision utilities for image processing, template matching, and alignment
|
|
20
|
+
*/
|
|
21
|
+
export declare class VisionUtil {
|
|
22
|
+
/**
|
|
23
|
+
* Load an image from a buffer into an OpenCV Mat
|
|
24
|
+
*/
|
|
25
|
+
loadImage(buffer: Buffer): any;
|
|
26
|
+
/**
|
|
27
|
+
* Convert a colored image to grayscale
|
|
28
|
+
*/
|
|
29
|
+
toGrayscale(image: any): any;
|
|
30
|
+
/**
|
|
31
|
+
* Perform template matching to find a template image within a larger image
|
|
32
|
+
* Returns the best match location and confidence
|
|
33
|
+
*/
|
|
34
|
+
matchTemplate(source: any, template: any, method?: number): MatchResult;
|
|
35
|
+
/**
|
|
36
|
+
* Extract a region of interest (ROI) from an image
|
|
37
|
+
*/
|
|
38
|
+
extractROI(image: any, rect: Rect): any;
|
|
39
|
+
/**
|
|
40
|
+
* Compare two images and count the number of different pixels
|
|
41
|
+
* Returns the count and whether they're considered different
|
|
42
|
+
*/
|
|
43
|
+
compareRegions(image1: any, image2: any, threshold?: number, minDiffPixels?: number): {
|
|
44
|
+
different: boolean;
|
|
45
|
+
diffPixelCount: number;
|
|
46
|
+
diffPercentage: number;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Create a difference image highlighting where two images differ
|
|
50
|
+
*/
|
|
51
|
+
createDiffImage(image1: any, image2: any, threshold?: number): any;
|
|
52
|
+
/**
|
|
53
|
+
* Keep filled-form pixels that changed vs blank; paint unchanged pixels white.
|
|
54
|
+
* Labels and box borders drop out so OCR reads only typed values.
|
|
55
|
+
*/
|
|
56
|
+
isolateChangedForOcr(filled: any, blank: any, threshold?: number): any;
|
|
57
|
+
/**
|
|
58
|
+
* Count pixels darker than `limit` (after converting color crops to gray).
|
|
59
|
+
*/
|
|
60
|
+
countDarkPixels(image: any, limit?: number): number;
|
|
61
|
+
hasEnoughInk(image: any, minPixels?: number): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Whether a checkbox crop looks checked.
|
|
64
|
+
* Ignores the box border (inset) and looks for ink in the interior, so an
|
|
65
|
+
* empty box is unchecked without a second template or a blank-screenshot match.
|
|
66
|
+
*/
|
|
67
|
+
checkboxChecked(image: any, options?: {
|
|
68
|
+
inset?: number;
|
|
69
|
+
darkLimit?: number;
|
|
70
|
+
minInk?: number;
|
|
71
|
+
}): boolean;
|
|
72
|
+
ocrPrepOptions(image: any, options?: {
|
|
73
|
+
charset?: string;
|
|
74
|
+
scale?: number;
|
|
75
|
+
}): {
|
|
76
|
+
scale: number;
|
|
77
|
+
threshold?: number;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Upscale, pad, and binarize a crop so Tesseract can read 18px UI text.
|
|
81
|
+
* Short crops use a fixed threshold — Otsu on tiny digits fattens 0 into 8.
|
|
82
|
+
*/
|
|
83
|
+
prepareForOcr(image: any, scale?: number, options?: {
|
|
84
|
+
threshold?: number;
|
|
85
|
+
charset?: string;
|
|
86
|
+
}): any;
|
|
87
|
+
/**
|
|
88
|
+
* Draw labeled rectangles on a clone of the screenshot (RGBA or gray).
|
|
89
|
+
*/
|
|
90
|
+
annotateRects(image: any, boxes: Array<{
|
|
91
|
+
rect: Rect;
|
|
92
|
+
label?: string;
|
|
93
|
+
color?: [number, number, number, number];
|
|
94
|
+
}>): any;
|
|
95
|
+
/**
|
|
96
|
+
* Align two images using feature detection and homography
|
|
97
|
+
* This corrects for rotation, skew, and perspective differences
|
|
98
|
+
*/
|
|
99
|
+
alignImages(filledForm: any, blankForm: any): any;
|
|
100
|
+
/**
|
|
101
|
+
* Convert a cv.Mat to a PNG buffer
|
|
102
|
+
*/
|
|
103
|
+
matToBuffer(mat: any): Buffer;
|
|
104
|
+
/**
|
|
105
|
+
* Use pixelmatch for fast pixel-level comparison
|
|
106
|
+
* Returns the number of mismatched pixels
|
|
107
|
+
*/
|
|
108
|
+
pixelMatch(img1: Buffer, img2: Buffer, options?: {
|
|
109
|
+
threshold?: number;
|
|
110
|
+
includeAA?: boolean;
|
|
111
|
+
}): {
|
|
112
|
+
mismatchedPixels: number;
|
|
113
|
+
diffBuffer: Buffer | null;
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=vision.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vision.d.ts","sourceRoot":"","sources":["../../src/utils/vision.ts"],"names":[],"mappings":"AASA,yEAAyE;AACzE,wBAAsB,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC,CAEnD;AAGD,MAAM,WAAW,KAAK;IACpB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACX;AAED,MAAM,WA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
// @ts-ignore - @techstark/opencv-js doesn't have complete type definitions
|
|
2
|
+
import cvModule from '@techstark/opencv-js';
|
|
3
|
+
import { PNG } from 'pngjs';
|
|
4
|
+
// The opencv-js package exports a Promise (WASM init), not the API directly.
|
|
5
|
+
// cv is set once ensureCvReady() resolves.
|
|
6
|
+
let cv = null;
|
|
7
|
+
const _cvInit = Promise.resolve(cvModule).then((resolved) => { cv = resolved; });
|
|
8
|
+
/** Await this before constructing a VisionUtil or calling any cv API. */
|
|
9
|
+
export async function ensureCvReady() {
|
|
10
|
+
await _cvInit;
|
|
11
|
+
}
|
|
12
|
+
import pixelmatch from 'pixelmatch';
|
|
13
|
+
/**
|
|
14
|
+
* Computer vision utilities for image processing, template matching, and alignment
|
|
15
|
+
*/
|
|
16
|
+
export class VisionUtil {
|
|
17
|
+
/**
|
|
18
|
+
* Load an image from a buffer into an OpenCV Mat
|
|
19
|
+
*/
|
|
20
|
+
loadImage(buffer) {
|
|
21
|
+
const png = PNG.sync.read(buffer);
|
|
22
|
+
const mat = new cv.Mat(png.height, png.width, cv.CV_8UC4);
|
|
23
|
+
mat.data.set(png.data);
|
|
24
|
+
return mat;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Convert a colored image to grayscale
|
|
28
|
+
*/
|
|
29
|
+
toGrayscale(image) {
|
|
30
|
+
const gray = new cv.Mat();
|
|
31
|
+
cv.cvtColor(image, gray, cv.COLOR_RGBA2GRAY);
|
|
32
|
+
return gray;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Perform template matching to find a template image within a larger image
|
|
36
|
+
* Returns the best match location and confidence
|
|
37
|
+
*/
|
|
38
|
+
matchTemplate(source, template, method = cv.TM_CCOEFF_NORMED) {
|
|
39
|
+
const result = new cv.Mat();
|
|
40
|
+
const mask = new cv.Mat();
|
|
41
|
+
cv.matchTemplate(source, template, result, method, mask);
|
|
42
|
+
const minMax = cv.minMaxLoc(result);
|
|
43
|
+
const matchLoc = minMax.maxLoc;
|
|
44
|
+
result.delete();
|
|
45
|
+
mask.delete();
|
|
46
|
+
return {
|
|
47
|
+
location: { x: matchLoc.x, y: matchLoc.y },
|
|
48
|
+
confidence: minMax.maxVal,
|
|
49
|
+
rect: {
|
|
50
|
+
x: matchLoc.x,
|
|
51
|
+
y: matchLoc.y,
|
|
52
|
+
width: template.cols,
|
|
53
|
+
height: template.rows,
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Extract a region of interest (ROI) from an image
|
|
59
|
+
*/
|
|
60
|
+
extractROI(image, rect) {
|
|
61
|
+
const roi = new cv.Rect(rect.x, rect.y, rect.width, rect.height);
|
|
62
|
+
return image.roi(roi);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Compare two images and count the number of different pixels
|
|
66
|
+
* Returns the count and whether they're considered different
|
|
67
|
+
*/
|
|
68
|
+
compareRegions(image1, image2, threshold = 80, minDiffPixels = 10) {
|
|
69
|
+
if (image1.rows !== image2.rows || image1.cols !== image2.cols) {
|
|
70
|
+
return {
|
|
71
|
+
different: true,
|
|
72
|
+
diffPixelCount: -1,
|
|
73
|
+
diffPercentage: 100,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
const diff = new cv.Mat();
|
|
77
|
+
cv.absdiff(image1, image2, diff);
|
|
78
|
+
cv.threshold(diff, diff, threshold, 255, cv.THRESH_BINARY);
|
|
79
|
+
const diffPixelCount = cv.countNonZero(diff);
|
|
80
|
+
const totalPixels = image1.rows * image1.cols;
|
|
81
|
+
const diffPercentage = (diffPixelCount / totalPixels) * 100;
|
|
82
|
+
diff.delete();
|
|
83
|
+
return {
|
|
84
|
+
different: diffPixelCount > minDiffPixels,
|
|
85
|
+
diffPixelCount,
|
|
86
|
+
diffPercentage,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Create a difference image highlighting where two images differ
|
|
91
|
+
*/
|
|
92
|
+
createDiffImage(image1, image2, threshold = 80) {
|
|
93
|
+
const diff = new cv.Mat();
|
|
94
|
+
cv.absdiff(image1, image2, diff);
|
|
95
|
+
cv.threshold(diff, diff, threshold, 255, cv.THRESH_BINARY);
|
|
96
|
+
return diff;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Keep filled-form pixels that changed vs blank; paint unchanged pixels white.
|
|
100
|
+
* Labels and box borders drop out so OCR reads only typed values.
|
|
101
|
+
*/
|
|
102
|
+
isolateChangedForOcr(filled, blank, threshold = 50) {
|
|
103
|
+
if (filled.rows !== blank.rows || filled.cols !== blank.cols) {
|
|
104
|
+
return filled.clone();
|
|
105
|
+
}
|
|
106
|
+
const diff = new cv.Mat();
|
|
107
|
+
cv.absdiff(filled, blank, diff);
|
|
108
|
+
const mask = new cv.Mat();
|
|
109
|
+
cv.threshold(diff, mask, threshold, 255, cv.THRESH_BINARY);
|
|
110
|
+
diff.delete();
|
|
111
|
+
// White background; copy filled pixels only where they differ from blank.
|
|
112
|
+
const white = new cv.Mat(filled.rows, filled.cols, filled.type(), new cv.Scalar(255, 255, 255, 255));
|
|
113
|
+
filled.copyTo(white, mask);
|
|
114
|
+
mask.delete();
|
|
115
|
+
return white;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Count pixels darker than `limit` (after converting color crops to gray).
|
|
119
|
+
*/
|
|
120
|
+
countDarkPixels(image, limit = 200) {
|
|
121
|
+
let gray = image;
|
|
122
|
+
let allocatedGray = false;
|
|
123
|
+
if (image.channels() > 1) {
|
|
124
|
+
gray = this.toGrayscale(image);
|
|
125
|
+
allocatedGray = true;
|
|
126
|
+
}
|
|
127
|
+
let dark = 0;
|
|
128
|
+
const data = gray.data;
|
|
129
|
+
for (let i = 0; i < data.length; i++) {
|
|
130
|
+
if (data[i] < limit)
|
|
131
|
+
dark += 1;
|
|
132
|
+
}
|
|
133
|
+
if (allocatedGray)
|
|
134
|
+
gray.delete();
|
|
135
|
+
return dark;
|
|
136
|
+
}
|
|
137
|
+
hasEnoughInk(image, minPixels = 8) {
|
|
138
|
+
return this.countDarkPixels(image) >= minPixels;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Whether a checkbox crop looks checked.
|
|
142
|
+
* Ignores the box border (inset) and looks for ink in the interior, so an
|
|
143
|
+
* empty box is unchecked without a second template or a blank-screenshot match.
|
|
144
|
+
*/
|
|
145
|
+
checkboxChecked(image, options = {}) {
|
|
146
|
+
const inset = options.inset ?? 2;
|
|
147
|
+
const darkLimit = options.darkLimit ?? 180;
|
|
148
|
+
const minInk = options.minInk ?? 6;
|
|
149
|
+
const padX = Math.min(inset, Math.max(0, Math.floor(image.cols / 4)));
|
|
150
|
+
const padY = Math.min(inset, Math.max(0, Math.floor(image.rows / 4)));
|
|
151
|
+
const width = image.cols - padX * 2;
|
|
152
|
+
const height = image.rows - padY * 2;
|
|
153
|
+
if (width < 2 || height < 2) {
|
|
154
|
+
return this.countDarkPixels(image, darkLimit) >= minInk;
|
|
155
|
+
}
|
|
156
|
+
const inner = this.extractROI(image, { x: padX, y: padY, width, height });
|
|
157
|
+
const dark = this.countDarkPixels(inner, darkLimit);
|
|
158
|
+
inner.delete();
|
|
159
|
+
return dark >= minInk;
|
|
160
|
+
}
|
|
161
|
+
ocrPrepOptions(image, options = {}) {
|
|
162
|
+
const rows = image.rows ?? 0;
|
|
163
|
+
// Fixed 200 on short crops turns @ into C. Otsu at 3× keeps the ring.
|
|
164
|
+
let scale = 3;
|
|
165
|
+
let threshold;
|
|
166
|
+
if (options.charset?.includes('@')) {
|
|
167
|
+
scale = 3;
|
|
168
|
+
}
|
|
169
|
+
else if (rows <= 24) {
|
|
170
|
+
scale = 3;
|
|
171
|
+
threshold = 200;
|
|
172
|
+
}
|
|
173
|
+
if (options.scale && options.scale >= 2 && options.scale <= 8)
|
|
174
|
+
scale = options.scale;
|
|
175
|
+
return threshold == null ? { scale } : { scale, threshold };
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Upscale, pad, and binarize a crop so Tesseract can read 18px UI text.
|
|
179
|
+
* Short crops use a fixed threshold — Otsu on tiny digits fattens 0 into 8.
|
|
180
|
+
*/
|
|
181
|
+
prepareForOcr(image, scale, options = {}) {
|
|
182
|
+
let gray = image;
|
|
183
|
+
let allocatedGray = false;
|
|
184
|
+
if (image.channels() > 1) {
|
|
185
|
+
gray = this.toGrayscale(image);
|
|
186
|
+
allocatedGray = true;
|
|
187
|
+
}
|
|
188
|
+
const auto = this.ocrPrepOptions(gray, options);
|
|
189
|
+
const usedScale = scale ?? auto.scale;
|
|
190
|
+
const threshold = options.threshold ?? auto.threshold;
|
|
191
|
+
const scaled = new cv.Mat();
|
|
192
|
+
cv.resize(gray, scaled, new cv.Size(Math.max(1, gray.cols * usedScale), Math.max(1, gray.rows * usedScale)), 0, 0, cv.INTER_CUBIC);
|
|
193
|
+
if (allocatedGray)
|
|
194
|
+
gray.delete();
|
|
195
|
+
const padded = new cv.Mat();
|
|
196
|
+
cv.copyMakeBorder(scaled, padded, 8, 8, 8, 8, cv.BORDER_CONSTANT, new cv.Scalar(255, 255, 255, 255));
|
|
197
|
+
scaled.delete();
|
|
198
|
+
const binary = new cv.Mat();
|
|
199
|
+
if (typeof threshold === 'number') {
|
|
200
|
+
cv.threshold(padded, binary, threshold, 255, cv.THRESH_BINARY);
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
cv.threshold(padded, binary, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU);
|
|
204
|
+
}
|
|
205
|
+
padded.delete();
|
|
206
|
+
return binary;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Draw labeled rectangles on a clone of the screenshot (RGBA or gray).
|
|
210
|
+
*/
|
|
211
|
+
annotateRects(image, boxes) {
|
|
212
|
+
const out = image.clone();
|
|
213
|
+
for (const box of boxes) {
|
|
214
|
+
if (!box.rect.width || !box.rect.height)
|
|
215
|
+
continue;
|
|
216
|
+
const [r, g, b, a] = box.color ?? [75, 200, 120, 255];
|
|
217
|
+
const color = new cv.Scalar(r, g, b, a);
|
|
218
|
+
cv.rectangle(out, new cv.Point(Math.round(box.rect.x), Math.round(box.rect.y)), new cv.Point(Math.round(box.rect.x + box.rect.width), Math.round(box.rect.y + box.rect.height)), color, 2);
|
|
219
|
+
if (!box.label)
|
|
220
|
+
continue;
|
|
221
|
+
cv.putText(out, box.label, new cv.Point(Math.round(box.rect.x) + 2, Math.max(14, Math.round(box.rect.y) - 4)), cv.FONT_HERSHEY_SIMPLEX, 0.45, color, 1);
|
|
222
|
+
}
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Align two images using feature detection and homography
|
|
227
|
+
* This corrects for rotation, skew, and perspective differences
|
|
228
|
+
*/
|
|
229
|
+
alignImages(filledForm, blankForm) {
|
|
230
|
+
if (filledForm.rows === blankForm.rows && filledForm.cols === blankForm.cols) {
|
|
231
|
+
return filledForm.clone();
|
|
232
|
+
}
|
|
233
|
+
if (typeof cv.SIFT !== 'function') {
|
|
234
|
+
throw new Error('Filled and blank screenshots differ in size, and this OpenCV build cannot align them. Capture both at the same resolution.');
|
|
235
|
+
}
|
|
236
|
+
// Detect SIFT features
|
|
237
|
+
const sift = new cv.SIFT();
|
|
238
|
+
const filledKeypoints = new cv.KeyPointVector();
|
|
239
|
+
const filledDescriptors = new cv.Mat();
|
|
240
|
+
sift.detectAndCompute(filledForm, new cv.Mat(), filledKeypoints, filledDescriptors);
|
|
241
|
+
const blankKeypoints = new cv.KeyPointVector();
|
|
242
|
+
const blankDescriptors = new cv.Mat();
|
|
243
|
+
sift.detectAndCompute(blankForm, new cv.Mat(), blankKeypoints, blankDescriptors);
|
|
244
|
+
// Match features using FLANN-based matcher
|
|
245
|
+
const matcher = new cv.BFMatcher(cv.NORM_L2, false);
|
|
246
|
+
const matches = new cv.DMatchVectorVector();
|
|
247
|
+
matcher.knnMatch(filledDescriptors, blankDescriptors, matches, 2);
|
|
248
|
+
// Filter matches using Lowe's ratio test
|
|
249
|
+
const goodMatches = [];
|
|
250
|
+
const ratioThresh = 0.7;
|
|
251
|
+
for (let i = 0; i < matches.size(); i++) {
|
|
252
|
+
const match = matches.get(i);
|
|
253
|
+
if (match.size() >= 2) {
|
|
254
|
+
const m1 = match.get(0);
|
|
255
|
+
const m2 = match.get(1);
|
|
256
|
+
if (m1.distance < ratioThresh * m2.distance) {
|
|
257
|
+
goodMatches.push(m1);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
// Extract point correspondences
|
|
262
|
+
const filledPoints = [];
|
|
263
|
+
const blankPoints = [];
|
|
264
|
+
for (const match of goodMatches) {
|
|
265
|
+
const filledPt = filledKeypoints.get(match.queryIdx).pt;
|
|
266
|
+
const blankPt = blankKeypoints.get(match.trainIdx).pt;
|
|
267
|
+
filledPoints.push(filledPt.x, filledPt.y);
|
|
268
|
+
blankPoints.push(blankPt.x, blankPt.y);
|
|
269
|
+
}
|
|
270
|
+
// Find homography
|
|
271
|
+
const filledMat = cv.matFromArray(goodMatches.length, 1, cv.CV_32FC2, filledPoints);
|
|
272
|
+
const blankMat = cv.matFromArray(goodMatches.length, 1, cv.CV_32FC2, blankPoints);
|
|
273
|
+
const homography = cv.findHomography(filledMat, blankMat, cv.RANSAC, 5.0);
|
|
274
|
+
// Warp the filled form to align with blank
|
|
275
|
+
const aligned = new cv.Mat();
|
|
276
|
+
cv.warpPerspective(filledForm, aligned, homography, new cv.Size(blankForm.cols, blankForm.rows));
|
|
277
|
+
// Clean up
|
|
278
|
+
filledKeypoints.delete();
|
|
279
|
+
filledDescriptors.delete();
|
|
280
|
+
blankKeypoints.delete();
|
|
281
|
+
blankDescriptors.delete();
|
|
282
|
+
matches.delete();
|
|
283
|
+
filledMat.delete();
|
|
284
|
+
blankMat.delete();
|
|
285
|
+
homography.delete();
|
|
286
|
+
return aligned;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Convert a cv.Mat to a PNG buffer
|
|
290
|
+
*/
|
|
291
|
+
matToBuffer(mat) {
|
|
292
|
+
const width = mat.cols;
|
|
293
|
+
const height = mat.rows;
|
|
294
|
+
const png = new PNG({ width, height });
|
|
295
|
+
const src = mat.data;
|
|
296
|
+
const pixels = width * height;
|
|
297
|
+
if (src.length >= pixels * 4) {
|
|
298
|
+
png.data.set(src.subarray(0, pixels * 4));
|
|
299
|
+
}
|
|
300
|
+
else if (src.length >= pixels * 3) {
|
|
301
|
+
for (let i = 0; i < pixels; i++) {
|
|
302
|
+
png.data[i * 4] = src[i * 3] ?? 0;
|
|
303
|
+
png.data[i * 4 + 1] = src[i * 3 + 1] ?? 0;
|
|
304
|
+
png.data[i * 4 + 2] = src[i * 3 + 2] ?? 0;
|
|
305
|
+
png.data[i * 4 + 3] = 255;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
for (let i = 0; i < pixels; i++) {
|
|
310
|
+
const value = src[i] ?? 0;
|
|
311
|
+
png.data[i * 4] = value;
|
|
312
|
+
png.data[i * 4 + 1] = value;
|
|
313
|
+
png.data[i * 4 + 2] = value;
|
|
314
|
+
png.data[i * 4 + 3] = 255;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return PNG.sync.write(png);
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Use pixelmatch for fast pixel-level comparison
|
|
321
|
+
* Returns the number of mismatched pixels
|
|
322
|
+
*/
|
|
323
|
+
pixelMatch(img1, img2, options = {}) {
|
|
324
|
+
const png1 = PNG.sync.read(img1);
|
|
325
|
+
const png2 = PNG.sync.read(img2);
|
|
326
|
+
if (png1.width !== png2.width || png1.height !== png2.height) {
|
|
327
|
+
throw new Error('Images must have the same dimensions');
|
|
328
|
+
}
|
|
329
|
+
const diff = new PNG({ width: png1.width, height: png1.height });
|
|
330
|
+
const mismatchedPixels = pixelmatch(png1.data, png2.data, diff.data, png1.width, png1.height, {
|
|
331
|
+
threshold: options.threshold ?? 0.1,
|
|
332
|
+
includeAA: options.includeAA ?? false,
|
|
333
|
+
});
|
|
334
|
+
return {
|
|
335
|
+
mismatchedPixels,
|
|
336
|
+
diffBuffer: PNG.sync.write(diff),
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
//# sourceMappingURL=vision.js.map
|