Skip to content

Commit

Permalink
添加 v3 版的人脸对比
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjaysong committed Oct 12, 2021
1 parent b72a39d commit 71097c5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions vision/face/v2/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
44 changes: 44 additions & 0 deletions vision/face/v3/match.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 71097c5

Please sign in to comment.