change: make ocr as optional build tags

This commit is contained in:
debugtalk
2022-09-21 16:03:58 +08:00
parent a742b575fa
commit 937b48236e
8 changed files with 65 additions and 33 deletions

View File

@@ -21,24 +21,36 @@ import (
// TemplateMatchMode is the type of the template matching operation. // TemplateMatchMode is the type of the template matching operation.
type TemplateMatchMode int type TemplateMatchMode int
type CVArgs struct {
scale float64
matchMode TemplateMatchMode
threshold float64
}
type CVOption func(*CVArgs)
func WithTemplateMatchMode(mode TemplateMatchMode) CVOption {
return func(args *CVArgs) {
args.matchMode = mode
}
}
func WithThreshold(threshold float64) CVOption {
return func(args *CVArgs) {
args.threshold = threshold
}
}
type DriverExt struct { type DriverExt struct {
gwda.WebDriver gwda.WebDriver
windowSize gwda.Size windowSize gwda.Size
frame *bytes.Buffer frame *bytes.Buffer
doneMjpegStream chan bool doneMjpegStream chan bool
// OpenCV CVArgs
scale float64
matchMode TemplateMatchMode
threshold float64
} }
// Extend 获得扩展后的 Driver func extend(driver gwda.WebDriver) (dExt *DriverExt, err error) {
// 并指定匹配阀值,
// 获取当前设备的 Scale
// 默认匹配模式为 TmCcoeffNormed
// 默认关闭 OpenCV 匹配值计算后的输出
func Extend(driver gwda.WebDriver, threshold float64, matchMode ...TemplateMatchMode) (dExt *DriverExt, err error) {
dExt = &DriverExt{WebDriver: driver} dExt = &DriverExt{WebDriver: driver}
dExt.doneMjpegStream = make(chan bool, 1) dExt.doneMjpegStream = make(chan bool, 1)
@@ -48,8 +60,7 @@ func Extend(driver gwda.WebDriver, threshold float64, matchMode ...TemplateMatch
return nil, errors.Wrap(err, "failed to get windows size") return nil, errors.Wrap(err, "failed to get windows size")
} }
err = dExt.extendOpenCV(threshold, matchMode...) return dExt, nil
return dExt, err
} }
func (dExt *DriverExt) ConnectMjpegStream(httpClient *http.Client) (err error) { func (dExt *DriverExt) ConnectMjpegStream(httpClient *http.Client) (err error) {

View File

@@ -45,7 +45,7 @@ func InitWDAClient(options ...gwda.DeviceOption) (*DriverExt, error) {
if err != nil { if err != nil {
return nil, errors.Wrap(err, "failed to init WDA driver") return nil, errors.Wrap(err, "failed to init WDA driver")
} }
driverExt, err := Extend(driver, 0.95) driverExt, err := Extend(driver)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "failed to extend gwda.WebDriver") return nil, errors.Wrap(err, "failed to extend gwda.WebDriver")
} }

View File

@@ -0,0 +1,10 @@
//go:build !ocr
package uixt
import "github.com/rs/zerolog/log"
func (dExt *DriverExt) FindTextByOCR(ocrText string) (x, y, width, height float64, err error) {
log.Fatal().Msg("OCR is not supported")
return
}

View File

@@ -1,16 +1,14 @@
//go:build ocr
package uixt package uixt
import ( import (
"bytes" "bytes"
"encoding/base64"
"encoding/json"
"fmt" "fmt"
"image" "image"
"io/ioutil"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"strings" "strings"
"time"
) )
var client = &http.Client{ var client = &http.Client{

View File

@@ -1,3 +1,5 @@
//go:build ocr
package uixt package uixt
import ( import (

View File

@@ -5,12 +5,12 @@ package uixt
import ( import (
"image" "image"
"github.com/electricbubble/gwda"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
func (dExt *DriverExt) extendOpenCV(threshold float64, matchMode ...TemplateMatchMode) (err error) { func Extend(driver gwda.WebDriver, options ...CVOption) (dExt *DriverExt, err error) {
log.Fatal().Msg("opencv is not supported") return extend(driver)
return
} }
func (dExt *DriverExt) FindAllImageRect(search string) (rects []image.Rectangle, err error) { func (dExt *DriverExt) FindAllImageRect(search string) (rects []image.Rectangle, err error) {

View File

@@ -8,12 +8,15 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"github.com/electricbubble/gwda"
cvHelper "github.com/electricbubble/opencv-helper" cvHelper "github.com/electricbubble/opencv-helper"
) )
const ( const (
// TmCcoeffNormed maps to TM_CCOEFF_NORMED
TmCcoeffNormed TemplateMatchMode = iota
// TmSqdiff maps to TM_SQDIFF // TmSqdiff maps to TM_SQDIFF
TmSqdiff TemplateMatchMode = iota TmSqdiff
// TmSqdiffNormed maps to TM_SQDIFF_NORMED // TmSqdiffNormed maps to TM_SQDIFF_NORMED
TmSqdiffNormed TmSqdiffNormed
// TmCcorr maps to TM_CCORR // TmCcorr maps to TM_CCORR
@@ -22,8 +25,6 @@ const (
TmCcorrNormed TmCcorrNormed
// TmCcoeff maps to TM_CCOEFF // TmCcoeff maps to TM_CCOEFF
TmCcoeff TmCcoeff
// TmCcoeffNormed maps to TM_CCOEFF_NORMED
TmCcoeffNormed
) )
type DebugMode int type DebugMode int
@@ -42,18 +43,28 @@ const (
// 获取当前设备的 Scale // 获取当前设备的 Scale
// 默认匹配模式为 TmCcoeffNormed // 默认匹配模式为 TmCcoeffNormed
// 默认关闭 OpenCV 匹配值计算后的输出 // 默认关闭 OpenCV 匹配值计算后的输出
func (dExt *DriverExt) extendOpenCV(threshold float64, matchMode ...TemplateMatchMode) (err error) { func Extend(driver gwda.WebDriver, options ...CVOption) (dExt *DriverExt, err error) {
if dExt.scale, err = dExt.Scale(); err != nil { dExt, err = extend(driver)
return err if err != nil {
return nil, err
} }
if len(matchMode) == 0 { for _, option := range options {
matchMode = []TemplateMatchMode{TmCcoeffNormed} option(&dExt.CVArgs)
}
if dExt.scale, err = dExt.Scale(); err != nil {
return nil, err
}
if dExt.threshold == 0 {
dExt.threshold = 0.95 // default threshold
}
if dExt.matchMode == 0 {
dExt.matchMode = TmCcoeffNormed // default match mode
} }
dExt.matchMode = matchMode[0]
cvHelper.Debug(cvHelper.DebugMode(DmOff)) cvHelper.Debug(cvHelper.DebugMode(DmOff))
dExt.threshold = threshold return
return nil
} }
func (dExt *DriverExt) Debug(dm DebugMode) { func (dExt *DriverExt) Debug(dm DebugMode) {

View File

@@ -15,8 +15,8 @@ mkdir -p "output"
bin_path="output/hrp" bin_path="output/hrp"
# build # build
# optional build tags: opencv # optional build tags: opencv ocr
go build -ldflags '-s -w' -o "$bin_path" hrp/cmd/cli/main.go go build -ldflags '-s -w' -tags ocr -o "$bin_path" hrp/cmd/cli/main.go
# check output and version # check output and version
ls -lh "$bin_path" ls -lh "$bin_path"