From 4ca4a2f94613264677530369a90576d822556a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E8=81=AA?= Date: Mon, 25 Mar 2024 11:34:38 +0800 Subject: [PATCH] fix: failed to compress image for requesting vedm algorithm service --- hrp/cmd/adb/devices.go | 6 ++++- hrp/cmd/run.go | 2 +- hrp/pkg/gadb/client.go | 5 ++++- hrp/pkg/gadb/device.go | 42 ++++++++++++++++++++++++++--------- hrp/pkg/uixt/ext.go | 38 ++++++++++++++++++++++--------- hrp/pkg/uixt/service_vedem.go | 7 +++--- hrp/pkg/uixt/video_crawler.go | 6 ++--- 7 files changed, 76 insertions(+), 30 deletions(-) diff --git a/hrp/cmd/adb/devices.go b/hrp/cmd/adb/devices.go index 6640a45e..7aef4d18 100644 --- a/hrp/cmd/adb/devices.go +++ b/hrp/cmd/adb/devices.go @@ -41,7 +41,11 @@ var listAndroidDevicesCmd = &cobra.Command{ if isDetail { fmt.Println(format(d.DeviceInfo())) } else { - fmt.Println(d.Serial(), d.Usb()) + if usb, err := d.Usb(); err != nil { + fmt.Println(d.Serial()) + } else { + fmt.Println(d.Serial(), usb) + } } } return nil diff --git a/hrp/cmd/run.go b/hrp/cmd/run.go index e042a5b7..cb14a8c3 100644 --- a/hrp/cmd/run.go +++ b/hrp/cmd/run.go @@ -42,7 +42,7 @@ func init() { runCmd.Flags().BoolVarP(&continueOnFailure, "continue-on-failure", "c", false, "continue running next step when failure occurs") runCmd.Flags().BoolVar(&requestsLogOff, "log-requests-off", false, "turn off request & response details logging") runCmd.Flags().BoolVar(&httpStatOn, "http-stat", false, "turn on HTTP latency stat (DNSLookup, TCP Connection, etc.)") - runCmd.Flags().BoolVar(&pluginLogOn, "log-plugin", false, "turn on plugin logging") + runCmd.Flags().BoolVar(&pluginLogOn, "log-plugin", true, "turn on plugin logging") runCmd.Flags().StringVarP(&proxyUrl, "proxy-url", "p", "", "set proxy url") runCmd.Flags().BoolVarP(&saveTests, "save-tests", "s", false, "save tests summary") runCmd.Flags().BoolVarP(&genHTMLReport, "gen-html-report", "g", false, "generate html report") diff --git a/hrp/pkg/gadb/client.go b/hrp/pkg/gadb/client.go index 1c867dd1..eb6b9d8c 100644 --- a/hrp/pkg/gadb/client.go +++ b/hrp/pkg/gadb/client.go @@ -108,7 +108,7 @@ func (c Client) DeviceList() (devices []*Device, err error) { } fields := strings.Fields(line) - if len(fields) < 5 || len(fields[0]) == 0 { + if len(fields) < 4 || len(fields[0]) == 0 { log.Error().Str("line", line).Msg("get unexpected line") continue } @@ -117,6 +117,9 @@ func (c Client) DeviceList() (devices []*Device, err error) { mapAttrs := map[string]string{} for _, field := range sliceAttrs { split := strings.Split(field, ":") + if len(split) == 1 { + continue + } key, val := split[0], split[1] mapAttrs[key] = val } diff --git a/hrp/pkg/gadb/device.go b/hrp/pkg/gadb/device.go index 8b9705b6..c9f61602 100644 --- a/hrp/pkg/gadb/device.go +++ b/hrp/pkg/gadb/device.go @@ -107,20 +107,37 @@ func (d *Device) features() (features Features, err error) { return features, nil } -func (d *Device) Product() string { - return d.attrs["product"] +func (d Device) HasAttribute(key string) bool { + _, ok := d.attrs[key] + return ok } -func (d *Device) Model() string { - return d.attrs["model"] +func (d Device) Product() (string, error) { + if d.HasAttribute("product") { + return d.attrs["product"], nil + } + return "", errors.New("does not have attribute: product") } -func (d *Device) Usb() string { - return d.attrs["usb"] +func (d Device) Model() (string, error) { + if d.HasAttribute("model") { + return d.attrs["model"], nil + } + return "", errors.New("does not have attribute: model") } -func (d *Device) transportId() string { - return d.attrs["transport_id"] +func (d *Device) Usb() (string, error) { + if d.HasAttribute("usb") { + return d.attrs["usb"], nil + } + return "", errors.New("does not have attribute: usb") +} + +func (d Device) transportId() (string, error) { + if d.HasAttribute("transport_id") { + return d.attrs["transport_id"], nil + } + return "", errors.New("does not have attribute: transport_id") } func (d *Device) DeviceInfo() map[string]string { @@ -132,8 +149,13 @@ func (d *Device) Serial() string { return d.serial } -func (d *Device) IsUsb() bool { - return d.Usb() != "" +func (d *Device) IsUsb() (bool, error) { + usb, err := d.Usb() + if err != nil { + return false, err + } + + return usb != "", nil } func (d *Device) State() (DeviceState, error) { diff --git a/hrp/pkg/uixt/ext.go b/hrp/pkg/uixt/ext.go index 28f09fc9..cb86fff9 100644 --- a/hrp/pkg/uixt/ext.go +++ b/hrp/pkg/uixt/ext.go @@ -4,9 +4,9 @@ import ( "bytes" "fmt" "image" - "image/gif" + _ "image/gif" "image/jpeg" - "image/png" + _ "image/png" "math/rand" "mime" "mime/multipart" @@ -190,8 +190,26 @@ func (dExt *DriverExt) takeScreenShot(fileName string) (raw *bytes.Buffer, path } func compressImageBuffer(raw *bytes.Buffer) (compressed *bytes.Buffer, err error) { - // TODO: compress image data - return raw, nil + // 解码原始图像数据 + img, format, err := image.Decode(raw) + if err != nil { + return nil, err + } + + // 创建一个用来保存压缩后数据的buffer + var buf bytes.Buffer + + switch format { + // Convert to jpeg uniformly and compress with a compression rate of 95 + case "jpeg", "png": + jpegOptions := &jpeg.Options{Quality: 95} + err = jpeg.Encode(&buf, img, jpegOptions) + default: + return nil, fmt.Errorf("unsupported image format: %s", format) + } + + // 返回压缩后的图像数据 + return &buf, nil } // saveScreenShot saves image file with file name @@ -207,7 +225,8 @@ func (dExt *DriverExt) saveScreenShot(raw *bytes.Buffer, fileName string) (strin return "", errors.Wrap(err, "decode screenshot image failed") } - screenshotPath := filepath.Join(fmt.Sprintf("%s.%s", fileName, format)) + // The default format uses jpeg for compression + screenshotPath := filepath.Join(fmt.Sprintf("%s.%s", fileName, "jpeg")) file, err := os.Create(screenshotPath) if err != nil { return "", errors.Wrap(err, "create screenshot image file failed") @@ -217,12 +236,9 @@ func (dExt *DriverExt) saveScreenShot(raw *bytes.Buffer, fileName string) (strin }() switch format { - case "png": - err = png.Encode(file, img) - case "jpeg": - err = jpeg.Encode(file, img, nil) - case "gif": - err = gif.Encode(file, img, nil) + case "jpeg", "png": + jpegOptions := &jpeg.Options{} + err = jpeg.Encode(file, img, jpegOptions) default: return "", fmt.Errorf("unsupported image format: %s", format) } diff --git a/hrp/pkg/uixt/service_vedem.go b/hrp/pkg/uixt/service_vedem.go index 00d9ec2c..1b7ede88 100644 --- a/hrp/pkg/uixt/service_vedem.go +++ b/hrp/pkg/uixt/service_vedem.go @@ -218,6 +218,7 @@ func (s *veDEMImageService) GetImage(imageBuf *bytes.Buffer, options ...ActionOp bodyWriter.WriteField("uiTypes", uiType) } + // 使用高精度集群 bodyWriter.WriteField("ocrCluster", "highPrecision") if actionOptions.Timeout > 0 { @@ -488,8 +489,8 @@ func (box Box) IsEmpty() bool { func (box Box) IsIdentical(box2 Box) bool { // set the coordinate precision to 1 pixel return box.Point.IsIdentical(box2.Point) && - math.Abs(box.Width-box2.Width) < 1 && - math.Abs(box.Height-box2.Height) < 1 + builtin.IsZeroFloat64(math.Abs(box.Width-box2.Width)) && + builtin.IsZeroFloat64(math.Abs(box.Height-box2.Height)) } func (box Box) Center() PointF { @@ -544,7 +545,7 @@ func (u UIResultMap) FilterUIResults(uiTypes []string) (uiResults UIResults, err return } } - err = errors.Errorf("UI types %v not detected", uiTypes) + err = errors.Wrap(code.CVResultNotFoundError, fmt.Sprintf("UI types %v not detected", uiTypes)) return } diff --git a/hrp/pkg/uixt/video_crawler.go b/hrp/pkg/uixt/video_crawler.go index 7fe29cff..17bcb630 100644 --- a/hrp/pkg/uixt/video_crawler.go +++ b/hrp/pkg/uixt/video_crawler.go @@ -171,7 +171,7 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) { // swipe to next feed video log.Info().Msg("swipe to next feed video") swipeStartTime := time.Now() - if err = dExt.SwipeRelative(0.9, 0.8, 0.9, 0.1, WithOffsetRandomRange(-10, 10)); err != nil { + if err = dExt.SwipeRelative(0.85, 0.83-(float64(crawler.failedCount)*0.01), 0.85, 0.1, WithOffsetRandomRange(-10, 10)); err != nil { log.Error().Err(err).Msg("feed swipe up failed") return err } @@ -182,10 +182,10 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) { currentVideo, err := crawler.getCurrentVideo() if err != nil || currentVideo.Type == "" { crawler.failedCount++ - if crawler.failedCount >= 10 { + if crawler.failedCount >= 3 { // failed 10 consecutive times return errors.Wrap(code.TrackingGetError, - "get current feed video failed 10 consecutive times") + "get current feed video failed 3 consecutive times") } log.Warn(). Int64("failedCount", crawler.failedCount).