fix: failed to compress image for requesting vedm algorithm service

This commit is contained in:
徐聪
2024-03-25 11:34:38 +08:00
parent 4acf9e5dec
commit 4ca4a2f946
7 changed files with 76 additions and 30 deletions
+5 -1
View File
@@ -41,7 +41,11 @@ var listAndroidDevicesCmd = &cobra.Command{
if isDetail { if isDetail {
fmt.Println(format(d.DeviceInfo())) fmt.Println(format(d.DeviceInfo()))
} else { } 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 return nil
+1 -1
View File
@@ -42,7 +42,7 @@ func init() {
runCmd.Flags().BoolVarP(&continueOnFailure, "continue-on-failure", "c", false, "continue running next step when failure occurs") 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(&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(&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().StringVarP(&proxyUrl, "proxy-url", "p", "", "set proxy url")
runCmd.Flags().BoolVarP(&saveTests, "save-tests", "s", false, "save tests summary") runCmd.Flags().BoolVarP(&saveTests, "save-tests", "s", false, "save tests summary")
runCmd.Flags().BoolVarP(&genHTMLReport, "gen-html-report", "g", false, "generate html report") runCmd.Flags().BoolVarP(&genHTMLReport, "gen-html-report", "g", false, "generate html report")
+4 -1
View File
@@ -108,7 +108,7 @@ func (c Client) DeviceList() (devices []*Device, err error) {
} }
fields := strings.Fields(line) 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") log.Error().Str("line", line).Msg("get unexpected line")
continue continue
} }
@@ -117,6 +117,9 @@ func (c Client) DeviceList() (devices []*Device, err error) {
mapAttrs := map[string]string{} mapAttrs := map[string]string{}
for _, field := range sliceAttrs { for _, field := range sliceAttrs {
split := strings.Split(field, ":") split := strings.Split(field, ":")
if len(split) == 1 {
continue
}
key, val := split[0], split[1] key, val := split[0], split[1]
mapAttrs[key] = val mapAttrs[key] = val
} }
+32 -10
View File
@@ -107,20 +107,37 @@ func (d *Device) features() (features Features, err error) {
return features, nil return features, nil
} }
func (d *Device) Product() string { func (d Device) HasAttribute(key string) bool {
return d.attrs["product"] _, ok := d.attrs[key]
return ok
} }
func (d *Device) Model() string { func (d Device) Product() (string, error) {
return d.attrs["model"] if d.HasAttribute("product") {
return d.attrs["product"], nil
}
return "", errors.New("does not have attribute: product")
} }
func (d *Device) Usb() string { func (d Device) Model() (string, error) {
return d.attrs["usb"] if d.HasAttribute("model") {
return d.attrs["model"], nil
}
return "", errors.New("does not have attribute: model")
} }
func (d *Device) transportId() string { func (d *Device) Usb() (string, error) {
return d.attrs["transport_id"] 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 { func (d *Device) DeviceInfo() map[string]string {
@@ -132,8 +149,13 @@ func (d *Device) Serial() string {
return d.serial return d.serial
} }
func (d *Device) IsUsb() bool { func (d *Device) IsUsb() (bool, error) {
return d.Usb() != "" usb, err := d.Usb()
if err != nil {
return false, err
}
return usb != "", nil
} }
func (d *Device) State() (DeviceState, error) { func (d *Device) State() (DeviceState, error) {
+27 -11
View File
@@ -4,9 +4,9 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"image" "image"
"image/gif" _ "image/gif"
"image/jpeg" "image/jpeg"
"image/png" _ "image/png"
"math/rand" "math/rand"
"mime" "mime"
"mime/multipart" "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) { 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 // 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") 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) file, err := os.Create(screenshotPath)
if err != nil { if err != nil {
return "", errors.Wrap(err, "create screenshot image file failed") 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 { switch format {
case "png": case "jpeg", "png":
err = png.Encode(file, img) jpegOptions := &jpeg.Options{}
case "jpeg": err = jpeg.Encode(file, img, jpegOptions)
err = jpeg.Encode(file, img, nil)
case "gif":
err = gif.Encode(file, img, nil)
default: default:
return "", fmt.Errorf("unsupported image format: %s", format) return "", fmt.Errorf("unsupported image format: %s", format)
} }
+4 -3
View File
@@ -218,6 +218,7 @@ func (s *veDEMImageService) GetImage(imageBuf *bytes.Buffer, options ...ActionOp
bodyWriter.WriteField("uiTypes", uiType) bodyWriter.WriteField("uiTypes", uiType)
} }
// 使用高精度集群
bodyWriter.WriteField("ocrCluster", "highPrecision") bodyWriter.WriteField("ocrCluster", "highPrecision")
if actionOptions.Timeout > 0 { if actionOptions.Timeout > 0 {
@@ -488,8 +489,8 @@ func (box Box) IsEmpty() bool {
func (box Box) IsIdentical(box2 Box) bool { func (box Box) IsIdentical(box2 Box) bool {
// set the coordinate precision to 1 pixel // set the coordinate precision to 1 pixel
return box.Point.IsIdentical(box2.Point) && return box.Point.IsIdentical(box2.Point) &&
math.Abs(box.Width-box2.Width) < 1 && builtin.IsZeroFloat64(math.Abs(box.Width-box2.Width)) &&
math.Abs(box.Height-box2.Height) < 1 builtin.IsZeroFloat64(math.Abs(box.Height-box2.Height))
} }
func (box Box) Center() PointF { func (box Box) Center() PointF {
@@ -544,7 +545,7 @@ func (u UIResultMap) FilterUIResults(uiTypes []string) (uiResults UIResults, err
return return
} }
} }
err = errors.Errorf("UI types %v not detected", uiTypes) err = errors.Wrap(code.CVResultNotFoundError, fmt.Sprintf("UI types %v not detected", uiTypes))
return return
} }
+3 -3
View File
@@ -171,7 +171,7 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
// swipe to next feed video // swipe to next feed video
log.Info().Msg("swipe to next feed video") log.Info().Msg("swipe to next feed video")
swipeStartTime := time.Now() 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") log.Error().Err(err).Msg("feed swipe up failed")
return err return err
} }
@@ -182,10 +182,10 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
currentVideo, err := crawler.getCurrentVideo() currentVideo, err := crawler.getCurrentVideo()
if err != nil || currentVideo.Type == "" { if err != nil || currentVideo.Type == "" {
crawler.failedCount++ crawler.failedCount++
if crawler.failedCount >= 10 { if crawler.failedCount >= 3 {
// failed 10 consecutive times // failed 10 consecutive times
return errors.Wrap(code.TrackingGetError, return errors.Wrap(code.TrackingGetError,
"get current feed video failed 10 consecutive times") "get current feed video failed 3 consecutive times")
} }
log.Warn(). log.Warn().
Int64("failedCount", crawler.failedCount). Int64("failedCount", crawler.failedCount).