Skip to content

Commit

Permalink
small updates to microsoft transformers after PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
qmac committed Feb 12, 2018
1 parent ad67191 commit 8bd812a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
16 changes: 9 additions & 7 deletions pliers/converters/microsoft.py
Expand Up @@ -10,21 +10,23 @@ class MicrosoftAPITextConverter(MicrosoftVisionAPITransformer, ImageToTextConver
''' Detects text within images using the Microsoft Vision API. '''

api_method = 'ocr'
_log_attributes = ('api_version', 'language')

def __init__(self, language='en', **kwargs):
self.language = language
super(MicrosoftAPITextConverter, self).__init__(**kwargs)

def _convert(self, stim):
params = {
'language': 'en',
'language': self.language,
'detectOrientation': False
}
response = self._query_api(stim, params)

text = ''
lines = []
for r in response['regions']:
for l in r['lines']:
for w in l['words']:
if text:
text += ' ' + w['text']
else:
text += w['text']
lines.append(' '.join([w['text'] for w in l['words']]))

text = '\n'.join(lines)
return TextStim(text=text, onset=stim.onset, duration=stim.duration)
2 changes: 1 addition & 1 deletion pliers/tests/converters/test_microsoft_converters.py
Expand Up @@ -17,4 +17,4 @@ def test_microsoft_vision_api_text_converter():
conv = MicrosoftAPITextConverter()
img = ImageStim(join(IMAGE_DIR, 'CC0', '28010844841_c5b81cb9cc_z.jpg'))
text = conv.transform(img).text
assert 'Santander' in text
assert 'Santander\nSantander' in text
9 changes: 8 additions & 1 deletion pliers/transformers/microsoft.py
Expand Up @@ -16,6 +16,9 @@ class MicrosoftAPITransformer(Transformer):
Services. Only needs to be passed the first time the extractor is
initialized.
location (str): region the subscription key has been registered in.
It will be the first part of the endpoint URL suggested by
Microsoft when you first created the key.
Examples include: westus, westcentralus, eastus
api_version (str): API version to use.
'''

Expand Down Expand Up @@ -49,7 +52,8 @@ def __init__(self, subscription_key=None, location=None,

def _query_api(self, stim, params):
with stim.get_filename() as filename:
data = open(filename, 'rb').read()
with open(filename, 'rb') as fp:
data = fp.read()

headers = {
'Content-Type': 'application/octet-stream',
Expand All @@ -70,6 +74,9 @@ def _query_api(self, stim, params):
raise Exception(response['error']['message'])
elif 'statusCode' in response and response['statusCode'] in [401, 429]:
raise Exception(response['message'])
elif 'code' in response and \
response['code'] == 'NotSupportedVisualFeature':
raise Exception(response['message'])

return response

Expand Down

0 comments on commit 8bd812a

Please sign in to comment.