refactor: FindText(s) returns OCRText(s)

This commit is contained in:
lilong.129
2023-05-02 11:49:37 +08:00
parent 042e3ba8ba
commit b5b84aed0a
4 changed files with 27 additions and 20 deletions
+2 -1
View File
@@ -47,7 +47,8 @@ func TestIOSDemo(t *testing.T) {
continue continue
} }
err = driverExt.TapAbsXY(points[1].X, points[1].Y) point := points[1].Center()
err = driverExt.TapAbsXY(point.X, point.Y)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
+21 -16
View File
@@ -39,6 +39,10 @@ type OCRText struct {
Rect image.Rectangle Rect image.Rectangle
} }
func (t OCRText) Center() PointF {
return getRectangleCenterPoint(t.Rect)
}
type OCRTexts []OCRText type OCRTexts []OCRText
func (t OCRTexts) texts() (texts []string) { func (t OCRTexts) texts() (texts []string) {
@@ -49,11 +53,11 @@ func (t OCRTexts) texts() (texts []string) {
} }
func (t OCRTexts) FindText(text string, options ...ActionOption) ( func (t OCRTexts) FindText(text string, options ...ActionOption) (
point PointF, err error) { result OCRText, err error) {
actionOptions := NewActionOptions(options...) actionOptions := NewActionOptions(options...)
var rects []image.Rectangle var results []OCRText
for _, ocrText := range t { for _, ocrText := range t {
rect := ocrText.Rect rect := ocrText.Rect
@@ -80,44 +84,44 @@ func (t OCRTexts) FindText(text string, options ...ActionOption) (
} }
} }
rects = append(rects, rect) results = append(results, ocrText)
} }
if len(rects) == 0 { if len(results) == 0 {
return PointF{}, errors.Wrap(code.OCRTextNotFoundError, return OCRText{}, errors.Wrap(code.OCRTextNotFoundError,
fmt.Sprintf("text %s not found in %v", text, t.texts())) fmt.Sprintf("text %s not found in %v", text, t.texts()))
} }
// get index // get index
idx := actionOptions.Index idx := actionOptions.Index
if idx < 0 { if idx < 0 {
idx = len(rects) + idx idx = len(results) + idx
} }
// index out of range // index out of range
if idx >= len(rects) || idx < 0 { if idx >= len(results) || idx < 0 {
return PointF{}, errors.Wrap(code.OCRTextNotFoundError, return OCRText{}, errors.Wrap(code.OCRTextNotFoundError,
fmt.Sprintf("text %s found %d, index %d out of range", text, len(rects), idx)) fmt.Sprintf("text %s found %d, index %d out of range", text, len(results), idx))
} }
return getRectangleCenterPoint(rects[idx]), nil return results[idx], nil
} }
func (t OCRTexts) FindTexts(texts []string, options ...ActionOption) ( func (t OCRTexts) FindTexts(texts []string, options ...ActionOption) (
points []PointF, err error) { results OCRTexts, err error) {
for _, text := range texts { for _, text := range texts {
point, err := t.FindText(text, options...) ocrText, err := t.FindText(text, options...)
if err != nil { if err != nil {
continue continue
} }
points = append(points, point) results = append(results, ocrText)
} }
if len(points) != len(texts) { if len(results) != len(texts) {
return nil, errors.Wrap(code.OCRTextNotFoundError, return nil, errors.Wrap(code.OCRTextNotFoundError,
fmt.Sprintf("texts %s not found in %v", texts, t.texts())) fmt.Sprintf("texts %s not found in %v", texts, t.texts()))
} }
return points, nil return results, nil
} }
func newVEDEMOCRService() (*veDEMOCRService, error) { func newVEDEMOCRService() (*veDEMOCRService, error) {
@@ -300,11 +304,12 @@ func (dExt *DriverExt) FindScreenTextByOCR(text string, options ...ActionOption)
if err != nil { if err != nil {
return return
} }
point, err = ocrTexts.FindText(text, dExt.ParseActionOptions(options...)...) result, err := ocrTexts.FindText(text, dExt.ParseActionOptions(options...)...)
if err != nil { if err != nil {
log.Warn().Msgf("FindText failed: %s", err.Error()) log.Warn().Msgf("FindText failed: %s", err.Error())
return return
} }
point = result.Center()
log.Info().Str("text", text). log.Info().Str("text", text).
Interface("point", point).Msgf("FindScreenTextByOCR success") Interface("point", point).Msgf("FindScreenTextByOCR success")
+1 -1
View File
@@ -143,7 +143,7 @@ func (dExt *DriverExt) swipeToTapTexts(texts []string, options ...ActionOption)
if err != nil { if err != nil {
return err return err
} }
point = points[0] // FIXME point = points[0].Center() // FIXME
return nil return nil
} }
foundTextAction := func(d *DriverExt) error { foundTextAction := func(d *DriverExt) error {
+3 -2
View File
@@ -79,7 +79,7 @@ func (l *LiveCrawler) checkLiveVideo(texts OCRTexts) (enterPoint PointF, yes boo
// 预览流入口 // 预览流入口
points, err := texts.FindTexts([]string{"点击进入直播间", "直播中"}) points, err := texts.FindTexts([]string{"点击进入直播间", "直播中"})
if err == nil { if err == nil {
return points[0], true return points[0].Center(), true
} }
// TODO: 头像入口 // TODO: 头像入口
@@ -271,7 +271,8 @@ func (dExt *DriverExt) autoPopupHandler(texts OCRTexts) error {
points, err := texts.FindTexts([]string{"确定", "取消"}) points, err := texts.FindTexts([]string{"确定", "取消"})
if err == nil { if err == nil {
log.Warn().Msg("text popup found") log.Warn().Msg("text popup found")
if err := dExt.TapAbsXY(points[1].X, points[1].Y); err != nil { point := points[1].Center()
if err := dExt.TapAbsXY(point.X, point.Y); err != nil {
log.Error().Err(err).Msg("tap popup failed") log.Error().Err(err).Msg("tap popup failed")
return err return err
} }