Skip to content

Commit 67e6d91

Browse files
author
Vitor Pedro
committed
feat: add duplicate_check flag when adding person to group
1 parent 1a1d806 commit 67e6d91

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

tests/test_face.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
__image_file = './sample/detection1.jpg'
2020
__person_id = "person1"
21+
__person_id_duplicate = "person1_duplicate"
2122
__template: str
2223

2324
__group_id_async = "test_face_sdk_py_group_async"
@@ -298,6 +299,39 @@ def test_group_add_person(use_async: bool, group_id: str, loop: asyncio.Abstract
298299
assert False
299300

300301

302+
@pytest.mark.parametrize('use_async, group_id', [
303+
(True, __group_id_async),
304+
(False, __group_id)
305+
])
306+
def test_group_add_duplicate_person(use_async: bool, group_id: str, loop: asyncio.AbstractEventLoop):
307+
"""
308+
Test sync and async group add_person request with duplicate check.
309+
:param use_async: flag to use the async function
310+
:param group_id: group identifier
311+
:param loop: event loop for the current test
312+
:return:
313+
Passes if YoonikApiException is raised.
314+
"""
315+
with pytest.raises(YoonikApiException) as exception:
316+
if use_async:
317+
loop.run_until_complete(
318+
YKF.group.add_person_async(
319+
group_id=group_id,
320+
person_id=__person_id_duplicate,
321+
face_template=__template,
322+
duplicate_check=True
323+
)
324+
)
325+
else:
326+
YKF.group.add_person(
327+
group_id=group_id,
328+
person_id=__person_id_duplicate,
329+
face_template=__template,
330+
duplicate_check=True
331+
)
332+
333+
assert exception.value.status_code == 409
334+
301335
# @pytest.mark.parametrize('use_async, group_id', [
302336
# (True, 'invalid_group1'),
303337
# (False, 'invalid_group3')

yk_face/group.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,16 @@ async def list_ids_async(group_id: str) -> List[str]:
9191
return await request_async('GET', url)
9292

9393

94-
def add_person(group_id: str, person_id: str, face_template: str):
94+
def add_person(group_id: str, person_id: str, face_template: str, duplicate_check=False):
9595
"""Add a person to a group.
9696
:param group_id:
9797
ID of the group. `group_id` is created in `group.create`.
9898
:param person_id:
9999
Person ID.
100100
:param face_template:
101101
Biometric template to be associated with the provided `person_id` (obtained from `face.process`).
102+
:param duplicate_check:
103+
Set True to check if a template already exists with a different person_id.
102104
:return:
103105
"""
104106
if group_id is None:
@@ -107,11 +109,12 @@ def add_person(group_id: str, person_id: str, face_template: str):
107109
raise ValueError("Person ID must be specified.")
108110

109111
url = f'gallery/{group_id}/{person_id}'
110-
template_request = Template(template=face_template).model_dump(mode='json')
112+
template_request = Template(
113+
template=face_template, duplicate_check=duplicate_check).model_dump(mode='json')
111114
request('POST', url, json=template_request)
112115

113116

114-
async def add_person_async(group_id: str, person_id: str, face_template: str):
117+
async def add_person_async(group_id: str, person_id: str, face_template: str, duplicate_check=False):
115118
"""
116119
Add a person to a group.
117120
Performs the request asynchronously.
@@ -121,6 +124,8 @@ async def add_person_async(group_id: str, person_id: str, face_template: str):
121124
Person ID.
122125
:param face_template:
123126
Biometric template to be associated with the provided `person_id` (obtained from `face.process`).
127+
:param duplicate_check:
128+
Set True to check if a template already exists with a different person_id.
124129
:return:
125130
"""
126131
if group_id is None:
@@ -129,7 +134,8 @@ async def add_person_async(group_id: str, person_id: str, face_template: str):
129134
raise ValueError("Person ID must be specified.")
130135

131136
url = f'gallery/{group_id}/{person_id}'
132-
template_request = Template(template=face_template).model_dump(mode='json')
137+
template_request = Template(
138+
template=face_template, duplicate_check=duplicate_check).model_dump(mode='json')
133139
await request_async('POST', url, json=template_request)
134140

135141

0 commit comments

Comments
 (0)