finished item finder

This commit is contained in:
Boki 2026-02-10 19:10:59 -05:00
parent 1246884be9
commit 930e00c9cc
7 changed files with 450 additions and 37 deletions

View file

@ -1,5 +1,5 @@
import { logger } from '../util/logger.js';
import type { OcrDaemon } from './OcrDaemon.js';
import type { OcrDaemon, GridItem, GridMatch } from './OcrDaemon.js';
import type { Region } from '../types.js';
// ── Grid type definitions ───────────────────────────────────────────────────
@ -20,6 +20,8 @@ export interface CellCoord {
export interface ScanResult {
layout: GridLayout;
occupied: CellCoord[];
items: GridItem[];
matches?: GridMatch[];
}
// ── Calibrated grid layouts (2560×1440) ─────────────────────────────────────
@ -89,20 +91,20 @@ export class GridReader {
/**
* Scan a named grid layout for occupied cells.
*/
async scan(layoutName: string, threshold?: number): Promise<ScanResult> {
async scan(layoutName: string, threshold?: number, targetRow?: number, targetCol?: number): Promise<ScanResult> {
const layout = GRID_LAYOUTS[layoutName];
if (!layout) throw new Error(`Unknown grid layout: ${layoutName}`);
const t = performance.now();
const occupied = await this.getOccupiedCells(layout, threshold);
const { occupied, items, matches } = await this.getOccupiedCells(layout, threshold, targetRow, targetCol);
const ms = (performance.now() - t).toFixed(0);
logger.info(
{ layoutName, cols: layout.cols, rows: layout.rows, occupied: occupied.length, ms },
{ layoutName, cols: layout.cols, rows: layout.rows, occupied: occupied.length, items: items.length, matches: matches?.length, ms },
'Grid scan complete',
);
return { layout, occupied };
return { layout, occupied, items, matches };
}
/** Get the screen-space center of a grid cell */
@ -115,20 +117,22 @@ export class GridReader {
};
}
/** Scan the grid and return which cells are occupied */
async getOccupiedCells(layout: GridLayout, threshold?: number): Promise<CellCoord[]> {
/** Scan the grid and return which cells are occupied and detected items */
async getOccupiedCells(layout: GridLayout, threshold?: number, targetRow?: number, targetCol?: number): Promise<{ occupied: CellCoord[]; items: GridItem[]; matches?: GridMatch[] }> {
const t = performance.now();
const cells = await this.daemon.gridScan(
const result = await this.daemon.gridScan(
layout.region,
layout.cols,
layout.rows,
threshold,
targetRow,
targetCol,
);
const occupied: CellCoord[] = [];
for (let row = 0; row < cells.length; row++) {
for (let col = 0; col < cells[row].length; col++) {
if (cells[row][col]) {
for (let row = 0; row < result.cells.length; row++) {
for (let col = 0; col < result.cells[row].length; col++) {
if (result.cells[row][col]) {
const center = this.getCellCenter(layout, row, col);
occupied.push({ row, col, x: center.x, y: center.y });
}
@ -137,10 +141,10 @@ export class GridReader {
const ms = (performance.now() - t).toFixed(0);
logger.info(
{ layout: `${layout.cols}x${layout.rows}`, occupied: occupied.length, ms },
{ layout: `${layout.cols}x${layout.rows}`, occupied: occupied.length, items: result.items.length, matches: result.matches?.length, ms },
'Grid scan complete',
);
return occupied;
return { occupied, items: result.items, matches: result.matches };
}
/** Get all cell centers in the grid */