@aspiresys/visor 1.0.1 → 1.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/app.js CHANGED
@@ -63,9 +63,7 @@ async function openApp(target) {
63
63
  if (!resolved) {
64
64
  throw new Error(`App not found: ${target}`);
65
65
  }
66
- const firstPath = resolved
67
- .split("\n")[0]
68
- .trim();
66
+ const firstPath = resolved.split("\n")[0].trim();
69
67
  (0, logger_1.log)(`[APP] Resolved path: ${firstPath}`);
70
68
  (0, child_process_1.exec)(`start "" "${firstPath}"`);
71
69
  await new Promise(r => setTimeout(r, 5000));
package/dist/index.js CHANGED
@@ -162,7 +162,7 @@ class Visor {
162
162
  */
163
163
  async find(image, confidence = 0.8) {
164
164
  const screen = await (0, screen_1.captureScreen)();
165
- const template = (0, matcher_1.loadTemplate)((0, path_1.resolveImagePath)(image));
165
+ const template = await (0, matcher_1.loadTemplate)((0, path_1.resolveImagePath)(image));
166
166
  try {
167
167
  return (0, matcher_1.matchTemplate)(screen, template, confidence);
168
168
  }
@@ -217,7 +217,7 @@ class Visor {
217
217
  */
218
218
  async findAll(image, confidence = 0.8) {
219
219
  const screen = await (0, screen_1.captureScreen)();
220
- const template = (0, matcher_1.loadTemplate)((0, path_1.resolveImagePath)(image));
220
+ const template = await (0, matcher_1.loadTemplate)((0, path_1.resolveImagePath)(image));
221
221
  try {
222
222
  return (0, matcher_1.findAllMatches)(screen, template, confidence);
223
223
  }
@@ -914,7 +914,7 @@ CONF:${m.confidence.toFixed(3)}
914
914
  const screen = await (0, screen_1.captureScreen)();
915
915
  try {
916
916
  for (const image of images) {
917
- const template = (0, matcher_1.loadTemplate)((0, path_1.resolveImagePath)(image));
917
+ const template = await (0, matcher_1.loadTemplate)((0, path_1.resolveImagePath)(image));
918
918
  try {
919
919
  const region = (0, matcher_1.matchTemplate)(screen, template, confidence);
920
920
  if (region) {
package/dist/matcher.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { Region } from "./types";
2
- export declare function loadTemplate(path: string): any;
2
+ export declare function loadTemplate(path: string): Promise<any>;
3
3
  export declare function matchTemplate(screen: any, template: any, confidence?: number): Region | null;
4
4
  export declare function findAllMatches(screen: any, template: any, confidence?: number): Region[];
package/dist/matcher.js CHANGED
@@ -9,30 +9,53 @@ exports.findAllMatches = findAllMatches;
9
9
  const fs_1 = __importDefault(require("fs"));
10
10
  const pngjs_1 = require("pngjs");
11
11
  const cv = require("@techstark/opencv-js");
12
- function loadTemplate(path) {
12
+ const sharp_1 = __importDefault(require("sharp"));
13
+ const logger_1 = require("./logger");
14
+ async function loadTemplate(path) {
13
15
  const buffer = fs_1.default.readFileSync(path);
14
- const png = pngjs_1.PNG.sync.read(buffer);
16
+ const processed = await (0, sharp_1.default)(buffer).normalize().sharpen().png().toBuffer();
17
+ const png = pngjs_1.PNG.sync.read(processed);
15
18
  return cv.matFromImageData({
16
19
  data: png.data,
17
20
  width: png.width,
18
21
  height: png.height
19
22
  });
20
23
  }
24
+ function resizeTemplate(template, scale) {
25
+ const resized = new cv.Mat();
26
+ const newWidth = Math.max(1, Math.floor(template.cols * scale));
27
+ const newHeight = Math.max(1, Math.floor(template.rows * scale));
28
+ cv.resize(template, resized, new cv.Size(newWidth, newHeight), 0, 0, scale < 1 ? cv.INTER_AREA : cv.INTER_CUBIC);
29
+ return resized;
30
+ }
21
31
  function matchTemplate(screen, template, confidence = 0.8) {
22
- const result = new cv.Mat();
23
- cv.matchTemplate(screen, template, result, cv.TM_CCOEFF_NORMED);
24
- const { maxLoc, maxVal } = cv.minMaxLoc(result);
25
- result.delete();
26
- if (maxVal < confidence) {
27
- return null;
32
+ const scales = Array.from({ length: 11 }, (_, i) => Number((0.5 + i * 0.1).toFixed(2)));
33
+ let bestMatch = null;
34
+ for (const scale of scales) {
35
+ const resizedTemplate = resizeTemplate(template, scale);
36
+ if (resizedTemplate.cols > screen.cols || resizedTemplate.rows > screen.rows) {
37
+ resizedTemplate.delete();
38
+ continue;
39
+ }
40
+ const result = new cv.Mat();
41
+ cv.matchTemplate(screen, resizedTemplate, result, cv.TM_CCOEFF_NORMED);
42
+ const { maxLoc, maxVal } = cv.minMaxLoc(result);
43
+ (0, logger_1.log)(`[MATCH] Scale ${scale} -> ${maxVal}`);
44
+ result.delete();
45
+ if (maxVal >= confidence &&
46
+ (!bestMatch ||
47
+ maxVal > bestMatch.confidence)) {
48
+ bestMatch = {
49
+ x: maxLoc.x,
50
+ y: maxLoc.y,
51
+ width: resizedTemplate.cols,
52
+ height: resizedTemplate.rows,
53
+ confidence: maxVal
54
+ };
55
+ }
56
+ resizedTemplate.delete();
28
57
  }
29
- return {
30
- x: maxLoc.x,
31
- y: maxLoc.y,
32
- width: template.cols,
33
- height: template.rows,
34
- confidence: maxVal
35
- };
58
+ return bestMatch;
36
59
  }
37
60
  function findAllMatches(screen, template, confidence = 0.8) {
38
61
  const result = new cv.Mat();
package/dist/mouse.js CHANGED
@@ -7,10 +7,8 @@ const config_1 = require("./config");
7
7
  const logger_1 = require("./logger");
8
8
  async function moveToRegion(region) {
9
9
  const scaleFactor = config_1.visorConfig.scaleFactor;
10
- const centerX = Math.floor((region.x + region.width / 2)
11
- / scaleFactor);
12
- const centerY = Math.floor((region.y + region.height / 2)
13
- / scaleFactor);
10
+ const centerX = Math.floor((region.x + region.width / 2) / scaleFactor);
11
+ const centerY = Math.floor((region.y + region.height / 2) / scaleFactor);
14
12
  (0, logger_1.log)("[MOUSE] Moving mouse to:", centerX, centerY);
15
13
  await nut_js_1.mouse.move([
16
14
  new nut_js_1.Point(centerX, centerY)
package/dist/ocr.js CHANGED
@@ -65,12 +65,7 @@ async function extractTextFromRegion(region) {
65
65
  config_1.visorConfig.scaleFactor)
66
66
  });
67
67
  }
68
- const processed = await image
69
- .grayscale()
70
- .normalize()
71
- .sharpen()
72
- .png()
73
- .toBuffer();
68
+ const processed = await image.grayscale().normalize().sharpen().png().toBuffer();
74
69
  const worker = await getWorker();
75
70
  const result = await worker.recognize(processed, {}, {
76
71
  blocks: true,
package/dist/path.js CHANGED
@@ -7,8 +7,7 @@ exports.resolveImagePath = resolveImagePath;
7
7
  const path_1 = __importDefault(require("path"));
8
8
  const config_1 = require("./config");
9
9
  function resolveImagePath(image) {
10
- if (image.includes("/") ||
11
- image.includes("\\")) {
10
+ if (image.includes("/") || image.includes("\\")) {
12
11
  return image;
13
12
  }
14
13
  return path_1.default.join(config_1.visorConfig.imagePath, image);
package/dist/screen.js CHANGED
@@ -8,12 +8,12 @@ exports.saveScreenshot = saveScreenshot;
8
8
  const screenshot_desktop_1 = __importDefault(require("screenshot-desktop"));
9
9
  const pngjs_1 = require("pngjs");
10
10
  const fs_1 = __importDefault(require("fs"));
11
+ const sharp_1 = __importDefault(require("sharp"));
11
12
  const cv = require("@techstark/opencv-js");
12
13
  async function captureScreen() {
13
- const buffer = await (0, screenshot_desktop_1.default)({
14
- format: "png"
15
- });
16
- const png = pngjs_1.PNG.sync.read(buffer);
14
+ const buffer = await (0, screenshot_desktop_1.default)({ format: "png" });
15
+ const processed = await (0, sharp_1.default)(buffer).normalize().sharpen().png().toBuffer();
16
+ const png = pngjs_1.PNG.sync.read(processed);
17
17
  return cv.matFromImageData({
18
18
  data: png.data,
19
19
  width: png.width,
@@ -21,8 +21,6 @@ async function captureScreen() {
21
21
  });
22
22
  }
23
23
  async function saveScreenshot(path) {
24
- const img = await (0, screenshot_desktop_1.default)({
25
- format: "png"
26
- });
24
+ const img = await (0, screenshot_desktop_1.default)({ format: "png" });
27
25
  fs_1.default.writeFileSync(path, img);
28
26
  }
package/dist/text.js CHANGED
@@ -31,9 +31,7 @@ async function findText(text, region) {
31
31
  });
32
32
  }
33
33
  //console.log(words);
34
- const match = words.find(w => w.text
35
- .toLowerCase()
36
- .includes(text.toLowerCase()));
34
+ const match = words.find(w => w.text.toLowerCase().includes(text.toLowerCase()));
37
35
  if (!match) {
38
36
  return null;
39
37
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aspiresys/visor",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {