spywiree-priv / translateimage

Google Translate Images for free in Go
MIT License
2 stars 1 forks source link

Multiple Image Translation Test Cases #9

Closed Avey777 closed 4 months ago

Avey777 commented 4 months ago

Is it possible to create a test case that translates multiple images, as the current test file contains cases of translating a single image?

Avey777 commented 4 months ago

As far as I understand, I can now transfer multiple image file path strings. However, I can only transfer one image file path. In this case, the code of this branch does not need to be modified, but the translation speed of the second and following images has not changed compared to the previous one.

Avey777 commented 4 months ago
curl --location '127.0.0.1:8090/translation' \
--header 'Content-Type: text/plain' \
--data '{
    "URL": [
        "https://cbu01.alicdn.com/img/ibank/O1CN01LdZDpx1qMLl44TLGV_!!2215685915481-0-cib.jpg",
        "https://cbu01.alicdn.com/img/ibank/O1CN01LdZDpx1qMLl44TLGV_!!2215685915481-0-cib.jpg",
        "https://cbu01.alicdn.com/img/ibank/O1CN01LdZDpx1qMLl44TLGV_!!2215685915481-0-cib.jpg",
        "https://cbu01.alicdn.com/img/ibank/O1CN01LdZDpx1qMLl44TLGV_!!2215685915481-0-cib.jpg"
    ],
    "Sl": "DETECT_LANGUAGE",
    "TL": "ENGLISH",
    "ID": "09343436",
    "DeleteMode": 0
}'
spywiree-priv commented 4 months ago

You can write something like that and I don't see the point in covering it with tests.

package main

import (
    "log"
    "os"
    "strconv"
    "sync"

    languagecodes "github.com/spywiree/langcodes"
    "github.com/spywiree/translateimage"
)

func main() {
    ctx, err := translateimage.NewContext()
    if err != nil {
        log.Fatalln(err)
    }
    defer ctx.Close()

    var wg sync.WaitGroup

    imagePaths := []string{} //Your image paths
    var n int
    for _, path := range imagePaths {
        wg.Add(1)
        n++

        go func() {
            img, err := ctx.TranslateFile(path,
                languagecodes.DETECT_LANGUAGE,
                languagecodes.ENGLISH)
            if err != nil {
                log.Println(err)
                wg.Done()
                return
            }

            data, err := img.ConvertTo("image/png")
            if err != nil {
                log.Println(err)
                wg.Done()
                return
            }

            err = os.WriteFile(strconv.Itoa(n)+".png", data, 0666)
            if err != nil {
                log.Println(err)
            }

            wg.Done()
        }()
    }

    wg.Wait()
}
Avey777 commented 4 months ago

I have tried this, but it does not guarantee that the output names of the images will be named in the order of the image paths I passed in, which will disrupt the order of my images.

spywiree-priv commented 4 months ago

What do you mean? First file should be named 1.png, second - 2.png and so on.

Avey777 commented 4 months ago

What do you mean? First file should be named 1.png, second - 2.png and so on.

image After debugging for a long time, I still don't understand why the example you gave above only outputs the last picture.

image

spywiree-priv commented 4 months ago

Ok, here is the fixed version.

package main

import (
    "log"
    "os"
    "strconv"
    "sync"

    languagecodes "github.com/spywiree/langcodes"
    "github.com/spywiree/translateimage"
)

func main() {
    ctx, err := translateimage.NewContext()
    if err != nil {
        log.Fatalln(err)
    }
    defer ctx.Close()

    var wg sync.WaitGroup

    imagePaths := []string{} //Your image paths
    for i, path := range imagePaths {
        wg.Add(1)

        go func(i int) {
            img, err := ctx.TranslateFile(path,
                languagecodes.DETECT_LANGUAGE,
                languagecodes.ENGLISH)
            if err != nil {
                log.Println(err)
                wg.Done()
                return
            }

            data, err := img.ConvertTo("image/png")
            if err != nil {
                log.Println(err)
                wg.Done()
                return
            }

            err = os.WriteFile(strconv.Itoa(i+1)+".png", data, 0666)
            if err != nil {
                log.Println(err)
            }

            wg.Done()
        }(i)
    }

    wg.Wait()
}
Avey777 commented 4 months ago

Perfect!

Avey777 commented 4 months ago
package main

import (
        "bytes"
        "fmt"
        "io/ioutil"
        "log"
        "net/http"
        "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)

                        // 将[]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(img)

                        // data, err := img.ConvertTo("image/png")
                        // if err != nil {
                        //         log.Println(err)
                        //         return
                        // }
                        // err = os.WriteFile("outputDir/"+strconv.Itoa(i+1)+".png", data, 0666)

                        // if err != nil {
                        //         log.Println(err)
                        // }
                        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)

}