class Clarifai(): def __init__(self, api_id, api_secret): self.api_id = api_id self.api_secret = api_secret self.app = ClarifaiApp(self.api_id, self.api_secret) def tag(self, filenames): raw_tags = self.app.tag_files(filenames) res = {} if raw_tags['status']['description'] == 'Ok': tagged_files = raw_tags['outputs'] for index, f in enumerate(tagged_files): tags = f['data']['concepts'] tag_dict = {} for tag in tags: tag_dict[tag['name']] = tag['value'] filename = filenames[index] res[filename] = tag_dict return res def tag_raw(self, files): return self.app.tag_files(files) def test(self): res = self.app.tag_urls(['https://samples.clarifai.com/metro-north.jpg']) if res['status']['description'] == 'Ok': concepts = res['outputs'][0]['data']['concepts'] for concept in concepts: print(concept['name'], concept['value'])
def doStuffWithURL(imageURL): print 'ID: ' + cnf.ConfigVars['clarifai_id'] print 'SECRET: ' + cnf.ConfigVars['clarifai_secret'] app = ClarifaiApp(cnf.ConfigVars['clarifai_id'], cnf.ConfigVars['clarifai_secret']) os.environ['CLARIFAI_APP_ID'] = cnf.ConfigVars['clarifai_id'] os.environ['CLARIFAI_APP_SECRET'] = cnf.ConfigVars['clarifai_secret'] response = app.tag_files([imageURL]) return response
def getConceptsinImage(imageinput): CLARIFAI_APP_ID = 'Re11ornY9K5Hd06foa9uCknPoaEY9y0w0MpHcGTa' CLARIFAI_APP_SECRET = 'XMEAHfXNyfA8TM1x76F1jQKJWcl7zBLRdFJuIdSN' app = ClarifaiApp(app_id=CLARIFAI_APP_ID, app_secret=CLARIFAI_APP_SECRET) model = app.models.get('general-v1.3') res = app.tag_files([imageinput]) probconcept = {} for output in res['outputs']: for data in output['data']['concepts']: probconcept[data['name']] = data['value'] return probconcept
#!/usr/bin/env python # -*- coding: utf-8 -*- from clarifai.rest import ClarifaiApp import sys import json # this program target for clarifai's simple general model predict app = ClarifaiApp() del sys.argv[0] ret = app.tag_files(sys.argv) print json.dumps(ret, indent=4, sort_keys=True)
def store_csv(csv_input): with open(csv_file_name, 'a') as csvfile: csv_writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) try: csv_writer.writerow(csv_input) except UnicodeEncodeError: csv_writer.writerow(["ERROR"]) imgnames = sorted(glob.glob("images/*.png")) for i in imgnames: out = app.tag_files([i]) json_str = json.dumps(out) data = json.loads(json_str) concepts = data['outputs'][0]['data']['concepts'] url = data['outputs'][0]['input']['data']['image']['url'] query = i all_labels = '' for i in concepts: name = str(i['name']) value = str(i['value']) all_labels += name + ' , ' + value + ', ' csv_response = [url, all_labels] store_csv(csv_response)