Documentation
¶
Overview ¶
Package imageutil hosts the pixel-driven targeting primitives: find_text (OCR via Tesseract), find_image (template match — gocv when the gocv build tag is set, pure-Go normalized cross-correlation otherwise), and find_color (pure-Go pixel and blob search).
Build matrix:
default build pure-Go template match (multi-scale disabled) -tags gocv gocv-based multi-scale template match
Both modes are tested by CI. The default is pure-Go so the binary builds on machines without OpenCV; gocv is opt-in.
Index ¶
- func DownscaleNearest(src image.Image, maxEdge int) image.Image
- func FindColor(ctx context.Context, region contract.Bounds, img *image.RGBA, ...) (contract.FindResult, error)
- func FindImage(ctx context.Context, region contract.Bounds, img *image.RGBA, ...) (contract.FindResult, error)
- func FindText(ctx context.Context, batch *BatchContext, region contract.Bounds, ...) (contract.FindResult, error)
- func ProbeOCRTesseract() contract.BackendStatus
- func ProbeTemplateMatch() contract.BackendStatus
- func SaveJPEG(path string, img image.Image, quality int) error
- func SavePNG(path string, img image.Image) error
- type BatchContext
- type FindColorRequest
- type FindImageRequest
- type FindTextRequest
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FindColor ¶
func FindColor(ctx context.Context, region contract.Bounds, img *image.RGBA, req FindColorRequest) (contract.FindResult, error)
FindColor either samples one pixel (Point mode) or searches a region for contiguous blobs of pixels within tolerance of the target color. Returned bounds are in screen coordinates.
func FindImage ¶
func FindImage(ctx context.Context, region contract.Bounds, img *image.RGBA, req FindImageRequest) (contract.FindResult, error)
FindImage performs template matching against the captured screen image. The actual backend (gocv vs pure-Go) is selected at build time. Both implementations honor the same FindImageRequest shape.
func FindText ¶
func FindText(ctx context.Context, batch *BatchContext, region contract.Bounds, img *image.RGBA, req FindTextRequest) (contract.FindResult, error)
FindText runs Tesseract over the requested region and returns candidates whose recognized text matches the query. Sort order: confidence desc, then top-to-bottom, left-to-right.
Caching: when batch is non-nil, the OCR result for a given region + pixel hash is cached for the duration of the batch.
Errors:
- DEPENDENCY_UNAVAILABLE (exit 4): tesseract binary missing.
- TARGET_NOT_FOUND (exit 5): zero candidates matched the query.
func ProbeOCRTesseract ¶
func ProbeOCRTesseract() contract.BackendStatus
ProbeOCRTesseract reports tesseract availability for the doctor table. Required:false — find_text fails with DEPENDENCY_UNAVAILABLE when this is the only missing backend.
Details surfaced (when available):
- binary: absolute path
- version: first line of `tesseract --version` (e.g. "tesseract 5.3.0")
- data_dir: TESSDATA_PREFIX or compiled-in path
- langs: list of installed language codes
- supported_psm: "0..13" (inclusive ints accepted by --psm)
- supported_oem: "0..3" (inclusive ints accepted by --oem)
PSM/OEM ranges are documented constants from upstream Tesseract and are stable across 4.x/5.x; we do not probe them at runtime.
func ProbeTemplateMatch ¶
func ProbeTemplateMatch() contract.BackendStatus
ProbeTemplateMatch reports which template-match backend is compiled in. The implementation is supplied by find_image_purego.go or find_image_gocv.go depending on build tags.
Types ¶
type BatchContext ¶
type BatchContext struct {
// contains filtered or unexported fields
}
BatchContext carries per-batch state shared across find_* primitives inside a single computer_actions batch. Today it caches OCR results keyed by region + pixel-hash so repeated find_text calls in the same batch do not re-shell-out to tesseract. Lifetime is exactly one batch; callers MUST allocate a fresh BatchContext per pipeline.Run.
The zero value is NOT safe — use NewBatchContext.
func NewBatchContext ¶
func NewBatchContext() *BatchContext
NewBatchContext returns an empty BatchContext ready for use.
func (*BatchContext) OCRCalls ¶
func (b *BatchContext) OCRCalls() int
OCRCalls reports how many times the Tesseract subprocess was actually invoked through this batch context. Exposed for tests (batch-cache hit counter).
func (*BatchContext) SetSlot ¶
func (b *BatchContext) SetSlot(name string, c contract.FindCandidate)
SetSlot stores a named find candidate so later actions in the batch can target it via target_slot.
func (*BatchContext) Slot ¶
func (b *BatchContext) Slot(name string) (contract.FindCandidate, bool)
Slot retrieves a previously stored named candidate.
type FindColorRequest ¶
type FindColorRequest struct {
Point *contract.Point `json:"point,omitempty"`
Color string `json:"color,omitempty"`
Region contract.Bounds `json:"region,omitzero"`
Tolerance int `json:"tolerance,omitempty"`
MinArea int `json:"min_area,omitempty"`
}
FindColorRequest accepts either a Point sample mode or a Color blob search mode. The two modes are mutually exclusive; if both are set, Point wins because it is the more specific query.
type FindImageRequest ¶
type FindImageRequest struct {
TemplatePath string `json:"template_path"`
Region contract.Bounds `json:"region,omitzero"`
Threshold float64 `json:"threshold,omitempty"`
Scales []float64 `json:"scales,omitempty"`
}
FindImageRequest carries inputs for find_image template matching. Threshold defaults to 0.9 if zero. Scales defaults to []float{1.0}; the gocv backend honors multi-scale, the pure-Go backend uses scale 1.0 only and logs a notice in Extra.
type FindTextRequest ¶
type FindTextRequest struct {
Region contract.Bounds `json:"region,omitzero"`
Query string `json:"query"`
Lang string `json:"lang,omitempty"`
CaseSensitive bool `json:"case_sensitive,omitempty"`
MinConfidence float64 `json:"min_confidence,omitempty"`
Regex bool `json:"regex,omitempty"`
Preprocess string `json:"preprocess,omitempty"`
PSM int `json:"psm,omitempty"`
OEM int `json:"oem,omitempty"`
}
FindTextRequest carries the inputs for find_text. Region is in screen coordinates; an empty region defaults to the full screen at the call site (callers that want focused-window default supply the bounds).
Preprocess selects the pixel-pipeline applied before Tesseract:
- "" or "auto" — sample region mean luminance; if below darkThemeLuminanceThreshold invert the image (white text on dark becomes black on white, which Tesseract handles natively).
- "invert" — unconditionally invert RGB channels.
- "binarize" — Otsu thresholding to pure black/white.
- "none" — pass pixels through unchanged.
PSM and OEM are Tesseract tunables: any non-zero value is forwarded as `--psm <n>` / `--oem <n>`. Zero (the default) omits the flag so Tesseract uses its own defaults. This keeps the OCR output bit-for-bit identical for callers that don't opt in.