From 71097c5c0d4e0f8ed6a1b0aaa70b5fe3b7f2a170 Mon Sep 17 00:00:00 2001 From: CyJaySong Date: Tue, 12 Oct 2021 15:45:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20v3=20=E7=89=88=E7=9A=84?= =?UTF-8?q?=E4=BA=BA=E8=84=B8=E5=AF=B9=E6=AF=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vision/face/v2/match.go | 4 ++-- vision/face/v3/match.go | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 vision/face/v3/match.go diff --git a/vision/face/v2/match.go b/vision/face/v2/match.go index fa473cf..e98d2e1 100644 --- a/vision/face/v2/match.go +++ b/vision/face/v2/match.go @@ -8,7 +8,7 @@ import ( ) const ( - FACE_MATCH_URL = "https://aip.baidubce.com/rest/2.0/face/v2/match" + faceMatchUrl = "https://aip.baidubce.com/rest/2.0/face/v2/match" ) func (fc FaceClient) Match(img1, img2 *vision.Image, options map[string]interface{}) (*FaceResponse, error) { @@ -30,7 +30,7 @@ func (fc FaceClient) Match(img1, img2 *vision.Image, options map[string]interfac header := req.Header{ "Content-Type": "application/x-www-form-urlencoded", } - url := fmt.Sprintf("%s?access_token=%s", FACE_MATCH_URL, fc.AccessToken) + url := fmt.Sprintf("%s?access_token=%s", faceMatchUrl, fc.AccessToken) resp, err := req.Post(url, header, req.Param(options)) if err != nil { return nil, err diff --git a/vision/face/v3/match.go b/vision/face/v3/match.go new file mode 100644 index 0000000..4d94b21 --- /dev/null +++ b/vision/face/v3/match.go @@ -0,0 +1,44 @@ +package v3 + +import ( + "errors" + "fmt" + "github.com/imroc/req" +) + +const ( + faceMatchUrl = "https://aip.baidubce.com/rest/2.0/face/v3/match" +) + +type FaceMatchResponse struct { + *req.Resp +} + +type FaceMatchImage struct { + Image string `json:"image"` + ImageType string `json:"image_type"` + FaceType string `json:"face_type,omitempty"` // 默认LIVE + QualityControl string `json:"quality_control,omitempty"` // 默认 NONE + LivenessControl string `json:"liveness_control,omitempty"` // 默认 NONE + FaceSortType string `json:"face_sort_type,omitempty"` // 默认为0 +} + +func (fc FaceClient) FaceMatch(images [2]FaceMatchImage) (*FaceMatchResponse, error) { + for _, image := range images { + if image.ImageType != "BASE64" && image.ImageType != "URL" && image.ImageType != "FACE_TOKEN" { + return nil, errors.New("image_type is invalid") + } + if image.ImageType == "BASE64" && len(image.Image) > 2<<(20-1)*2 { + return nil, errors.New("image length is invalid") + } + } + if err := fc.Auth(); err != nil { + return nil, err + } + url := fmt.Sprintf("%s?access_token=%s", faceMatchUrl, fc.AccessToken) + resp, err := req.Post(url, req.BodyJSON(&images)) + if err != nil { + return nil, err + } + return &FaceMatchResponse{resp}, nil +}