refactor: simplify APIs, remove GetUIResultMap

This commit is contained in:
lilong.129
2023-08-23 23:00:51 +08:00
parent 7984dcc928
commit 02a2d800e5
2 changed files with 23 additions and 42 deletions
+17 -9
View File
@@ -54,11 +54,13 @@ type ScreenResult struct {
bufSource *bytes.Buffer // raw image buffer bytes bufSource *bytes.Buffer // raw image buffer bytes
imagePath string // image file path imagePath string // image file path
Texts OCRTexts `json:"texts"` // dumped raw OCRTexts UploadedURL string `json:"uploaded_url"` // uploaded image url
Tags []string `json:"tags"` // tags for image, e.g. ["feed", "ad", "live"] Texts OCRTexts `json:"texts"` // dumped raw OCRTexts
VideoType string `json:"video_type,omitempty"` // video type: feed, live-preview or live Icons UIResultMap `json:"icons"` // CV 识别的图标
Feed *FeedVideo `json:"feed,omitempty"` Tags []string `json:"tags"` // tags for image, e.g. ["feed", "ad", "live"]
Live *LiveRoom `json:"live,omitempty"` VideoType string `json:"video_type,omitempty"` // video type: feed, live-preview or live
Feed *FeedVideo `json:"feed,omitempty"`
Live *LiveRoom `json:"live,omitempty"`
SwipeStartTime int64 `json:"swipe_start_time"` // 滑动开始时间戳 SwipeStartTime int64 `json:"swipe_start_time"` // 滑动开始时间戳
SwipeFinishTime int64 `json:"swipe_finish_time"` // 滑动结束时间戳 SwipeFinishTime int64 `json:"swipe_finish_time"` // 滑动结束时间戳
@@ -72,8 +74,7 @@ type ScreenResult struct {
type cacheStepData struct { type cacheStepData struct {
// cache step screenshot paths // cache step screenshot paths
screenShots []string screenShots []string
screenShotsUrls map[string]string // map screenshot file path to uploaded url
// cache step screenshot ocr results, key is image path, value is ScreenResult // cache step screenshot ocr results, key is image path, value is ScreenResult
screenResults map[string]*ScreenResult screenResults map[string]*ScreenResult
// cache feed/live video stat // cache feed/live video stat
@@ -82,7 +83,6 @@ type cacheStepData struct {
func (d *cacheStepData) reset() { func (d *cacheStepData) reset() {
d.screenShots = make([]string, 0) d.screenShots = make([]string, 0)
d.screenShotsUrls = make(map[string]string)
d.screenResults = make(map[string]*ScreenResult) d.screenResults = make(map[string]*ScreenResult)
d.videoCrawler = nil d.videoCrawler = nil
} }
@@ -222,7 +222,15 @@ func (dExt *DriverExt) GetStepCacheData() map[string]interface{} {
cacheData := make(map[string]interface{}) cacheData := make(map[string]interface{})
cacheData["video_stat"] = dExt.cacheStepData.videoCrawler cacheData["video_stat"] = dExt.cacheStepData.videoCrawler
cacheData["screenshots"] = dExt.cacheStepData.screenShots cacheData["screenshots"] = dExt.cacheStepData.screenShots
cacheData["screenshots_urls"] = dExt.cacheStepData.screenShotsUrls
screenShotsUrls := make(map[string]string)
for imagePath, screenResult := range dExt.cacheStepData.screenResults {
if screenResult.UploadedURL == "" {
continue
}
screenShotsUrls[imagePath] = screenResult.UploadedURL
}
cacheData["screenshots_urls"] = screenShotsUrls
screenSize, err := dExt.Driver.WindowSize() screenSize, err := dExt.Driver.WindowSize()
if err != nil { if err != nil {
+6 -33
View File
@@ -353,7 +353,6 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S
ScreenshotTakeElapsed: screenshotTakeElapsed, ScreenshotTakeElapsed: screenshotTakeElapsed,
} }
var imageUrl string
imageResult, err := dExt.ImageService.GetImage(bufSource, options...) imageResult, err := dExt.ImageService.GetImage(bufSource, options...)
if err != nil { if err != nil {
log.Error().Err(err).Msg("GetImage from ImageService failed") log.Error().Err(err).Msg("GetImage from ImageService failed")
@@ -362,11 +361,8 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S
if imageResult != nil { if imageResult != nil {
screenResult.ScreenshotCVElapsed = time.Since(startTime).Milliseconds() - screenshotTakeElapsed screenResult.ScreenshotCVElapsed = time.Since(startTime).Milliseconds() - screenshotTakeElapsed
screenResult.Texts = imageResult.OCRResult.ToOCRTexts() screenResult.Texts = imageResult.OCRResult.ToOCRTexts()
screenResult.UploadedURL = imageResult.URL
imageUrl = imageResult.URL screenResult.Icons = imageResult.UIResult
if imageUrl != "" {
dExt.cacheStepData.screenShotsUrls[imagePath] = imageUrl
}
if imageResult.LiveType != "" && imageResult.LiveType != "NoLive" { if imageResult.LiveType != "" && imageResult.LiveType != "NoLive" {
screenResult.Live = &LiveRoom{ screenResult.Live = &LiveRoom{
@@ -379,7 +375,7 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S
log.Debug(). log.Debug().
Str("imagePath", imagePath). Str("imagePath", imagePath).
Str("imageUrl", imageUrl). Str("imageUrl", screenResult.UploadedURL).
Int64("screenshot_take_elapsed(ms)", screenResult.ScreenshotTakeElapsed). Int64("screenshot_take_elapsed(ms)", screenResult.ScreenshotTakeElapsed).
Int64("screenshot_cv_elapsed(ms)", screenResult.ScreenshotCVElapsed). Int64("screenshot_cv_elapsed(ms)", screenResult.ScreenshotCVElapsed).
Msg("log screenshot") Msg("log screenshot")
@@ -506,36 +502,13 @@ func (u UIResults) GetUIResult(options ...ActionOption) (UIResult, error) {
return uiResults[idx], nil return uiResults[idx], nil
} }
func (dExt *DriverExt) GetUIResultMap(uiTypes []string) (uiResultMap UIResultMap, err error) {
screenResult, err := dExt.GetScreenResult()
if err != nil {
return nil, err
}
bufSource := screenResult.bufSource
imagePath := screenResult.imagePath
imageResult, err := dExt.ImageService.GetImage(bufSource,
WithScreenShotUITypes(uiTypes...)) // turn on UI type detection
if err != nil {
log.Error().Err(err).Msg("GetImage from ImageService failed")
return
}
imageUrl := imageResult.URL
if imageUrl != "" {
dExt.cacheStepData.screenShotsUrls[imagePath] = imageUrl
log.Debug().Str("imagePath", imagePath).Str("imageUrl", imageUrl).Msg("log screenshot")
}
uiResultMap = imageResult.UIResult
return
}
func (dExt *DriverExt) FindUIResult(uiTypes []string, options ...ActionOption) (point PointF, err error) { func (dExt *DriverExt) FindUIResult(uiTypes []string, options ...ActionOption) (point PointF, err error) {
uiResultMap, err := dExt.GetUIResultMap(uiTypes) screenResult, err := dExt.GetScreenResult(WithScreenShotUITypes(uiTypes...))
if err != nil { if err != nil {
return return
} }
uiResults, err := uiResultMap.FilterUIResults(uiTypes)
uiResults, err := screenResult.Icons.FilterUIResults(uiTypes)
if err != nil { if err != nil {
return return
} }