Skip to content
This repository was archived by the owner on Nov 8, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import cognitive_face as CF
KEY = 'subscription key' # Replace with a valid Subscription Key here.
CF.Key.set(KEY)

BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/' # Replace with your regional Base URL
CF.BaseUrl.set(BASE_URL)

img_url = 'https://raw.githubusercontent.com/Microsoft/Cognitive-Face-Windows/master/Data/detection1.jpg'
result = CF.face.detect(img_url)
print result
Expand Down
1 change: 1 addition & 0 deletions cognitive_face/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
from . import util
from .util import CognitiveFaceException
from .util import Key
from .util import BaseUrl
2 changes: 2 additions & 0 deletions cognitive_face/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ def setUpModule():
"""Setup for the whole unitests.

- Set Subscription Key.
- Set Base URL.
- Setup needed data for unitests.
"""
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()
Expand Down
4 changes: 4 additions & 0 deletions cognitive_face/tests/config.sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
# Subscription Key for calling the Cognitive Face API.
KEY = ''

# Base URL for calling the Cognitive Face API.
# default is 'https://westus.api.cognitive.microsoft.com/face/v1.0/'
BASE_URL = ''

# Time (in seconds) for sleep between each call to avoid exceeding quota.
# Default to 3 as free subscription have limit of 20 calls per minute.
TIME_SLEEP = 3
23 changes: 18 additions & 5 deletions cognitive_face/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@

import cognitive_face as CF

_BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of setting/getting Region, what about making it more general to set/get BaseUrl directly?

DEFAULT_BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/'

class BaseUrl(object):

@classmethod
def set(cls, base_url):
cls.base_url = base_url

@classmethod
def get(cls):
if not hasattr(cls, 'base_url'):
cls.base_url = DEFAULT_BASE_URL
return cls.base_url

TIME_SLEEP = 1


Expand Down Expand Up @@ -58,9 +71,9 @@ def request(method, url, data=None, json=None, headers=None, params=None):
# pylint: disable=too-many-arguments
"""Universal interface for request."""

# Make it possible to call only with short name (without _BASE_URL).
# Make it possible to call only with short name (without BaseUrl).
if not url.startswith('https://'):
url = _BASE_URL + url
url = BaseUrl.get() + url

# Setup the headers with default Content-Type and Subscription Key.
headers = headers or {}
Expand All @@ -87,7 +100,7 @@ def request(method, url, data=None, json=None, headers=None, params=None):
error_msg.get('code'),
error_msg.get('message'))

# Prevent `reponse.json()` complains about empty response.
# Prevent `response.json()` complains about empty response.
if response.text:
result = response.json()
else:
Expand Down Expand Up @@ -117,7 +130,7 @@ def parse_image(image):
headers = {'Content-Type': 'application/octet-stream'}
data = open(image, 'rb').read()
return headers, data, None
else: # Defailt treat it as a URL (string).
else: # Default treat it as a URL (string).
headers = {'Content-Type': 'application/json'}
json = {'url': image}
return headers, None, json
Expand Down