From f4b0bf5c23f5f88f3eff295e99e92ca70ebc2a0f Mon Sep 17 00:00:00 2001 From: CyJaySong Date: Tue, 15 Dec 2020 21:09:48 +0800 Subject: [PATCH] add person verify support --- vision/face/v2/person_verify.go | 38 +++++++++++++++++++++++++++++++ vision/face/v3/client.go | 2 +- vision/face/v3/person_verify.go | 40 +++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 vision/face/v2/person_verify.go create mode 100644 vision/face/v3/person_verify.go diff --git a/vision/face/v2/person_verify.go b/vision/face/v2/person_verify.go new file mode 100644 index 0000000..c6f9b9f --- /dev/null +++ b/vision/face/v2/person_verify.go @@ -0,0 +1,38 @@ +package v2 + +import ( + "errors" + "fmt" + "github.com/imroc/req" +) + +const ( + facePersonVerifyUrl = "https://aip.baidubce.com/rest/2.0/face/v2/person/verify" +) + +type PersonVerifyResponse struct { + *req.Resp +} + +/// image cannot exceed 5M +func (fc FaceClient) PersonVerify(image, idCardNumber, name string, options map[string]string) (*PersonVerifyResponse, error) { + if len(image) > 2<<(20-1)*5 { + return nil, errors.New("image length is invalid") + } + + if fc.AccessToken == "" { + if err := fc.Auth(); err != nil { + return nil, err + } + } + options["image"] = image + options["id_card_number"] = idCardNumber + options["name"] = name + + url := fmt.Sprintf("%s?access_token=%s", facePersonVerifyUrl, fc.AccessToken) + resp, err := req.Post(url, req.BodyJSON(&options)) + if err != nil { + return nil, err + } + return &PersonVerifyResponse{resp}, nil +} diff --git a/vision/face/v3/client.go b/vision/face/v3/client.go index 0194080..c310895 100644 --- a/vision/face/v3/client.go +++ b/vision/face/v3/client.go @@ -1,4 +1,4 @@ -package v2 +package v3 import ( sdk "github.com/chenqinghe/baidu-ai-go-sdk" diff --git a/vision/face/v3/person_verify.go b/vision/face/v3/person_verify.go new file mode 100644 index 0000000..79aea38 --- /dev/null +++ b/vision/face/v3/person_verify.go @@ -0,0 +1,40 @@ +package v3 + +import ( + "errors" + "fmt" + "github.com/imroc/req" +) + +const ( + facePersonVerifyUrl = "https://aip.baidubce.com/rest/2.0/face/v3/person/verify" +) + +type PersonVerifyResponse struct { + *req.Resp +} + +func (fc FaceClient) PersonVerify(image, imageType, idCardNumber, name string, options map[string]string) (*PersonVerifyResponse, error) { + if imageType != "BASE64" && imageType != "URL" && imageType != "FACE_TOKEN" { + return nil, errors.New("image_type is invalid") + } + if imageType == "BASE64" && len(image) > 2<<(20-1)*2 { + return nil, errors.New("image length is invalid") + } + if fc.AccessToken == "" { + if err := fc.Auth(); err != nil { + return nil, err + } + } + options["image"] = image + options["image_type"] = imageType + options["id_card_number"] = idCardNumber + options["name"] = name + + url := fmt.Sprintf("%s?access_token=%s", facePersonVerifyUrl, fc.AccessToken) + resp, err := req.Post(url, req.BodyJSON(&options)) + if err != nil { + return nil, err + } + return &PersonVerifyResponse{resp}, nil +}