From 042e3ba8ba29107db103dfe84cdc17278899ecd5 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Tue, 2 May 2023 10:55:02 +0800 Subject: [PATCH] feat: find text with regex --- hrp/pkg/uixt/action.go | 17 +++++++++++------ hrp/pkg/uixt/ocr_vedem.go | 35 +++++++++++++++-------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/hrp/pkg/uixt/action.go b/hrp/pkg/uixt/action.go index dfe27ec5..e17db95f 100644 --- a/hrp/pkg/uixt/action.go +++ b/hrp/pkg/uixt/action.go @@ -85,13 +85,9 @@ type ActionOptions struct { Scope []float64 `json:"scope,omitempty" yaml:"scope,omitempty"` // [x1, y1, x2, y2] in percentage of the screen AbsScope []int `json:"abs_scope,omitempty" yaml:"abs_scope,omitempty"` // [x1, y1, x2, y2] in absolute pixels + Regex bool `json:"regex,omitempty" yaml:"regex,omitempty"` // use regex to match text Offset []int `json:"offset,omitempty" yaml:"offset,omitempty"` // used to tap offset of point - Index int `json:"index,omitempty" yaml:"index,omitempty"` // index of the target element, should start from 1 - - // element related - Text string `json:"text,omitempty" yaml:"text,omitempty"` - ID string `json:"id,omitempty" yaml:"id,omitempty"` - Description string `json:"description,omitempty" yaml:"description,omitempty"` + Index int `json:"index,omitempty" yaml:"index,omitempty"` // index of the target element // set custiom options such as textview, id, description Custom map[string]interface{} `json:"custom,omitempty" yaml:"custom,omitempty"` @@ -154,6 +150,9 @@ func (o *ActionOptions) Options() []ActionOption { if len(o.Offset) == 2 { options = append(options, WithOffset(o.Offset[0], o.Offset[1])) } + if o.Regex { + options = append(options, WithRegex(true)) + } // custom options if o.Custom != nil { @@ -307,6 +306,12 @@ func WithOffset(offsetX, offsetY int) ActionOption { } } +func WithRegex(regex bool) ActionOption { + return func(o *ActionOptions) { + o.Regex = regex + } +} + func WithFrequency(frequency int) ActionOption { return func(o *ActionOptions) { o.Frequency = frequency diff --git a/hrp/pkg/uixt/ocr_vedem.go b/hrp/pkg/uixt/ocr_vedem.go index b2f0ccc0..92888ef3 100644 --- a/hrp/pkg/uixt/ocr_vedem.go +++ b/hrp/pkg/uixt/ocr_vedem.go @@ -7,7 +7,7 @@ import ( "io/ioutil" "mime/multipart" "net/http" - "strings" + "regexp" "time" "github.com/pkg/errors" @@ -68,22 +68,19 @@ func (t OCRTexts) FindText(text string, options ...ActionOption) ( } } - // not contains text - if !strings.Contains(ocrText.Text, text) { - continue + if actionOptions.Regex { + // regex on, check if match regex + if !regexp.MustCompile(text).MatchString(ocrText.Text) { + continue + } + } else { + // regex off, check if match exactly + if ocrText.Text != text { + continue + } } rects = append(rects, rect) - - // contains text while not match exactly - if ocrText.Text != text { - continue - } - - // match exactly, and not specify index, return the first one - if actionOptions.Index == 0 { - return getRectangleCenterPoint(rect), nil - } } if len(rects) == 0 { @@ -93,15 +90,12 @@ func (t OCRTexts) FindText(text string, options ...ActionOption) ( // get index idx := actionOptions.Index - if idx > 0 { - // NOTICE: index start from 1 - idx = idx - 1 - } else if idx < 0 { + if idx < 0 { idx = len(rects) + idx } // index out of range - if idx >= len(rects) { + if idx >= len(rects) || idx < 0 { return PointF{}, errors.Wrap(code.OCRTextNotFoundError, fmt.Sprintf("text %s found %d, index %d out of range", text, len(rects), idx)) } @@ -109,7 +103,8 @@ func (t OCRTexts) FindText(text string, options ...ActionOption) ( return getRectangleCenterPoint(rects[idx]), nil } -func (t OCRTexts) FindTexts(texts []string, options ...ActionOption) (points []PointF, err error) { +func (t OCRTexts) FindTexts(texts []string, options ...ActionOption) ( + points []PointF, err error) { for _, text := range texts { point, err := t.FindText(text, options...) if err != nil {