From b5b84aed0a470bd83185a454a647077d735ac284 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Tue, 2 May 2023 11:49:37 +0800 Subject: [PATCH] refactor: FindText(s) returns OCRText(s) --- hrp/pkg/uixt/demo/main_test.go | 3 ++- hrp/pkg/uixt/ocr_vedem.go | 37 +++++++++++++++++++--------------- hrp/pkg/uixt/swipe.go | 2 +- hrp/pkg/uixt/video_crawler.go | 5 +++-- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/hrp/pkg/uixt/demo/main_test.go b/hrp/pkg/uixt/demo/main_test.go index b2610e0e..c2c8c3dd 100644 --- a/hrp/pkg/uixt/demo/main_test.go +++ b/hrp/pkg/uixt/demo/main_test.go @@ -47,7 +47,8 @@ func TestIOSDemo(t *testing.T) { continue } - err = driverExt.TapAbsXY(points[1].X, points[1].Y) + point := points[1].Center() + err = driverExt.TapAbsXY(point.X, point.Y) if err != nil { t.Fatal(err) } diff --git a/hrp/pkg/uixt/ocr_vedem.go b/hrp/pkg/uixt/ocr_vedem.go index 92888ef3..9b7dd24a 100644 --- a/hrp/pkg/uixt/ocr_vedem.go +++ b/hrp/pkg/uixt/ocr_vedem.go @@ -39,6 +39,10 @@ type OCRText struct { Rect image.Rectangle } +func (t OCRText) Center() PointF { + return getRectangleCenterPoint(t.Rect) +} + type OCRTexts []OCRText func (t OCRTexts) texts() (texts []string) { @@ -49,11 +53,11 @@ func (t OCRTexts) texts() (texts []string) { } func (t OCRTexts) FindText(text string, options ...ActionOption) ( - point PointF, err error) { + result OCRText, err error) { actionOptions := NewActionOptions(options...) - var rects []image.Rectangle + var results []OCRText for _, ocrText := range t { 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 { - return PointF{}, errors.Wrap(code.OCRTextNotFoundError, + if len(results) == 0 { + return OCRText{}, errors.Wrap(code.OCRTextNotFoundError, fmt.Sprintf("text %s not found in %v", text, t.texts())) } // get index idx := actionOptions.Index if idx < 0 { - idx = len(rects) + idx + idx = len(results) + idx } // index out of range - 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)) + if idx >= len(results) || idx < 0 { + return OCRText{}, errors.Wrap(code.OCRTextNotFoundError, + 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) ( - points []PointF, err error) { + results OCRTexts, err error) { for _, text := range texts { - point, err := t.FindText(text, options...) + ocrText, err := t.FindText(text, options...) if err != nil { 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, fmt.Sprintf("texts %s not found in %v", texts, t.texts())) } - return points, nil + return results, nil } func newVEDEMOCRService() (*veDEMOCRService, error) { @@ -300,11 +304,12 @@ func (dExt *DriverExt) FindScreenTextByOCR(text string, options ...ActionOption) if err != nil { return } - point, err = ocrTexts.FindText(text, dExt.ParseActionOptions(options...)...) + result, err := ocrTexts.FindText(text, dExt.ParseActionOptions(options...)...) if err != nil { log.Warn().Msgf("FindText failed: %s", err.Error()) return } + point = result.Center() log.Info().Str("text", text). Interface("point", point).Msgf("FindScreenTextByOCR success") diff --git a/hrp/pkg/uixt/swipe.go b/hrp/pkg/uixt/swipe.go index 69d5cfe7..7173caf6 100644 --- a/hrp/pkg/uixt/swipe.go +++ b/hrp/pkg/uixt/swipe.go @@ -143,7 +143,7 @@ func (dExt *DriverExt) swipeToTapTexts(texts []string, options ...ActionOption) if err != nil { return err } - point = points[0] // FIXME + point = points[0].Center() // FIXME return nil } foundTextAction := func(d *DriverExt) error { diff --git a/hrp/pkg/uixt/video_crawler.go b/hrp/pkg/uixt/video_crawler.go index 02e8324e..809314d1 100644 --- a/hrp/pkg/uixt/video_crawler.go +++ b/hrp/pkg/uixt/video_crawler.go @@ -79,7 +79,7 @@ func (l *LiveCrawler) checkLiveVideo(texts OCRTexts) (enterPoint PointF, yes boo // 预览流入口 points, err := texts.FindTexts([]string{"点击进入直播间", "直播中"}) if err == nil { - return points[0], true + return points[0].Center(), true } // TODO: 头像入口 @@ -271,7 +271,8 @@ func (dExt *DriverExt) autoPopupHandler(texts OCRTexts) error { points, err := texts.FindTexts([]string{"确定", "取消"}) if err == nil { 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") return err }