* Code format.

* Add logs in test init and cleanup.

* Typo fix.
This commit is contained in:
Xuan Hu (Sean) 2017-09-19 16:58:02 +08:00 коммит произвёл GitHub
Родитель b5de7ccb18
Коммит dd28822b04
9 изменённых файлов: 53 добавлений и 50 удалений

Просмотреть файл

@ -37,12 +37,15 @@ def detect(image, face_id=True, landmarks=False, attributes=''):
'returnFaceAttributes': attributes,
}
return util.request('POST', url, headers=headers, params=params, json=json,
data=data)
return util.request(
'POST', url, headers=headers, params=params, json=json, data=data)
def find_similars(face_id, face_list_id=None, face_ids=None,
max_candidates_return=20, mode='matchPerson'):
def find_similars(face_id,
face_list_id=None,
face_ids=None,
max_candidates_return=20,
mode='matchPerson'):
"""Given query face's `face_id`, to search the similar-looking faces from a
`face_id` array or a `face_list_id`.
@ -100,7 +103,9 @@ def group(face_ids):
return util.request('POST', url, json=json)
def identify(face_ids, person_group_id, max_candidates_return=1,
def identify(face_ids,
person_group_id,
max_candidates_return=1,
threshold=None):
"""Identify unknown faces from a person group.

Просмотреть файл

@ -38,8 +38,8 @@ def add_face(image, face_list_id, user_data=None, target_face=None):
'targetFace': target_face,
}
return util.request('POST', url, headers=headers, params=params, json=json,
data=data)
return util.request(
'POST', url, headers=headers, params=params, json=json, data=data)
def create(face_list_id, name=None, user_data=None):
@ -57,7 +57,7 @@ def create(face_list_id, name=None, user_data=None):
Returns:
An empty response body.
"""
name = face_list_id if name is None else name
name = name or face_list_id
url = 'facelists/{}'.format(face_list_id)
json = {
'name': name,
@ -68,23 +68,22 @@ def create(face_list_id, name=None, user_data=None):
def delete_face(face_list_id, persisted_face_id):
"""Delete an existing face from a face list (given by a
`persisited_face_id` and a `face_list_id`). Persisted image related to the
face will also be deleted.
"""Delete an existing face from a face list (given by a `persisted_face_id`
and a `face_list_id`). Persisted image related to the face will also be
deleted.
Args:
face_list_id: Valid character is letter in lower case or digit or '-'
or '_', maximum length is 64.
persisited_face_id: `persisited_face_id` of an existing face. Valid
persisted_face_id: `persisted_face_id` of an existing face. Valid
character is letter in lower case or digit or '-' or '_', maximum
length is 64.
Returns:
An empty response body.
"""
url = 'facelists/{}/persistedFaces/{}'.format(
face_list_id, persisted_face_id
)
url = 'facelists/{}/persistedFaces/{}'.format(face_list_id,
persisted_face_id)
return util.request('DELETE', url)

Просмотреть файл

@ -7,7 +7,10 @@ Description: Person section of the Cognitive Face API.
from . import util
def add_face(image, person_group_id, person_id, user_data=None,
def add_face(image,
person_group_id,
person_id,
user_data=None,
target_face=None):
"""Add a representative face to a person for identification. The input face
is specified as an image with a `target_face` rectangle. It returns a
@ -40,8 +43,8 @@ def add_face(image, person_group_id, person_id, user_data=None,
'targetFace': target_face,
}
return util.request('POST', url, headers=headers, params=params, json=json,
data=data)
return util.request(
'POST', url, headers=headers, params=params, json=json, data=data)
def create(person_group_id, name, user_data=None):
@ -100,8 +103,7 @@ def delete_face(person_group_id, person_id, persisted_face_id):
An empty response body.
"""
url = 'persongroups/{}/persons/{}/persistedFaces/{}'.format(
person_group_id, person_id, persisted_face_id
)
person_group_id, person_id, persisted_face_id)
return util.request('DELETE', url)
@ -139,8 +141,7 @@ def get_face(person_group_id, person_id, persisted_face_id):
`user_data`).
"""
url = 'persongroups/{}/persons/{}/persistedFaces/{}'.format(
person_group_id, person_id, persisted_face_id
)
person_group_id, person_id, persisted_face_id)
return util.request('GET', url)
@ -148,7 +149,7 @@ def get_face(person_group_id, person_id, persisted_face_id):
def lists(person_group_id, start=None, top=None):
"""List `top` persons in a person group with `person_id` greater than
`start`, and retrieve person information (including `person_id`, `name`,
`user_data` and `persisited_face_ids` of registered faces of the person).
`user_data` and `persisted_face_ids` of registered faces of the person).
Args:
person_group_id: `person_group_id` of the target person group.
@ -207,8 +208,7 @@ def update_face(person_group_id, person_id, persisted_face_id, user_data=None):
An empty response body.
"""
url = 'persongroups/{}/persons/{}/persistedFaces/{}'.format(
person_group_id, person_id, persisted_face_id
)
person_group_id, person_id, persisted_face_id)
json = {
'userData': user_data,
}

Просмотреть файл

@ -22,7 +22,7 @@ def create(person_group_id, name=None, user_data=None):
Returns:
An empty response body.
"""
name = person_group_id if name is None else name
name = name or person_group_id
url = 'persongroups/{}'.format(person_group_id)
json = {
'name': name,

Просмотреть файл

@ -26,11 +26,13 @@ def setUpModule():
- Set Base URL.
- Setup needed data for unitests.
"""
print("setUpModule Begin.")
CF.Key.set(config.KEY)
CF.BaseUrl.set(config.BASE_URL)
util.DataStore.setup_person_group()
util.DataStore.setup_face_list()
util.DataStore.setup_face()
print("setUpModule End.")
def tearDownModule():
@ -39,5 +41,7 @@ def tearDownModule():
- Remove all the created persisted data.
"""
print("tearDownModule Begin.")
CF.util.clear_face_lists()
CF.util.clear_person_groups()
print("tearDownModule End.")

Просмотреть файл

@ -26,11 +26,11 @@ class TestFaceList(unittest.TestCase):
self.assertIsInstance(res, dict)
util.wait()
persisited_face_id = res['persistedFaceId']
persisted_face_id = res['persistedFaceId']
res = CF.face_list.delete_face(
util.DataStore.face_list_id,
persisited_face_id,
persisted_face_id,
)
print(res)
self.assertIsInstance(res, dict)

Просмотреть файл

@ -31,12 +31,12 @@ class TestPerson(unittest.TestCase):
self.assertIsInstance(res, dict)
util.wait()
persisited_face_id = res['persistedFaceId']
persisted_face_id = res['persistedFaceId']
res = CF.person.update_face(
util.DataStore.person_group_id,
util.DataStore.person_id['Dad'],
persisited_face_id,
persisted_face_id,
'TempUserData',
)
print(res)
@ -46,7 +46,7 @@ class TestPerson(unittest.TestCase):
res = CF.person.delete_face(
util.DataStore.person_group_id,
util.DataStore.person_id['Dad'],
persisited_face_id,
persisted_face_id,
)
print(res)
self.assertIsInstance(res, dict)

Просмотреть файл

@ -24,6 +24,7 @@ class CognitiveFaceException(Exception):
code: error code.
msg: error message.
"""
def __init__(self, status_code, code, msg):
super(CognitiveFaceException, self).__init__()
self.status_code = status_code
@ -31,12 +32,11 @@ class CognitiveFaceException(Exception):
self.msg = msg
def __str__(self):
return (
'Error when calling Cognitive Face API:\n'
'\tstatus_code: {}\n'
'\tcode: {}\n'
'\tmessage: {}\n'
).format(self.status_code, self.code, self.msg)
return ('Error when calling Cognitive Face API:\n'
'\tstatus_code: {}\n'
'\tcode: {}\n'
'\tmessage: {}\n').format(self.status_code, self.code,
self.msg)
class Key(object):
@ -56,7 +56,6 @@ class Key(object):
class BaseUrl(object):
@classmethod
def set(cls, base_url):
cls.base_url = base_url
@ -82,8 +81,8 @@ def request(method, url, data=None, json=None, headers=None, params=None):
headers['Content-Type'] = 'application/json'
headers['Ocp-Apim-Subscription-Key'] = Key.get()
response = requests.request(method, url, params=params, data=data,
json=json, headers=headers)
response = requests.request(
method, url, params=params, data=data, json=json, headers=headers)
# Handle result and raise custom exception when something wrong.
result = None
@ -92,14 +91,11 @@ def request(method, url, data=None, json=None, headers=None, params=None):
try:
error_msg = response.json()['error']
except:
raise CognitiveFaceException(
response.status_code,
response.status_code,
response.text)
raise CognitiveFaceException(
response.status_code,
error_msg.get('code'),
error_msg.get('message'))
raise CognitiveFaceException(response.status_code,
response.status_code, response.text)
raise CognitiveFaceException(response.status_code,
error_msg.get('code'),
error_msg.get('message'))
# Prevent `response.json()` complains about empty response.
if response.text:

Просмотреть файл

@ -37,5 +37,4 @@ setup(
'Topic :: Scientific/Engineering :: Image Recognition',
],
test_suite='nose.collector',
tests_require=['nose'],
)
tests_require=['nose'])