feat: find text with regex

This commit is contained in:
lilong.129
2023-05-02 10:55:02 +08:00
parent 0413ff62d1
commit 042e3ba8ba
2 changed files with 26 additions and 26 deletions

View File

@@ -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

View File

@@ -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 {