Closed Avey777 closed 5 months ago
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"reflect"
"sync"
"time"
languagecodes "github.com/spywiree/langcodes"
"github.com/spywiree/translateimage"
)
func main() {
ctx, err := translateimage.NewContext()
if err != nil {
log.Fatalln(err)
}
defer ctx.Close()
imageUrls := []string{
"https://cbu01.alicdn.com/img/ibank/O1CN01pVf3451Bs2s4RGWMU_!!0-0-cib.jpg",
"https://cbu01.alicdn.com/img/ibank/O1CN01LdZDpx1qMLl44TLGV_!!2215685915481-0-cib.jpg",
"https://cbu01.alicdn.com/img/ibank/O1CN01kwi4Jc2AxFpXEEgFI_!!962348269-0-cib.jpg",
"https://cbu01.alicdn.com/img/ibank/O1CN01CzxMUa1HpC3cCRpa1_!!3700320806-0-cib.jpg",
"https://cbu01.alicdn.com/img/ibank/O1CN01jziTCm1pYQUzEOw6S_!!2215180245372-0-cib.jpg",
"https://cbu01.alicdn.com/img/ibank/O1CN01cOVSDp1o9rjn1wUVH_!!3221035183-0-cib.jpg",
"https://cbu01.alicdn.com/img/ibank/O1CN01k52gqp1EFWReizHK4_!!2202163030322-0-cib.jpg",
"https://cbu01.alicdn.com/img/ibank/O1CN01WHXDiY1WON4XcaHv1_!!2213114702778-0-cib.jpg",
"https://cbu01.alicdn.com/img/ibank/O1CN01eEYODs1RX0ThIFKEr_!!2579812120-0-cib.jpg",
"https://cbu01.alicdn.com/img/ibank/O1CN01SdgSTa2EYInQLYAok_!!2215615578756-0-cib.jpg",
}
start := time.Now().Unix()
log.Print(start)
var wg sync.WaitGroup
for i, url := range imageUrls {
wg.Add(1)
go func(i int) {
// 下载图片
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error downloading image:", err)
return
}
defer resp.Body.Close()
// 读取图片数据
imageData, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading image data:", err)
return
}
fmt.Println("imageData:", i)
fmt.Println("start translate", time.Now().Unix())
// 将[]byte转换为io.Reader
reader := bytes.NewReader(imageData)
img, err := ctx.TranslateReader(reader,
languagecodes.DETECT_LANGUAGE,
languagecodes.ENGLISH)
if err != nil {
log.Println(err)
return
}
fmt.Println(reflect.TypeOf(img), i)
fmt.Println("start TranslateReader", time.Now().Unix())
wg.Done()
}(i)
}
wg.Wait()
end := time.Now().Unix()
log.Print(end)
// 将时间戳转换为 time.Time 对象
startTime := time.Unix(start, 0)
endTime := time.Unix(end, 0)
duration := endTime.Sub(startTime)
log.Print(duration)
}
(base) root@Jengro:/home/jengro/dev/google_translate_images# go run .
2024/06/21 10:36:11 1718937371
imageData: 6
start translate 1718937371
imageData: 5
start translate 1718937372
imageData: 9
start translate 1718937372
imageData: 4
start translate 1718937372
imageData: 2
start translate 1718937372
imageData: 7
start translate 1718937372
imageData: 8
start translate 1718937372
imageData: 3
start translate 1718937372
imageData: 0
start translate 1718937372
imageData: 1
start translate 1718937372
*spywiree_translateimage.ImageData 5
start TranslateReader 1718937383
*spywiree_translateimage.ImageData 6
start TranslateReader 1718937383
*spywiree_translateimage.ImageData 4
start TranslateReader 1718937393
*spywiree_translateimage.ImageData 9
start TranslateReader 1718937394
*spywiree_translateimage.ImageData 2
start TranslateReader 1718937403
*spywiree_translateimage.ImageData 7
start TranslateReader 1718937404
*spywiree_translateimage.ImageData 8
start TranslateReader 1718937414
*spywiree_translateimage.ImageData 3
start TranslateReader 1718937415
*spywiree_translateimage.ImageData 0
start TranslateReader 1718937425
*spywiree_translateimage.ImageData 1
start TranslateReader 1718937426
2024/06/21 10:37:06 1718937426
2024/06/21 10:37:06 55s
(base) root@Jengro:/home/jengro/dev/google_translate_images#
func (ctx *Context) TranslateFile(path string, source, target langcodes.LanguageCode, options ...Options) (*ImageData, error) {
_ = ctx.sem.Acquire(context.Background(), 1)
defer ctx.sem.Release(1)
opt := getOptions(options)
var pageOptions playwright.BrowserNewPageOptions
if opt.DebugMode {
videoPath, err := filepath.Abs(opt.VideoPath)
if err != nil {
return nil, err
}
pageOptions = playwright.BrowserNewPageOptions{
RecordVideo: &playwright.RecordVideo{
Dir: videoPath,
},
}
}
_ = ctx.sem.Acquire(context.Background(), 1)
If I comment this out, it will become a real coroutine. What is the purpose here?
When I allowed more than 2 tabs to run in parallel, they became so laggy that some tests began to fail.
When I allowed more than 2 tabs to run in parallel, they became so laggy that some tests began to fail.
I understand. This is to ensure the stability of the translation.
Does TranslateReader support concurrency? When calling here, the pictures are translated one by one, not at the same time.