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.
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 {
gwda.WebDriver
windowSize gwda.Size
frame *bytes.Buffer
doneMjpegStream chan bool
// OpenCV
scale float64
matchMode TemplateMatchMode
threshold float64
CVArgs
}
// Extend 获得扩展后的 Driver
// 并指定匹配阀值,
// 获取当前设备的 Scale
// 默认匹配模式为 TmCcoeffNormed
// 默认关闭 OpenCV 匹配值计算后的输出
func Extend(driver gwda.WebDriver, threshold float64, matchMode ...TemplateMatchMode) (dExt *DriverExt, err error) {
func extend(driver gwda.WebDriver) (dExt *DriverExt, err error) {
dExt = &DriverExt{WebDriver: driver}
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")
}
err = dExt.extendOpenCV(threshold, matchMode...)
return dExt, err
return dExt, nil
}
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 {
return nil, errors.Wrap(err, "failed to init WDA driver")
}
driverExt, err := Extend(driver, 0.95)
driverExt, err := Extend(driver)
if err != nil {
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
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"image"
"io/ioutil"
"mime/multipart"
"net/http"
"strings"
"time"
)
var client = &http.Client{

View File

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

View File

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

View File

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

View File

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