facepp

package module
v0.0.0-...-7bb1fd4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2025 License: GPL-2.0 Imports: 4 Imported by: 0

README

facepp-go

一个用于调用 Face++ API 的 Go 语言封装库,支持人脸检测、搜索以及人脸集合管理等功能。

功能特性

  • 人脸检测:支持通过图片 URL、本地图片文件或 Base64 编码进行人脸检测。
  • 人脸搜索:支持通过图片 URL、本地图片文件、Base64 编码或 FaceToken 在指定人脸集合中搜索相似人脸。
  • 人脸集合管理:支持创建、查询详情、删除人脸集合,以及向集合中添加人脸。
  • 用户 ID 设置:支持为检测到的人脸设置用户 ID。

安装

确保你已经安装了 Go 环境,然后运行以下命令:

go get github.com/sofun/facepp-go

使用示例

初始化
import "github.com/sofun/facepp-go"

facepp := facepp.NewFacePP("your_api_key", "your_api_secret")
人脸检测
resp, err := facepp.FaceDetectByImageFile("path/to/image.jpg", "0", "0")
人脸搜索
resp, err := facepp.FaceSearchByImageFile("path/to/image.jpg", "faceset_token", "outer_id", "1")
创建人脸集合
resp, err := facepp.CreateFaceset("MyFaceSet", "my_outer_id", "tags", "user_data")
获取人脸集合详情
resp, err := facepp.FacesetDetail("faceset_token", "outer_id")
删除人脸集合
resp, err := facepp.FacesetDelete("faceset_token", "outer_id", "1")
添加人脸到集合
resp, err := facepp.FaceAdd("faceset_token", "outer_id", "face_token")
设置人脸用户 ID
resp, err := facepp.SetUserId("face_token", "user_id")

示例图片

项目中的 examples/face1.jpgexamples/image.png 可用于测试人脸检测和搜索功能。

许可证

本项目基于 MIT 许可证发布。详情请查看 LICENSE 文件。

贡献

欢迎提交 Pull Request 或提出 Issue 来帮助改进本项目。

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FACE_DETECT_URL        = "https://api-cn.faceplusplus.com/facepp/v3/detect"
	CREATE_FACE_SET_URL    = "https://api-cn.faceplusplus.com/facepp/v3/faceset/create"
	FACE_SET_DETAIL_URL    = "https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail"
	FACE_SET_DELETE_URL    = "https://api-cn.faceplusplus.com/facepp/v3/faceset/delete"
	FACE_ADD_URL           = "https://api-cn.faceplusplus.com/facepp/v3/faceset/addface"
	FACE_SET_USERID_URL    = "https://api-cn.faceplusplus.com/facepp/v3/face/setuserid"
	FACE_SEARCH_URL        = "https://api-cn.faceplusplus.com/facepp/v3/search"
	ACCEPT                 = "Accept"
	APP_JSON               = "application/json"
	CallApiErrFormat       = "call api error,err: %v"
	ParamsInvalidErrFormat = "%s can not be empty"
	CallApiErrFormat2      = "{\"code\":\"%v\",\"message\":\"%s\"}"
)
View Source
var (
	ParamsInvalid      = NewError(400, "请求参数错误: %s 不能为空")
	HttpClientReqError = NewError(500, "Http客户端请求异常")
)

Functions

func GeneratorParams

func GeneratorParams(params map[string]string) string

Types

type ApiErrResp

type ApiErrResp struct {
	Time_used     int    `json:"time_used"`
	Error_message string `json:"error_message"`
	Request_id    string `json:"request_id"`
}

type ErrorCode

type ErrorCode struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

func NewError

func NewError(code int, message string) *ErrorCode

type FaceAddResp

type FaceAddResp struct {
	Faceset_token string `json:"faceset_token"`
	Time_used     int    `json:"time_used"`
	Face_added    int    `json:"face_added"`
	Face_count    int    `json:"face_count"`
	Request_id    string `json:"request_id"`
	Outer_id      string `json:"outer_id"`
}

type FaceDetectFace

type FaceDetectFace struct {
	Face_token     string `json:"face_token"`
	Face_rectangle struct {
		Left   int `json:"left"`
		Top    int `json:"top"`
		Width  int `json:"width"`
		Height int `json:"height"`
	} `json:"face_rectangle"`
}

type FaceDetectResp

type FaceDetectResp struct {
	Request_id    string           `json:"request_id"`
	Faces         []FaceDetectFace `json:"faces"`
	Image_id      string           `json:"image_id"`
	Time_used     int              `json:"time_used"`
	Error_message string           `json:"error_message"`
	Face_num      int              `json:"face_num"`
}

type FacePP

type FacePP struct {
	ApiKey    string
	ApiSecret string
}

func NewFacePP

func NewFacePP(apiKey, apiSecret string) *FacePP

func (*FacePP) CreateFaceset

func (f *FacePP) CreateFaceset(displayName, outerId, tags, userData string) (*FaceSetResp, error)

创建人脸集

func (*FacePP) FaceAdd

func (f *FacePP) FaceAdd(facesetToken, outerId, faceTokens string) (*FaceAddResp, error)

添加人脸,facesetToken, outerId 参数二选一,faceTokens:人脸token,多个用,隔开

func (*FacePP) FaceDetectByBase64

func (f *FacePP) FaceDetectByBase64(imageBase64, returnLandmark, returnAttributes string) (*FaceDetectResp, error)

人脸检测:通过图片的base64编码数据进行人脸检测

func (*FacePP) FaceDetectByImageFile

func (f *FacePP) FaceDetectByImageFile(filepath, returnLandmark, returnAttributes string) (*FaceDetectResp, error)

人脸检测:通过图片文件进行人脸检测

func (*FacePP) FaceDetectByImageUrl

func (f *FacePP) FaceDetectByImageUrl(imageUrl, returnLandmark, returnAttributes string) (*FaceDetectResp, error)

人脸检测:通过图片的url进行人脸检测

func (*FacePP) FaceSearchByFaceToken

func (f *FacePP) FaceSearchByFaceToken(faceToken string, facesetToken, outerId string, returnResultCount string) (*FaceSearchResp, error)

人脸搜索,根据face_token搜索

func (*FacePP) FaceSearchByImageBase64

func (f *FacePP) FaceSearchByImageBase64(imageBase64 string, facesetToken, outerId string, returnResultCount string) (*FaceSearchResp, error)

人脸搜索,根据图片base64搜索。 注意:imageBase64的编码格式为UTF-8

func (*FacePP) FaceSearchByImageFile

func (f *FacePP) FaceSearchByImageFile(filepath string, facesetToken, outerId string, returnResultCount string) (*FaceSearchResp, error)

人脸搜索,根据图片文件搜索

func (*FacePP) FaceSearchByImageUrl

func (f *FacePP) FaceSearchByImageUrl(imageUrl string, facesetToken, outerId string, returnResultCount string) (*FaceSearchResp, error)

人脸搜索,根据图片地址搜索

func (*FacePP) FacesetDelete

func (f *FacePP) FacesetDelete(facesetToken, outerId, checkEmpty string) (*FacesetDeleteResp, error)

删除人脸集, facesetToken, outerId 参数二选一 checkEmpty:是否检查FaceSet中是否存在face_token,默认值为1,不能删除

func (*FacePP) FacesetDetail

func (f *FacePP) FacesetDetail(facesetToken, outerId string) (*FacesetDetailResp, error)

获取人脸集详情,facesetToken, outerId 参数二选一

func (*FacePP) SetUserId

func (f *FacePP) SetUserId(faceToken, userId string) (*FaceSetUserIdResp, error)

设置人脸属性用户ID,facesetToken:人脸token, userId:用户自定义的标识信息

type FaceSearchFace

type FaceSearchFace struct {
	Face_rectangle struct {
		Left   int `json:"left"`
		Top    int `json:"top"`
		Width  int `json:"width"`
		Height int `json:"height"`
	} `json:"face_rectangle"`
	Face_token string `json:"face_token"`
}

type FaceSearchResp

type FaceSearchResp struct {
	Image_id   string             `json:"image_id"`
	Faces      []FaceSearchFace   `json:"faces"`
	Time_used  int                `json:"time_used"`
	Request_id string             `json:"request_id"`
	Results    []FaceSearchResult `json:"results"`
}

type FaceSearchResult

type FaceSearchResult struct {
	Confidence float64 `json:"confidence"`
	UserId     string  `json:"user_id"`
	FaceToken  string  `json:"face_token"`
}

type FaceSetResp

type FaceSetResp struct {
	Faceset_token string `json:"faceset_token"`
	Time_used     int    `json:"time_used"`
	Face_count    int    `json:"face_count"`
	Face_added    int    `json:"face_added"`
	Request_id    string `json:"request_id"`
	Outer_id      string `json:"outer_id"`
}

type FaceSetUserIdResp

type FaceSetUserIdResp struct {
	Request_id    string `json:"request_id"`
	Face_token    string `json:"face_token"`
	User_id       string `json:"user_id"`
	Time_used     int    `json:"time_used"`
	Error_message string `json:"error_message"`
}

type FacesetDeleteResp

type FacesetDeleteResp struct {
	Request_id    string `json:"request_id"`
	Faceset_token string `json:"faceset_token"`
	Outer_id      string `json:"outer_id"`
	Time_used     int    `json:"time_used"`
	Error_message string `json:"error_message"`
}

type FacesetDetailResp

type FacesetDetailResp struct {
	Faceset_token string   `json:"faceset_token"`
	Tags          string   `json:"tags"`
	Time_used     int      `json:"time_used"`
	User_data     string   `json:"user_data"`
	Display_name  string   `json:"display_name"`
	Face_tokens   []string `json:"face_tokens"`
	Face_count    int      `json:"face_count"`
	Request_id    string   `json:"request_id"`
	Outer_id      string   `json:"outer_id"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL