From fcab5ece587a50fa24f059ae37cff3ca5d7e9dee Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Wed, 26 Apr 2023 21:46:24 +0800 Subject: [PATCH] refactor: OCRService.GetTexts remove options --- hrp/pkg/uixt/action.go | 2 +- hrp/pkg/uixt/ocr_vedem.go | 30 +++++++++++++++--------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/hrp/pkg/uixt/action.go b/hrp/pkg/uixt/action.go index 942610f6..d5f12c19 100644 --- a/hrp/pkg/uixt/action.go +++ b/hrp/pkg/uixt/action.go @@ -268,7 +268,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) error { var point PointF findTexts := func(d *DriverExt) error { var err error - ocrTexts, err := d.GetScreenTextsByOCR(scopeOption) + ocrTexts, err := d.GetScreenTextsByOCR() if err != nil { return err } diff --git a/hrp/pkg/uixt/ocr_vedem.go b/hrp/pkg/uixt/ocr_vedem.go index db230b6d..0b1ca58e 100644 --- a/hrp/pkg/uixt/ocr_vedem.go +++ b/hrp/pkg/uixt/ocr_vedem.go @@ -62,6 +62,13 @@ func (t OCRTexts) FindText(text string, options ...DataOption) ( continue } + // check if text in scope + if rect.Min.X < dataOptions.Scope[0] || rect.Max.X > dataOptions.Scope[2] || + rect.Min.Y < dataOptions.Scope[1] || rect.Max.Y > dataOptions.Scope[3] { + // not in scope + continue + } + rects = append(rects, rect) // contains text while not match exactly @@ -121,6 +128,7 @@ func newVEDEMOCRService() (*veDEMOCRService, error) { return &veDEMOCRService{}, nil } +// veDEMOCRService implements IOCRService interface type veDEMOCRService struct{} func (s *veDEMOCRService) getOCRResult(imageBuf *bytes.Buffer) ([]OCRResult, error) { @@ -205,7 +213,7 @@ func (s *veDEMOCRService) getOCRResult(imageBuf *bytes.Buffer) ([]OCRResult, err return ocrResult.OCRResult, nil } -func (s *veDEMOCRService) GetTexts(imageBuf *bytes.Buffer, options ...DataOption) ( +func (s *veDEMOCRService) GetTexts(imageBuf *bytes.Buffer) ( ocrTexts OCRTexts, err error) { ocrResults, err := s.getOCRResult(imageBuf) @@ -214,8 +222,6 @@ func (s *veDEMOCRService) GetTexts(imageBuf *bytes.Buffer, options ...DataOption return } - dataOptions := NewDataOptions(options...) - for _, ocrResult := range ocrResults { rect := image.Rectangle{ // ocrResult.Points 顺序:左上 -> 右上 -> 右下 -> 左下 @@ -229,18 +235,13 @@ func (s *veDEMOCRService) GetTexts(imageBuf *bytes.Buffer, options ...DataOption }, } - // check if text in scope - if rect.Min.X < dataOptions.Scope[0] || rect.Max.X > dataOptions.Scope[2] || - rect.Min.Y < dataOptions.Scope[1] || rect.Max.Y > dataOptions.Scope[3] { - // not in scope - continue - } - ocrTexts = append(ocrTexts, OCRText{ Text: ocrResult.Text, Rect: rect, }) } + + log.Debug().Interface("texts", ocrTexts).Msg("get screen texts by veDEM OCR") return } @@ -270,28 +271,27 @@ func getLogID(header http.Header) string { } type IOCRService interface { - GetTexts(imageBuf *bytes.Buffer, options ...DataOption) (ocrTexts OCRTexts, err error) + GetTexts(imageBuf *bytes.Buffer) (texts OCRTexts, err error) } -func (dExt *DriverExt) GetScreenTextsByOCR(options ...DataOption) (texts OCRTexts, err error) { +func (dExt *DriverExt) GetScreenTextsByOCR() (texts OCRTexts, err error) { var bufSource *bytes.Buffer if bufSource, err = dExt.TakeScreenShot( builtin.GenNameWithTimestamp("screenshot_%d_ocr")); err != nil { return } - ocrTexts, err := dExt.OCRService.GetTexts(bufSource, options...) + ocrTexts, err := dExt.OCRService.GetTexts(bufSource) if err != nil { log.Error().Err(err).Msg("GetScreenTextsByOCR failed") return } - log.Debug().Interface("texts", ocrTexts).Msg("get screen texts by OCR") return ocrTexts, nil } func (dExt *DriverExt) FindScreenTextByOCR(text string, options ...DataOption) (point PointF, err error) { - ocrTexts, err := dExt.GetScreenTextsByOCR(options...) + ocrTexts, err := dExt.GetScreenTextsByOCR() if err != nil { return }