Skip to content

Commit

Permalink
Merge pull request #18 from CyJaySong/master
Browse files Browse the repository at this point in the history
add person verify support
  • Loading branch information
chenqinghe authored Dec 16, 2020
2 parents b191385 + f4b0bf5 commit 3d49b78
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
38 changes: 38 additions & 0 deletions vision/face/v2/person_verify.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion vision/face/v3/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v2
package v3

import (
sdk "github.com/chenqinghe/baidu-ai-go-sdk"
Expand Down
40 changes: 40 additions & 0 deletions vision/face/v3/person_verify.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 3d49b78

Please sign in to comment.