Skip to content

Commit

Permalink
Clarifai support for selectable and multiple models. Issue #7.
Browse files Browse the repository at this point in the history
  • Loading branch information
28mm committed Feb 28, 2017
1 parent 6b0a447 commit 5332f5e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 10 deletions.
62 changes: 52 additions & 10 deletions bin/fovea.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ def main():
# Take one or more files (FIXME: reporting for multiple files)
parser.add_argument('files', nargs='+')

# Take one or more models (FIXME: Clarifai only for now)
parser.add_argument('--model', action='append', dest='models', default=[])

args = parser.parse_args()

# If no `flags' are set, default to --labels.
Expand Down Expand Up @@ -214,7 +217,8 @@ def main():
CLARIFAI_CLIENT_SECRET,
access_token=CLARIFAI_ACCESS_TOKEN,
labels=args.labels,
faces=args.faces)
faces=args.faces,
models=args.models)

elif args.provider == 'facebook':
raise
Expand Down Expand Up @@ -830,15 +834,29 @@ def tabular(self, confidence=0.0):

class Clarifai(Query):

# dict generated with .../utilities/clarifai/models.py
models = {
'general-v1.3' : 'aaa03c23b3724a16a56b629203edc62c',
'food-items-v1.0' : 'ecf02b31ad884d6785a9bd950f8e986c',
'apparel' : 'e0be3b9d6a454f0493ac3a30784001ff',
'weddings-v1.0' : 'c386b7a870114f4a87477c0824499348',
'travel-v1.0' : 'eee28c313d69466f836ab83287a54ed9',
'celeb-v1.3' : 'e466caa0619f444ab97497640cefc4dc',
'color' : 'eeed0b6733a644cea07cf4c60f87ebb7',
'nsfw-v1.0' : 'e9576d86d2004ed1a38ba0cf39ecb4b1'
}

def __init__(self, image, client_id, client_secret, access_token="",
labels=True, faces=False):
labels=True, faces=False, models=[]):

self.client_id = client_id
self.client_secret = client_secret
self.access_token = access_token
self.image = image
self._labels = labels
self._faces = faces
self._json = None
self.image = image
self._labels = labels
self._faces = faces
self._json = None
self._models = [ 'general-v1.3' ] if models is [] else models

# Use OpenCV to obtain image dimensions.
# Clarifai reports face bounding boxes as ratios
Expand All @@ -852,6 +870,7 @@ def __init__(self, image, client_id, client_secret, access_token="",
def run(self):

self._json = {}

headers = { 'Authorization' : 'Bearer ' + self.access_token,
'Content-Type' : 'application/json' }

Expand All @@ -865,17 +884,40 @@ def run(self):
]
}

if self._labels:
# FIXME find model id mappings. This is general 1.3?
#if self._labels and 'general-v1.3' not in self._models:
# self._models.append('general-v1.3')

for model in self._models:
model_id = self.models[model]

url = 'https://api.clarifai.com/v2/models/'\
+ 'aaa03c23b3724a16a56b629203edc62c/outputs'
+ model_id + '/outputs'

response = requests.post(url,
headers=headers,
data=json.dumps(data))

response_json = json.loads(response.text)
self._json['labels'] = response_json

# If we've run a second model, merge new concepts
if not self._json:
self._json['labels'] = response_json
else:
old_concepts = self._json['labels']['outputs'][0]['data']['concepts']
new_concepts = response_json['outputs'][0]['data']['concepts']

for c in new_concepts:
name = c['name']
value = c['value']
match = False

for oc in old_concepts:
if oc['name'] == name:
oc['value'] = value if value > oc['value'] \
else oc['value']
match = True
if match is False:
old_concepts.append(c)

if self._faces:

Expand Down
19 changes: 19 additions & 0 deletions utilities/clarifai/clarifai-models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python3

import os
import clarifai
from clarifai.rest import ClarifaiApp as app

def main():
a = app(app_id=os.environ['CLARIFAI_CLIENT_ID'],
app_secret=os.environ['CLARIFAI_CLIENT_SECRET'])
models = a.models.get_all()
for m in models:
info = m.get_info()
model = info['model']
print(model['name'] + '\t' \
+ model['id'] + '\t' \
+ model['created_at'])

if __name__ == '__main__': main()

12 changes: 12 additions & 0 deletions utilities/clarifai/clarify-token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python3

import os
import clarifai
from clarifai.rest import ApiClient as app

def main():
a = app(app_id=os.environ['CLARIFAI_CLIENT_ID'],
app_secret=os.environ['CLARIFAI_CLIENT_SECRET'])
print(a.get_token()['access_token'])

if __name__ == '__main__': main()
19 changes: 19 additions & 0 deletions utilities/clarifai/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python3

import os
import clarifai
from clarifai.rest import ClarifaiApp as app

def main():
a = app(app_id=os.environ['CLARIFAI_CLIENT_ID'],
app_secret=os.environ['CLARIFAI_CLIENT_SECRET'])
models = a.models.get_all()
for m in models:
info = m.get_info()
model = info['model']
print(model['name'] + '\t' \
+ model['id'] + '\t' \
+ model['created_at'])

if __name__ == '__main__': main()

0 comments on commit 5332f5e

Please sign in to comment.