如何解决“找不到默认凭据”错误

时间:2018-12-08 09:00:02

标签: image go google-cloud-platform detection

我正在使用this link进行图像检测程序,但是在调用函数时,它将在main函数中产生错误,我称该函数为检测图像类型的函数。该程序如下:-

package main
import (
    "bufio"
    "bytes"
    "context"
    "fmt"
    "io"
    "os"

    vision "cloud.google.com/go/vision/apiv1"
)
func init() {
    _ = context.Background()
    _ = vision.ImageAnnotatorClient{}
    _ = os.Open
}
func detectFaces(w io.Writer, file string) error {
    ctx := context.Background()

    client, err := vision.NewImageAnnotatorClient(ctx)
    if err != nil {
        fmt.Println("Hello in function")
        return err
    }

    f, err := os.Open(file)
    if err != nil {
        return err
    }
    defer f.Close()

    image, err := vision.NewImageFromReader(f)
    if err != nil {
        return err
    }
    annotations, err := client.DetectFaces(ctx, image, nil, 10)
    if err != nil {
        return err
    }
    if len(annotations) == 0 {
        fmt.Fprintln(w, "No faces found.")
    } else {
        fmt.Fprintln(w, "Faces:")
        for i, annotation := range annotations {
            fmt.Fprintln(w, "  Face", i)
            fmt.Fprintln(w, "    Anger:", annotation.AngerLikelihood)
            fmt.Fprintln(w, "    Joy:", annotation.JoyLikelihood)
            fmt.Fprintln(w, "    Surprise:", annotation.SurpriseLikelihood)
        }
    }
    return nil
}
func main() {
    var b bytes.Buffer
    writer := bufio.NewWriter(&b)
    err := detectFaces(writer, "aaa.jpg")
    fmt.Println(err)
}

错误是:-

  

google:找不到默认凭据。有关更多信息,请参见https://developers.google.com/accounts/docs/application-default-credentials

如何解决此错误。谁能帮我吗?

1 个答案:

答案 0 :(得分:1)

使用Google Cloud Console创建一个项目,然后启用Vision API。

在Cloud Console中,创建一个服务帐户,下载其json凭证文件,然后设置GOOGLE_APPLICATION_CREDENTIALS环境变量:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json