refactor: vedem ocr get texts

This commit is contained in:
debugtalk
2022-10-12 20:25:02 +08:00
parent f6623c0869
commit daa62486c5
6 changed files with 28 additions and 59 deletions
+4 -4
View File
@@ -26,11 +26,11 @@ You can get more installation introduction on [hybridgroup/gocv].
### OCR ### OCR
OCR API is a paid service, you need to pre-purchase and configure the account key. OCR API is a paid service, you need to pre-purchase and configure the environment variables.
```bash - VEDEM_OCR_URL
$ make build tags=ocr - VEDEM_OCR_AK
``` - VEDEM_OCR_SK
## Thanks ## Thanks
+3 -4
View File
@@ -30,14 +30,13 @@ func TestIOSDemo(t *testing.T) {
// 持续监测手机屏幕,直到出现青少年模式弹窗后,点击「我知道了」 // 持续监测手机屏幕,直到出现青少年模式弹窗后,点击「我知道了」
for { for {
_, err1 := driverExt.GetTextXY("青少年模式") points, err := driverExt.GetTextXYs([]string{"青少年模式", "我知道了"})
point, err2 := driverExt.GetTextXY("我知道了") if err != nil {
if err1 != nil || err2 != nil {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
continue continue
} }
err := driverExt.TapAbsXY(point.X, point.Y, "") err = driverExt.TapAbsXY(points[1].X, points[1].Y, "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
-17
View File
@@ -1,17 +0,0 @@
//go:build !ocr
package uixt
import (
"github.com/rs/zerolog/log"
)
func (dExt *DriverExt) FindTextByOCR(ocrText string, index ...int) (x, y, width, height float64, err error) {
log.Fatal().Msg("OCR is not supported")
return
}
func (dExt *DriverExt) FindTextsByOCR(ocrTexts []string) (ps map[string][]float64, err error) {
log.Fatal().Msg("OCR is not supported")
return
}
@@ -1,5 +1,3 @@
//go:build ocr
package uixt package uixt
import ( import (
@@ -159,25 +157,22 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, index ...int) (
return rects[idx], nil return rects[idx], nil
} }
func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte) (rects map[string]image.Rectangle, err error) { func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte) (rects []image.Rectangle, err error) {
ocrResults, err := s.getOCRResult(imageBuf) ocrResults, err := s.getOCRResult(imageBuf)
if err != nil { if err != nil {
log.Error().Err(err).Msg("getOCRResult failed") log.Error().Err(err).Msg("getOCRResult failed")
return return
} }
var ocrTexts []string
rects = map[string]image.Rectangle{}
for _, text := range texts { for _, text := range texts {
var found bool
for _, ocrResult := range ocrResults { for _, ocrResult := range ocrResults {
ocrTexts = append(ocrTexts, ocrResult.Text)
// not contains text // not contains text
if !strings.Contains(ocrResult.Text, text) { if !strings.Contains(ocrResult.Text, text) {
continue continue
} }
found = true
rect := image.Rectangle{ rect := image.Rectangle{
// ocrResult.Points 顺序:左上 -> 右上 -> 右下 -> 左下 // ocrResult.Points 顺序:左上 -> 右上 -> 右下 -> 左下
Min: image.Point{ Min: image.Point{
@@ -189,12 +184,11 @@ func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte) (rects map[
Y: int(ocrResult.Points[2].Y), Y: int(ocrResult.Points[2].Y),
}, },
} }
rects[text] = rect rects = append(rects, rect)
break break
} }
if !found {
if _, ok := rects[text]; !ok { rects = append(rects, image.Rectangle{})
rects[text] = image.Rectangle{}
} }
} }
@@ -220,12 +214,13 @@ func (dExt *DriverExt) FindTextByOCR(ocrText string, index ...int) (x, y, width,
return return
} }
log.Info().Str("ocrText", ocrText).Msgf("FindText success") log.Info().Str("ocrText", ocrText).
Interface("rect", rect).Msgf("FindTextByOCR success")
x, y, width, height = dExt.MappingToRectInUIKit(rect) x, y, width, height = dExt.MappingToRectInUIKit(rect)
return return
} }
func (dExt *DriverExt) FindTextsByOCR(ocrTexts []string) (ps map[string][]float64, err error) { func (dExt *DriverExt) FindTextsByOCR(ocrTexts []string) (points [][]float64, err error) {
var bufSource *bytes.Buffer var bufSource *bytes.Buffer
if bufSource, err = dExt.takeScreenShot(); err != nil { if bufSource, err = dExt.takeScreenShot(); err != nil {
err = fmt.Errorf("takeScreenShot error: %v", err) err = fmt.Errorf("takeScreenShot error: %v", err)
@@ -240,15 +235,11 @@ func (dExt *DriverExt) FindTextsByOCR(ocrTexts []string) (ps map[string][]float6
return return
} }
ps = map[string][]float64{} log.Info().Interface("ocrTexts", ocrTexts).
log.Info().Interface("ocrTexts", ocrTexts).Msgf("FindTexts success") Interface("rects", rects).Msgf("FindTextsByOCR success")
for text, rect := range rects { for _, rect := range rects {
if rect == (image.Rectangle{}) {
ps[text] = []float64{}
continue
}
x, y, width, height := dExt.MappingToRectInUIKit(rect) x, y, width, height := dExt.MappingToRectInUIKit(rect)
ps[text] = []float64{x, y, width, height} points = append(points, []float64{x, y, width, height})
} }
return return
+5 -9
View File
@@ -41,22 +41,18 @@ func (dExt *DriverExt) GetTextXY(ocrText string, index ...int) (point PointF, er
return point, nil return point, nil
} }
func (dExt *DriverExt) GetTextXYs(ocrText []string) (points map[string]PointF, err error) { func (dExt *DriverExt) GetTextXYs(ocrText []string) (points []PointF, err error) {
ps, err := dExt.FindTextsByOCR(ocrText) ps, err := dExt.FindTextsByOCR(ocrText)
if err != nil { if err != nil {
return map[string]PointF{}, err return nil, err
} }
points = map[string]PointF{} for _, point := range ps {
for text, point := range ps { pointF := PointF{
if len(point) == 0 {
points[text] = PointF{}
continue
}
points[text] = PointF{
X: point[0] + point[2]*0.5, X: point[0] + point[2]*0.5,
Y: point[1] + point[3]*0.5, Y: point[1] + point[3]*0.5,
} }
points = append(points, pointF)
} }
return points, nil return points, nil
+3 -3
View File
@@ -4,10 +4,10 @@
# Usage: # Usage:
# $ make build # $ make build
# $ make build tags=ocr # $ make build tags=opencv
# or # or
# $ bash scripts/build.sh # $ bash scripts/build.sh
# $ bash scripts/build.sh ocr # $ bash scripts/build.sh opencv
set -e set -e
set -x set -x
@@ -16,7 +16,7 @@ set -x
mkdir -p "output" mkdir -p "output"
bin_path="output/hrp" bin_path="output/hrp"
# optional build tags: opencv ocr # optional build tags: opencv
tags=$1 tags=$1
# build # build