Last active
January 29, 2021 02:15
-
-
Save likejazz/f9f664a64354fcb9b74088a17e903d92 to your computer and use it in GitHub Desktop.
Detect Faces from the Google Vision API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// References: https://cloud.google.com/vision/docs/detecting-faces#vision_face_detection_gcs-drest | |
package main | |
import ( | |
vision "cloud.google.com/go/vision/apiv1" | |
"fmt" | |
"golang.org/x/net/context" | |
"google.golang.org/api/option" | |
pb "google.golang.org/genproto/googleapis/cloud/vision/v1" | |
"io" | |
) | |
// | |
// detectFaces gets faces from the Vision API for an image at the given file path. | |
// | |
func detectFaces(r io.Reader) ([]*pb.FaceAnnotation, error) { | |
ctx := context.Background() | |
var visionClient *vision.ImageAnnotatorClient | |
visionClient, err := vision.NewImageAnnotatorClient(ctx, option.WithCredentialsFile("xxx-dd4a9f849db0.json")) | |
if err != nil { | |
return nil, err | |
} | |
image, err := vision.NewImageFromReader(r) | |
if err != nil { | |
return nil, err | |
} | |
annotations, err := visionClient.DetectFaces(ctx, image, nil, 10) | |
if err != nil { | |
return nil, err | |
} | |
if len(annotations) == 0 { | |
fmt.Println("No faces found.") | |
return nil, nil | |
} else { | |
return annotations, nil | |
} | |
return nil, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment