-
Notifications
You must be signed in to change notification settings - Fork 3
/
face.go
127 lines (108 loc) · 3.55 KB
/
face.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
package face
import (
"bytes"
"encoding/json"
"errors"
"log"
)
type Face struct {
client *Client
}
func NewFace(key string) *Face {
if len(key) == 0 {
return nil
}
f := new(Face)
f.client = NewClient(key)
return f
}
func (f *Face) detect(option *DetectParameters, data *bytes.Buffer, useJson bool) ([]byte, *ErrorResponse) {
url := getDetectURL(option)
return f.client.Connect("POST", url, data, useJson)
}
//Detect face with input URL
func (f *Face) DetectUrl(option *DetectParameters, url string) ([]byte, *ErrorResponse) {
data := getUrlByteBuffer(url)
return f.detect(option, data, true)
}
//Detect face with input image file path
func (f *Face) DetectFile(option *DetectParameters, filePath string) ([]byte, *ErrorResponse) {
data, err := getFileByteBuffer(filePath)
if err != nil {
return nil, &ErrorResponse{Err: errors.New("File path err:" + err.Error())}
}
return f.detect(option, data, false)
}
func getSimilarData(option SimilarParameter) *bytes.Buffer {
data, err := json.Marshal(option)
if err != nil {
log.Println("Error happen on json marshal:", err)
return nil
}
return bytes.NewBuffer(data)
}
// Find Face similarity from a Face List, with max return result to limited return records.
func (f *Face) FindSimilarFromList(targetID string, faceIdList []string, maxResult int) ([]byte, *ErrorResponse) {
var option SimilarParameter
option.FaceId = targetID
option.FaceIds = faceIdList
option.MaxNumOfCandidatesReturned = maxResult
data := getSimilarData(option)
api := getSimilarURL()
if data == nil {
return nil, &ErrorResponse{Err: errors.New("Invalid parameter")}
}
return f.client.Connect("POST", api, data, true)
}
// Find Face similarity from a Face List ID, with max return result to limited return records.
func (f *Face) FindSimilarFromListId(targetID string, listId string, maxResult int) ([]byte, *ErrorResponse) {
var option SimilarParameter
option.FaceId = targetID
option.FaceListId = listId
option.MaxNumOfCandidatesReturned = maxResult
data := getSimilarData(option)
api := getSimilarURL()
if data == nil {
return nil, &ErrorResponse{Err: errors.New("Invalid parameter")}
}
return f.client.Connect("POST", api, data, true)
}
// Grouping a slice of faceID to a Face Group
func (f *Face) GroupFaces(faceIDs []string) ([]byte, *ErrorResponse) {
var option GroupParameter
option.FaceIds = faceIDs
data, err := json.Marshal(option)
if err != nil {
log.Println("Error happen on json marshal:", err)
return nil, &ErrorResponse{Err: err}
}
url := getGroupURL()
return f.client.Connect("POST", url, bytes.NewBuffer(data), true)
}
// Identify a list of face to check belong to which face group
func (f *Face) IdentifyFaces(faceIDs []string, faceGroup string, maxResult int) ([]byte, *ErrorResponse) {
var option IdentifyParameter
option.FaceIds = faceIDs
option.PersonGroupId = faceGroup
option.MaxNumOfCandidatesReturned = maxResult
data, err := json.Marshal(option)
if err != nil {
log.Println("Error happen on json marshal:", err)
return nil, &ErrorResponse{Err: err}
}
url := getIdentifyURL()
return f.client.Connect("POST", url, bytes.NewBuffer(data), true)
}
// Compare input two face id to compute the similarity
func (f *Face) VerifyWithFace(face1 string, face2 string) ([]byte, *ErrorResponse) {
var option VerifyParameter
option.FaceId1 = face1
option.FaceId2 = face2
data, err := json.Marshal(option)
if err != nil {
log.Println("Error happen on json marshal:", err)
return nil, &ErrorResponse{Err: err}
}
url := getVerifyURL()
return f.client.Connect("POST", url, bytes.NewBuffer(data), true)
}