added test cases
This commit is contained in:
parent
c1892230b7
commit
93e2234c4e
9 changed files with 320 additions and 141 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import { mkdir } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
import { logger } from '../util/logger.js';
|
||||
import { OcrDaemon, type OcrResponse, type OcrEngine, type OcrPreprocess, type DiffOcrResponse, type TemplateMatchResult } from './OcrDaemon.js';
|
||||
import { OcrDaemon, type OcrResponse, type OcrEngine, type OcrPreprocess, type DiffOcrParams, type DiffOcrResponse, type TemplateMatchResult } from './OcrDaemon.js';
|
||||
import { GridReader, type GridLayout, type CellCoord } from './GridReader.js';
|
||||
import type { Region } from '../types.js';
|
||||
|
||||
|
|
@ -9,11 +9,24 @@ function elapsed(start: number): string {
|
|||
return `${(performance.now() - start).toFixed(0)}ms`;
|
||||
}
|
||||
|
||||
export interface OcrSettings {
|
||||
engine: OcrEngine;
|
||||
screenPreprocess: OcrPreprocess;
|
||||
tooltipPreprocess: OcrPreprocess;
|
||||
tooltipParams: DiffOcrParams;
|
||||
saveDebugImages: boolean;
|
||||
}
|
||||
|
||||
export class ScreenReader {
|
||||
private daemon = new OcrDaemon();
|
||||
readonly grid = new GridReader(this.daemon);
|
||||
debugOcrEngine: OcrEngine = 'tesseract';
|
||||
debugPreprocess: OcrPreprocess = 'bgsub';
|
||||
settings: OcrSettings = {
|
||||
engine: 'easyocr',
|
||||
screenPreprocess: 'none',
|
||||
tooltipPreprocess: 'bgsub',
|
||||
tooltipParams: {},
|
||||
saveDebugImages: true,
|
||||
};
|
||||
|
||||
// ── Screenshot capture ──────────────────────────────────────────────
|
||||
|
||||
|
|
@ -147,19 +160,23 @@ export class ScreenReader {
|
|||
fuzzy: boolean = false,
|
||||
): Promise<{ x: number; y: number } | null> {
|
||||
const t = performance.now();
|
||||
const result = await this.daemon.ocr();
|
||||
const { engine, screenPreprocess } = this.settings;
|
||||
const pp = screenPreprocess !== 'none' ? screenPreprocess : undefined;
|
||||
const result = await this.daemon.ocr(undefined, engine, pp);
|
||||
const pos = this.findWordInOcrResult(result, searchText, fuzzy);
|
||||
|
||||
if (pos) {
|
||||
logger.info({ searchText, x: pos.x, y: pos.y, totalMs: elapsed(t) }, 'Found text on screen');
|
||||
logger.info({ searchText, engine, x: pos.x, y: pos.y, totalMs: elapsed(t) }, 'Found text on screen');
|
||||
} else {
|
||||
logger.info({ searchText, totalMs: elapsed(t) }, 'Text not found on screen');
|
||||
logger.info({ searchText, engine, totalMs: elapsed(t) }, 'Text not found on screen');
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
async readFullScreen(): Promise<string> {
|
||||
const result = await this.daemon.ocr();
|
||||
const { engine, screenPreprocess } = this.settings;
|
||||
const pp = screenPreprocess !== 'none' ? screenPreprocess : undefined;
|
||||
const result = await this.daemon.ocr(undefined, engine, pp);
|
||||
return result.text;
|
||||
}
|
||||
|
||||
|
|
@ -170,7 +187,9 @@ export class ScreenReader {
|
|||
searchText: string,
|
||||
): Promise<{ x: number; y: number } | null> {
|
||||
const t = performance.now();
|
||||
const result = await this.daemon.ocr(region);
|
||||
const { engine, screenPreprocess } = this.settings;
|
||||
const pp = screenPreprocess !== 'none' ? screenPreprocess : undefined;
|
||||
const result = await this.daemon.ocr(region, engine, pp);
|
||||
const pos = this.findWordInOcrResult(result, searchText);
|
||||
|
||||
if (pos) {
|
||||
|
|
@ -185,7 +204,9 @@ export class ScreenReader {
|
|||
}
|
||||
|
||||
async readRegionText(region: Region): Promise<string> {
|
||||
const result = await this.daemon.ocr(region);
|
||||
const { engine, screenPreprocess } = this.settings;
|
||||
const pp = screenPreprocess !== 'none' ? screenPreprocess : undefined;
|
||||
const result = await this.daemon.ocr(region, engine, pp);
|
||||
return result.text;
|
||||
}
|
||||
|
||||
|
|
@ -201,7 +222,9 @@ export class ScreenReader {
|
|||
}
|
||||
|
||||
async diffOcr(savePath?: string, region?: Region): Promise<DiffOcrResponse> {
|
||||
return this.daemon.diffOcr(savePath, region);
|
||||
const { engine, tooltipPreprocess, tooltipParams } = this.settings;
|
||||
const pp = tooltipPreprocess !== 'none' ? tooltipPreprocess : undefined;
|
||||
return this.daemon.diffOcr(savePath, region, engine, pp, tooltipParams);
|
||||
}
|
||||
|
||||
// ── Template matching ──────────────────────────────────────────────
|
||||
|
|
@ -238,43 +261,6 @@ export class ScreenReader {
|
|||
logger.info({ path, region }, 'Region screenshot saved');
|
||||
}
|
||||
|
||||
// ── Debug OCR (alternative engines) ─────────────────────────────────
|
||||
|
||||
async debugDiffOcr(savePath?: string, region?: Region): Promise<DiffOcrResponse> {
|
||||
const t = performance.now();
|
||||
const result = await this.daemon.diffOcr(savePath, region, this.debugOcrEngine, this.debugPreprocess);
|
||||
logger.info({ engine: this.debugOcrEngine, preprocess: this.debugPreprocess, ms: elapsed(t) }, 'debugDiffOcr');
|
||||
return result;
|
||||
}
|
||||
|
||||
async debugOcr(region?: Region): Promise<OcrResponse> {
|
||||
const t = performance.now();
|
||||
const result = await this.daemon.ocr(region, this.debugOcrEngine, this.debugPreprocess);
|
||||
logger.info({ engine: this.debugOcrEngine, preprocess: this.debugPreprocess, ms: elapsed(t) }, 'debugOcr');
|
||||
return result;
|
||||
}
|
||||
|
||||
async debugReadFullScreen(): Promise<string> {
|
||||
const result = await this.daemon.ocr(undefined, this.debugOcrEngine, this.debugPreprocess);
|
||||
return result.text;
|
||||
}
|
||||
|
||||
async debugFindTextOnScreen(
|
||||
searchText: string,
|
||||
fuzzy: boolean = false,
|
||||
): Promise<{ x: number; y: number } | null> {
|
||||
const t = performance.now();
|
||||
const result = await this.daemon.ocr(undefined, this.debugOcrEngine, this.debugPreprocess);
|
||||
const pos = this.findWordInOcrResult(result, searchText, fuzzy);
|
||||
|
||||
if (pos) {
|
||||
logger.info({ searchText, engine: this.debugOcrEngine, preprocess: this.debugPreprocess, x: pos.x, y: pos.y, totalMs: elapsed(t) }, 'debugFindText found');
|
||||
} else {
|
||||
logger.info({ searchText, engine: this.debugOcrEngine, preprocess: this.debugPreprocess, totalMs: elapsed(t) }, 'debugFindText not found');
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
// ── Lifecycle ───────────────────────────────────────────────────────
|
||||
|
||||
async dispose(): Promise<void> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue