-
Notifications
You must be signed in to change notification settings - Fork 0
/
beans.go
288 lines (253 loc) · 10.5 KB
/
beans.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package duface
import (
"github.com/guestin/mob/merrors"
"strings"
)
type BaseResponse struct {
ErrCode int `json:"error_code"`
ErrMsg string `json:"error_msg"`
LogId int64 `json:"log_id"`
Ts int64 `json:"timestamp"`
Cached int `json:"cached"`
}
func (this *BaseResponse) parseError(expect ...int) error {
fullExpect := append([]int{0}, expect...)
for _, v := range fullExpect {
if v == this.ErrCode {
return nil
}
}
return merrors.Errorf0(this.ErrCode, "[%d] %s", this.ErrCode, this.ErrMsg)
}
type FaceField = string
type FaceFields []FaceField
func (this FaceFields) MarshalText() ([]byte, error) {
if len(this) == 0 {
return []byte(""), nil
}
return []byte(strings.Join(this, ",")), nil
}
const (
AGE FaceField = "age"
BEAUTY FaceField = "beauty"
EXPRESSION FaceField = "expression"
FACE_SHAPE FaceField = "face_shape"
GENDER FaceField = "gender"
GLASSES FaceField = "glasses"
LANDMARK FaceField = "landmark"
LANDMARK150 FaceField = "landmark150"
QUALITY FaceField = "quality"
EYE_STATUS FaceField = "eye_status"
EMOTION FaceField = "emotion"
FACE_TYPE FaceField = "face_type"
MASK FaceField = "mask"
SPOOFING FaceField = "spoofing" // 合成图检测
)
type ImageTypes = string
const (
BASE64 ImageTypes = "BASE64"
URL ImageTypes = "URL"
FACE_TOKEN ImageTypes = "FACE_TOKEN"
)
type ControlValues = string
const (
NONE ControlValues = "NONE"
DEFAULT = NONE
LOW ControlValues = "LOW"
NORMAL ControlValues = "NORMAL"
HIGH ControlValues = "HIGH"
)
type ActionType = string
const (
APPEND ActionType = "APPEND"
REPLACE ActionType = "REPLACE"
)
type RegExtParams struct {
// 用户资料,长度限制 256B 默认空
UserInfo string `json:"user_info,omitempty"`
QualityControl ControlValues `json:"quality_control,omitempty"`
LivenessControl ControlValues `json:"liveness_control,omitempty"`
ActionType ActionType `json:"action_type,omitempty"`
}
type FaceLocation struct {
Left float32 `json:"left"`
Top float32 `json:"top"`
Width float32 `json:"width"`
Height float32 `json:"height"`
Rotation int `json:"rotation"`
}
type RegisterFaceResult struct {
FaceToken string `json:"face_token"`
Location FaceLocation `json:"location"`
}
type BasicResponse struct {
ErrCode int `json:"error_code"`
LogId int `json:"log_id"`
ErrMsg string `json:"error_msg"`
}
type GroupIdList []string
func (this GroupIdList) MarshalText() ([]byte, error) {
if len(this) == 0 {
return []byte(""), nil
}
return []byte(strings.Join(this, ",")), nil
}
// 通用搜索扩展参数
type SearchExtGeneric struct {
QualityControl ControlValues `json:"quality_control,omitempty"`
LivenessControl ControlValues `json:"liveness_control,omitempty"`
//当需要对特定用户进行比对时,指定 user_id 进行比对.即人脸认证功能.
UserId string `json:"user_id,omitempty"`
// 返回相似度最高的几个用户,默认为1,最多返回50个
MaxUserNum int `json:"max_face_num,omitempty"`
// 从指定的 group 中进行查找 用逗号分隔,上限 10 个
// 由于library本身占用了一个,所以这里最多支持9个
GroupIdList GroupIdList `json:"-"`
}
// 1:N 搜索扩展参数
type SearchExtParams struct {
SearchExtGeneric
// 0: 代表检测出的人脸按照人脸面积从大到小排列
// 1: 代表检测出的人脸按照距离图片中心从近到远排列
// 默认为 0
FaceSortType int `json:"face_sort_type"`
}
// M:N 搜索扩展参数
type MultiSearchExtParams struct {
SearchExtGeneric
// 匹配阈值(设置阈值后,score 低于此阈值的用户信息将不会返回) 最大 100 最小 0 默认 80
// 此阈值设置得越高,检索速度将会越快,推荐使用默认阈值 80
MatchThreshold int `json:"match_threshold,omitempty"`
}
type ImageData struct {
// 图片信息 (总数据大小应小于 10M),图片上传方式根据 image_type 来判断
Data string `json:"image"`
/*
图片类型
BASE64: 图片的 base64 值,base64 编码后的图片数据,编码后的图片大小不超过 2M;
URL: 图片的 URL 地址 (可能由于网络等原因导致下载图片时间过长);
FACE_TOKEN: 人脸图片的唯一标识,调用人脸检测接口时,会为每个人脸图片赋予一个唯一的 FACE_TOKEN,
同一张图片多次检测得到的 FACE_TOKEN 是同一个.
*/
Type ImageTypes `json:"image_type"`
}
type ComparisonInfo struct {
GroupId string `json:"group_id"`
UserId string `json:"user_id"`
UserInfo string `json:"user_info"`
Score float32 `json:"score"`
}
type MultiSearchResultItem struct {
FaceToken string `json:"face_token"`
FaceLocation FaceLocation `json:"location"`
ComparisonInfos []*ComparisonInfo `json:"user_list"`
}
type DetectExtParams struct {
/*
包括 age,beauty,expression,face_shape,gender,glasses,landmark,landmark150,
quality,eye_status,emotion,face_type,mask,spoofing 信息
逗号分隔.
默认只返回 face_token,人脸框,概率和旋转角度
*/
FaceFields FaceFields `json:"face_field,omitempty"`
/*
最多处理人脸的数目,默认值为 1,根据人脸检测排序类型检测图片中排序第一的人脸(默认为人脸面积最大的人脸),最大值 120
*/
MaxFaceNum uint32 `json:"max_face_num,omitempty"`
/*
人脸的类型
LIVE 表示生活照:通常为手机,相机拍摄的人像图片,或从网络获取的人像图片等
IDCARD 表示身份证芯片照:二代身份证内置芯片中的人像照片
WATERMARK 表示带水印证件照:一般为带水印的小图,如公安网小图
CERT 表示证件照片:如拍摄的身份证,工卡,护照,学生证等证件图片
默认 LIVE
*/
FaceType string `json:"face_type,omitempty"`
LivenessControl ControlValues `json:"liveness_control,omitempty"`
/*
人脸检测排序类型
0: 代表检测出的人脸按照人脸面积从大到小排列
1: 代表检测出的人脸按照距离图片中心从近到远排列
默认为 0
*/
FaceSortType int `json:"face_sort_type,omitempty"`
}
type FaceAngle struct {
Yaw float32 `json:"yaw"` // 三维旋转之左右旋转角 [-90 (左), 90 (右)]
Pitch float32 `json:"pitch"` // 三维旋转之俯仰角度 [-90 (上), 90 (下)]
Roll float32 `json:"roll"` // 平面内旋转角 [-180 (逆时针), 180 (顺时针)]
}
type FaceAttribute struct {
Type string `json:"type"` // 分类
Probability float32 `json:"probability"` // probability: 范围[0~1],0 最小,1 最大.
}
// [0,1] 取值,越接近 0 闭合的可能性越大
type EyeStatus struct {
Left float32 `json:"left_eye"`
Right float32 `json:"right_eye"`
}
type LandmarkPoint struct {
X float32 `json:"x"`
Y float32 `json:"y"`
}
type FaceOcclusion struct {
LeftEye float32 `json:"left_eye"` // 左眼
RightEye float32 `json:"right_eye"` // 右眼
Nose float32 `json:"nose"` // 鼻子
Mouth float32 `json:"mouth"` // 嘴巴
LeftCheck float32 `json:"left_check"` // 左脸颊
RightCheck float32 `json:"right_check"` // 右脸颊
Chin float32 `json:"chin"` // 下巴
}
type FaceQuality struct {
// 人脸各部分遮挡的概率,范围 [0~1],0 表示完整,1 表示不完整
Occlusion FaceOcclusion `json:"occlusion"`
// 人脸模糊程度,范围 [0~1],0 表示清晰,1 表示模糊
Blur float32
// 取值范围在 [0~255], 表示脸部区域的光照程度 越大表示光照越好
Illumination float32
// 人脸完整度,0 或 1, 0 为人脸溢出图像边界,1 为人脸都在图像边界内
Completeness int
}
type DetectResultItem struct {
// 人脸图片的唯一标识 (人脸检测 face_token 有效期为 60min)
Token string `json:"face_token,omitempty"`
// 人脸在图片中的位置
Location FaceLocation `json:"location,omitempty"`
// 人脸置信度,范围[0~1],代表这是一张人脸的概率,0 最小,1 最大.其中返回 0 或 1 时,数据类型为 Integer
Probability float32 `json:"face_probability,omitempty"`
// 人脸旋转角度参数
Angle FaceAngle `json:"angle,omitempty"`
// 年龄 ,当 face_field 包含 age 时返回
Age *float32 `json:"age,omitempty"`
// 美丑打分,范围 0-100,越大表示越美.当 face_fields 包含 beauty 时返回
Beauty *int `json:"beauty,omitempty"`
// type: none: 不笑;smile: 微笑;laugh: 大笑
Expression *FaceAttribute `json:"expression,omitempty"` // 表情,当 face_field 包含 expression 时返回
// type: square: 正方形 triangle: 三角形 oval: 椭圆 heart: 心形 round: 圆形
Shape *FaceAttribute `json:"face_shape,omitempty"` // 脸型,当 face_field 包含 face_shape 时返回
// type: male: 男性 female: 女性
Gender *FaceAttribute `json:"gender,omitempty"` // 性别,face_field 包含 gender 时返回
// type: none: 无眼镜,common: 普通眼镜,sun: 墨镜
Glasses *FaceAttribute `json:"glasses,omitempty"` // 性别,face_field 包含 glasses 时返回
EyeStatus *EyeStatus `json:"eye_status,omitempty"` // 双眼状态(睁开 / 闭合) face_field 包含 eye_status 时返回
// type: angry: 愤怒 disgust: 厌恶 fear: 恐惧 happy: 高兴 sad: 伤心 surprise: 惊讶 neutral: 无表情 pouty: 撅嘴 grimace: 鬼脸
Emotion *FaceAttribute `json:"emotion,omitempty"` // 情绪 face_field 包含 emotion 时返回
// type: human: 真实人脸 cartoon: 卡通人脸
Type *FaceAttribute `json:"face_field,omitempty"` //真实人脸 / 卡通人脸 face_field 包含 face_type 时返回
// type: 没戴口罩 / 戴口罩 取值 0 或 1 0 代表没戴口罩 1 代表戴口罩
Mask *FaceAttribute `json:"mask,omitempty"` // 口罩识别 face_field 包含 mask 时返回
Landmark []LandmarkPoint `json:"landmark,omitempty"` // 4 个关键点位置,左眼中心,右眼中心,鼻尖,嘴中心.face_field 包含 landmark 时返回
Landmark72 []LandmarkPoint `json:"landmark72,omitempty"` // 72 个特征点位置 face_field 包含 landmark72 时返回
Landmark150 []LandmarkPoint `json:"landmark150,omitempty"` // 150 个特征点位置 face_field 包含 landmark150 时返回
Quality *FaceQuality `json:"quality,omitempty"` //人脸质量信息.face_field 包含 quality 时返回
Spoofing float32 `json:"spoofing,omitempty"` // 判断图片是否为合成图,注意:官方文档对该字段的取值范围没有说明
}
type DetectResult struct {
FaceNum int `json:"face_num"`
DetectResultItems []*DetectResultItem `json:"face_list"`
}
type UserFaceItem struct {
FaceToken string `json:"face_token"`
CreateTime string `json:"ctime"`
}