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
+27 -11
View File
@@ -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)
}
+4 -3
View File
@@ -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
}
+3 -3
View File
@@ -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).