Skip to content
This repository was archived by the owner on Nov 8, 2021. It is now read-only.

Commit 0b6a6bb

Browse files
Xuan (Sean) Huhuxuan
authored andcommitted
Merged PR 3143: Million Scale Python SDK update.
1 parent e985c8e commit 0b6a6bb

20 files changed

+1217
-65
lines changed

cognitive_face/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
from . import face
99
from . import face_list
10+
from . import large_face_list
11+
from . import large_face_list_face
12+
from . import large_person_group
13+
from . import large_person_group_person
14+
from . import large_person_group_person_face
1015
from . import person
1116
from . import person_group
1217
from . import util

cognitive_face/face.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ def detect(image, face_id=True, landmarks=False, attributes=''):
4343

4444
def find_similars(face_id,
4545
face_list_id=None,
46+
large_face_list_id=None,
4647
face_ids=None,
4748
max_candidates_return=20,
4849
mode='matchPerson'):
4950
"""Given query face's `face_id`, to search the similar-looking faces from a
50-
`face_id` array or a `face_list_id`.
51+
`face_id` array, a `face_list_id` or a `large_face_list_id`.
5152
52-
Parameter `face_list_id` and `face_ids` should not be provided at the same
53-
time.
53+
Parameter `large_face_list_id`, `face_list_id` and `face_ids` should not be
54+
provided at the same time.
5455
5556
Args:
5657
face_id: `face_id` of the query face. User needs to call `face.detect`
@@ -59,6 +60,10 @@ def find_similars(face_id,
5960
face_list_id: An existing user-specified unique candidate face list,
6061
created in `face_list.create`. Face list contains a set of
6162
`persisted_face_ids` which are persisted and will never expire.
63+
large_face_list_id: An existing user-specified unique candidate face
64+
list, created in `large_face_list.create`. Large Face list contains
65+
a set of `persisted_face_ids` which are persisted and will never
66+
expire.
6267
face_ids: An array of candidate `face_id`s. All of them are created by
6368
`face.detect` and the `face_id`s will expire in 24 hours after the
6469
detection call. The number of `face_id`s is limited to 1000.
@@ -70,12 +75,13 @@ def find_similars(face_id,
7075
Returns:
7176
An array of the most similar faces represented in `face_id` if the
7277
input parameter is `face_ids` or `persisted_face_id` if the input
73-
parameter is `face_list_id`.
78+
parameter is `face_list_id` or `large_face_list_id`.
7479
"""
7580
url = 'findsimilars'
7681
json = {
7782
'faceId': face_id,
7883
'faceListId': face_list_id,
84+
'largeFaceListId': large_face_list_id,
7985
'faceIds': face_ids,
8086
'maxNumOfCandidatesReturned': max_candidates_return,
8187
'mode': mode,
@@ -104,17 +110,20 @@ def group(face_ids):
104110

105111

106112
def identify(face_ids,
107-
person_group_id,
113+
person_group_id=None,
114+
large_person_group_id=None,
108115
max_candidates_return=1,
109116
threshold=None):
110-
"""Identify unknown faces from a person group.
117+
"""Identify unknown faces from a person group or a large person group.
111118
112119
Args:
113120
face_ids: An array of query `face_id`s, created by the `face.detect`.
114121
Each of the faces are identified independently. The valid number of
115122
`face_ids` is between [1, 10].
116123
person_group_id: `person_group_id` of the target person group, created
117124
by `person_group.create`.
125+
large_person_group_id: `large_person_group_id` of the target large
126+
person group, createdJ by `large_person_group.create`.
118127
max_candidates_return: Optional parameter. The range of
119128
`max_candidates_return` is between 1 and 5 (default is 1).
120129
threshold: Optional parameter. Confidence threshold of identification,
@@ -127,6 +136,7 @@ def identify(face_ids,
127136
url = 'identify'
128137
json = {
129138
'personGroupId': person_group_id,
139+
'largePersonGroupId': large_person_group_id,
130140
'faceIds': face_ids,
131141
'maxNumOfCandidatesReturned': max_candidates_return,
132142
'confidenceThreshold': threshold,
@@ -135,21 +145,27 @@ def identify(face_ids,
135145
return util.request('POST', url, json=json)
136146

137147

138-
def verify(face_id, another_face_id=None, person_group_id=None,
148+
def verify(face_id,
149+
another_face_id=None,
150+
person_group_id=None,
151+
large_person_group_id=None,
139152
person_id=None):
140153
"""Verify whether two faces belong to a same person or whether one face
141154
belongs to a person.
142155
143156
For face to face verification, only `face_id` and `another_face_id` is
144157
necessary. For face to person verification, only `face_id`,
145-
`person_group_id` and `person_id` is needed.
158+
`person_group_id` (or `large_person_group_id`) and `person_id` is needed.
146159
147160
Args:
148161
face_id: `face_id` of one face, comes from `face.detect`.
149162
another_face_id: `face_id` of another face, comes from `face.detect`.
150163
person_group_id: Using existing `person_group_id` and `person_id` for
151164
fast loading a specified person. `person_group_id` is created in
152165
`person_group.create`.
166+
large_person_group_id: Using existing `large_person_group_id` and
167+
`person_id` for fast loading a specified person.
168+
`large_person_group_id` is created in `large_person_group.create`.
153169
person_id: Specify a certain person in a person group. `person_id` is
154170
created in `person.create`.
155171
@@ -167,6 +183,7 @@ def verify(face_id, another_face_id=None, person_group_id=None,
167183
json.update({
168184
'faceId': face_id,
169185
'personGroupId': person_group_id,
186+
'largePersonGroupId': large_person_group_id,
170187
'personId': person_id,
171188
})
172189

cognitive_face/large_face_list.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
File: large_face_list.py
5+
Description: Large Face List section of the Cognitive Face API.
6+
"""
7+
from . import util
8+
9+
10+
def create(large_face_list_id, name=None, user_data=None):
11+
"""Create an empty large face list with user-specified
12+
`large_face_list_id`, `name` and an optional `user_data`.
13+
14+
Args:
15+
large_face_list_id: Valid character is letter in lower case or digit or
16+
'-' or '_', maximum length is 64.
17+
name: Name of the created large face list, maximum length is 128.
18+
user_data: Optional parameter. User-defined data for the large face
19+
list. Length should not exceed 16KB.
20+
21+
Returns:
22+
An empty response body.
23+
"""
24+
name = name or large_face_list_id
25+
url = 'largefacelists/{}'.format(large_face_list_id)
26+
json = {
27+
'name': name,
28+
'userData': user_data,
29+
}
30+
31+
return util.request('PUT', url, json=json)
32+
33+
34+
def delete(large_face_list_id):
35+
"""Delete an existing large face list according to `large_face_list_id`.
36+
Persisted face images in the large face list will also be deleted.
37+
38+
Args:
39+
large_face_list_id: Valid character is letter in lower case or digit or
40+
'-' or '_', maximum length is 64.
41+
42+
Returns:
43+
An empty response body.
44+
"""
45+
url = 'largefacelists/{}'.format(large_face_list_id)
46+
47+
return util.request('DELETE', url)
48+
49+
50+
def get(large_face_list_id):
51+
"""Retrieve a large face list's information, including
52+
`large_face_list_id`, `name`, `user_data`. Large face list simply
53+
represents a list of faces, and could be treated as a searchable data
54+
source in `face.find_similars`.
55+
56+
Args:
57+
large_face_list_id: Valid character is letter in lower case or digit or
58+
'-' or '_', maximum length is 64.
59+
60+
Returns:
61+
The large face list's information.
62+
"""
63+
url = 'largefacelists/{}'.format(large_face_list_id)
64+
65+
return util.request('GET', url)
66+
67+
68+
def get_status(large_face_list_id):
69+
"""Retrieve the training status of a large face list (completed or
70+
ongoing). Training can be triggered by `large_face_list.train`. The
71+
training will process for a while on the server side.
72+
73+
Args:
74+
large_face_list_id: `large_face_list_id` of the target large face list.
75+
76+
Returns:
77+
The large face list's training status.
78+
"""
79+
url = 'largefacelists/{}/training'.format(large_face_list_id)
80+
81+
return util.request('GET', url)
82+
83+
84+
def list(start=None, top=None):
85+
"""Retrieve information about all existing large face lists. Only
86+
`large_face_list_id`, `name` and `user_data` will be returned.
87+
88+
Args:
89+
start: Optional parameter. List large face lists from the least
90+
`large_face_list_id` greater than the "start". It contains no more
91+
than 64 characters. Default is empty.
92+
top: The number of large face lists to list, ranging in [1, 1000].
93+
Default is 1000.
94+
95+
Returns:
96+
An array of large face lists.
97+
"""
98+
url = 'largefacelists'
99+
params = {
100+
'start': start,
101+
'top': top,
102+
}
103+
104+
return util.request('GET', url, params=params)
105+
106+
107+
def train(large_face_list_id):
108+
"""Queue a large face list training task, the training task may not be
109+
started immediately.
110+
111+
Args:
112+
large_face_list_id: Target large face list to be trained.
113+
114+
Returns:
115+
An empty JSON body.
116+
"""
117+
url = 'largefacelists/{}/train'.format(large_face_list_id)
118+
119+
return util.request('POST', url)
120+
121+
122+
def update(large_face_list_id, name=None, user_data=None):
123+
"""Update information of a large face list, including `name` and `user_data`.
124+
125+
Args:
126+
large_face_list_id: Valid character is letter in lower case or digit or
127+
'-' or '_', maximum length is 64.
128+
name: Name of the created large face list, maximum length is 128.
129+
user_data: Optional parameter. User-defined data for the large face
130+
list. Length should not exceed 16KB.
131+
132+
Returns:
133+
An empty response body.
134+
"""
135+
url = 'largefacelists/{}'.format(large_face_list_id)
136+
json = {
137+
'name': name,
138+
'userData': user_data,
139+
}
140+
141+
return util.request('PATCH', url, json=json)
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
File: large_face_list_face.py
5+
Description: Large Face List Face section of the Cognitive Face API.
6+
"""
7+
from . import util
8+
9+
10+
def add(image, large_face_list_id, user_data=None, target_face=None):
11+
"""Add a face to a large face list.
12+
13+
The input face is specified as an image with a `target_face` rectangle. It
14+
returns a `persisted_face_id` representing the added face, and
15+
`persisted_face_id` will not expire.
16+
17+
Args:
18+
image: A URL or a file path or a file-like object represents an image.
19+
large_face_list_id: Valid character is letter in lower case or digit or
20+
'-' or '_', maximum length is 64.
21+
user_data: Optional parameter. User-specified data about the large face
22+
list for any purpose. The maximum length is 1KB.
23+
target_face: Optional parameter. A face rectangle to specify the target
24+
face to be added into the large face list, in the format of
25+
"left,top,width,height". E.g. "10,10,100,100". If there are more
26+
than one faces in the image, `target_face` is required to specify
27+
which face to add. No `target_face` means there is only one face
28+
detected in the entire image.
29+
30+
Returns:
31+
A new `persisted_face_id`.
32+
"""
33+
url = 'largefacelists/{}/persistedFaces'.format(large_face_list_id)
34+
headers, data, json = util.parse_image(image)
35+
params = {
36+
'userData': user_data,
37+
'targetFace': target_face,
38+
}
39+
40+
return util.request(
41+
'POST', url, headers=headers, params=params, json=json, data=data)
42+
43+
44+
def delete(large_face_list_id, persisted_face_id):
45+
"""Delete an existing face from a large face list (given by a
46+
`persisted_face_id` and a `large_face_list_id`). Persisted image related to
47+
the face will also be deleted.
48+
49+
Args:
50+
large_face_list_id: Valid character is letter in lower case or digit or
51+
'-' or '_', maximum length is 64.
52+
persisted_face_id: `persisted_face_id` of an existing face. Valid
53+
character is letter in lower case or digit or '-' or '_', maximum
54+
length is 64.
55+
56+
Returns:
57+
An empty response body.
58+
"""
59+
url = 'largefacelists/{}/persistedFaces/{}'.format(large_face_list_id,
60+
persisted_face_id)
61+
62+
return util.request('DELETE', url)
63+
64+
65+
def get(large_face_list_id, persisted_face_id):
66+
"""Retrieve information about a persisted face (specified by
67+
`persisted_face_id` and a `large_face_list_id`).
68+
69+
Args:
70+
large_face_list_id: Valid character is letter in lower case or digit or
71+
'-' or '_', maximum length is 64.
72+
persisted_face_id: `persisted_face_id` of an existing face. Valid
73+
character is letter in lower case or digit or '-' or '_', maximum
74+
length is 64.
75+
76+
Returns:
77+
The target persisted face's information (`persisted_face_id` and
78+
`user_data`).
79+
"""
80+
url = 'largefacelists/{}/persistedFaces/{}'.format(large_face_list_id,
81+
persisted_face_id)
82+
83+
return util.request('GET', url)
84+
85+
86+
def list(large_face_list_id, start=None, top=None):
87+
"""Retrieve information (`persisted_face_id` and `user_data`) about
88+
existing persisted faces in a large face list.
89+
90+
Args:
91+
start: Optional parameter. List large face lists from the least
92+
`large_face_list_id` greater than the "start". It contains no more
93+
than 64 characters. Default is empty.
94+
top: The number of large face lists to list, ranging in [1, 1000].
95+
Default is 1000.
96+
97+
Returns:
98+
An array of persisted faces and their information (`persisted_face_id`
99+
and `user_data`).
100+
"""
101+
url = 'largefacelists/{}/persistedFaces'.format(large_face_list_id)
102+
params = {
103+
'start': start,
104+
'top': top,
105+
}
106+
107+
return util.request('GET', url, params=params)
108+
109+
110+
def update(large_face_list_id, persisted_face_id, user_data=None):
111+
"""Update a persisted face's `user_data` field in a large face list.
112+
113+
Args:
114+
large_face_list_id: largeFaceListId of an existing large face list.
115+
person.
116+
person_id: `person_id` of the target person.
117+
persisted_face_id: `persisted_face_id` of the target face, which is
118+
persisted and will not expire.
119+
user_data: Optional parameter. Attach `user_data` to person's
120+
persisted face. The size limit is 1KB.
121+
122+
Returns:
123+
An empty response body.
124+
"""
125+
url = 'largefacelists/{}/persistedFaces/{}'.format(large_face_list_id,
126+
persisted_face_id)
127+
json = {
128+
'userData': user_data,
129+
}
130+
131+
return util.request('PATCH', url, json=json)

0 commit comments

Comments
 (0)