def getTags(file): # assumes environment variables are set. clarifai_api = ClarifaiApi() result = clarifai_api.tag_images(file) #parsing Json res = [] return result['results'][0]['result']['tag']['classes']
def get_tags(): #TODO: Error checking global d d = {} clarifai_api = ClarifaiApi() blob_service = BlobService('calhacks', 'mm7EmY+T+MGahePBDSDU5LHpZR5tRXuh4MSco4jFrzHovOPEf06e18c89pxtPIo4NDVhhjSeaQY/FQmKNxjjyA==') blob_name = request.data blob_name = blob_name.decode('utf-8') blob_service.get_blob_to_path('imagestore', blob_name, 'out.png') print("checkpoint 1") i = open ('out.png', 'r') strd = "" for line in i: strd += line.strip() fname = 'img.png' with open (fname, 'wb') as f: f.write (base64.b64decode(strd)) f.close() f = open (fname, 'rb') result = clarifai_api.tag_images(f) print(result) st = result['results'][0]['result']['tag']['classes'][0:6] for i in ['food', 'nobody', 'still life', 'meal', 'dish', 'plate', 'delicious', 'isolated', 'cutout', 'unhealthy', 'one', 'background']: while i in st: st.remove(i) d = {blob_name: search_terms(st)} return "success!"
def process_data(self, url): url = url.decode('utf-8') annotations_filename = self.get_model_filename(url) model_filename = get_cache_filename(url) pdf_filename = model_filename + '.data' images_dir = pdf_filename + '-images/' clarifai_api = ClarifaiApi() annotations = self.get_new_model() for file in os.listdir(images_dir): if file.endswith(".png"): response = clarifai_api.tag_images(open(images_dir + file, 'rb')) result = response['results'][0]['result']['tag'] for tag, prob in zip(result['classes'], result['probs']): print('Tag: ' + tag + ' Prob: ' + str(prob)) annotation = create_annotation( (namespaces.oa.confidence, Literal(prob, datatype=XSD.decimal)), target=URIRef(url) + '#image' + file, body=Literal(tag, datatype=XSD.string), annotator=Literal('Clarif.ai', datatype=XSD.string)) annotations += annotation self.write_and_merge_model(annotations, annotations_filename)
def test_unicode_urls(self): image_url = u'http://www.alvaronoboa.com/wp-content/uploads/2013/02/Álvaro-Noboa-y-Annabella-Azín-Votaciones-41-1024x682.jpg' api = ClarifaiApi() response = api.tag_image_urls(image_url) self.assertTrue(response) self.assertTrue(response['results'][0]['url'] == image_url)
def test_embed_one_image_from_localfs(self): image_file = 'tests/data/toddler-flowers.jpeg' api = ClarifaiApi() if os.path.exists(image_file): with open(image_file, 'rb') as fb: response = api.embed_images(fb) self.assertTrue(response)
def main(argv): if len(argv) > 1: imageurl = argv[1] else: imageurl = 'http://clarifai-img.s3.amazonaws.com/test/toddler-flowers.jpeg' print("We will be using the default since you provided no local directories") api = ClarifaiApi() if imageurl.startswith('http'): response = api.tag_image_urls(imageurl) elif os.path.isdir(imageurl): tags_list = tag_images_in_directory(imageurl, api) # this is a list of lists # you get simply something like ['hot', 'girl', 'model', 'young', 'brunette'] # so take that, join it as one string with _ between each word # and rename each file accordingly. response = rename_images_in_directory_with_tags(format_tags_with_filenames(tags_list, images), imageurl) elif os.path.isfile(imageurl): with open(imageurl,'rb') as image_file: response = api.tag_images(image_file) else: raise Exception("Must input url, directory path, or file path")
def test_unicode_urls(self): image_url = u"http://www.alvaronoboa.com/wp-content/uploads/2013/02/Álvaro-Noboa-y-Annabella-Azín-Votaciones-41-1024x682.jpg" api = ClarifaiApi() response = api.tag_image_urls(image_url) self.assertTrue(response) self.assertTrue(response["results"][0]["url"] == image_url)
def find_type(image_name): clarifai_api = ClarifaiApi('ph4Sk2DtEk0Et_9aRUxdzxLPzMpMjv75WlFPnT5H', 'kg_cH9uYZHYikRSrjv7izSRA1fgxCORKryLe8Mn6') # assumes environment variables are set. road_classes = ['vehicle', 'road', 'car', 'transportation system', 'truck', 'street', 'ambulance', 'storm'] common_classes = ['calamity', 'battle', 'accident', 'offense', 'police'] fire_classes = ['flame', 'smoke', 'burnt', 'light', 'energy', 'fire_truck', 'heat', 'explosion'] clf = clarifai_api.tag_images(open(image_name, 'rb')) img_classes = clf['results'][0]['result']['tag']['classes'] if len(img_classes) == 0: return 'Failed' common_score = 0 road_score = 0 fire_score = 0 for cl in img_classes: if cl in common_classes: common_score += 1 if cl in road_classes: road_score += 1 if cl in fire_classes: fire_score += 1 res = {'fire': fire_score, 'road': road_score, 'fine': common_score} print res if (res['road'] >= 3 and res['road'] > res['fire']): return 'road' if (res['fire'] > 1): print res['fire'] return 'fire' if (res['fine'] <= 1): return 'fine' else: return 'police' #print find_type('1.jpg')
def getImage(): image = request.args.get('urlname') if image: clarifai_api = ClarifaiApi(CLARIFAI_APP_ID, CLARIFAI_APP_SECRET) try: response = clarifai_api.tag_image_urls('%s' %(str(image))) tags = response['results'][0]['result']['tag']['classes'] for tag in tags: joke = searchForJoke(jokes, tag) if joke != "No joke with keyword found!": break try: page = wikipedia.page(tags[0]) except wikipedia.exceptions.DisambiguationError as e: page = e.options[0] return jsonify(result=str(tags).strip("[").strip("]"), joke="Possibly inappro Chuck Norris Joke: " + joke, wiki="Fun fact: " + page.summary + "...", readMore="Read more here: " + page.url) except: return jsonify(result="API keys failed!", joke="No image!", wiki="No image!", readMore="No image!") return jsonify(result="No input or failed to locate image!", joke="No image!", wiki="No image!", readMore="No image!")
def getTags(item): clarifai_api = ClarifaiApi() # assumes environment variables are set. result = clarifai_api.tag_image_urls(item.imgURL) result = result["results"][0]["result"]["tag"]["classes"] for i in result: Tag(tagText=i, item=item).save()
def send_image(): image = Images.query.filter_by(is_sent=0).first() if image == None: parameters = { "format" : "xml", "results_per_page" : "100" } result = requests.get('http://thecatapi.com/api/images/get', params=parameters) tree = html.document_fromstring(result.text) clarifai_api = ClarifaiApi() for val in tree.xpath('//images'): for elem in val: try: result = clarifai_api.tag_image_urls(elem.xpath('url')[0].text) total_results = result['results'][0]['result']['tag']['classes'] if 'cat' in total_results or 'kitten' in total_results: db.session.add(Images(elem.xpath('url')[0].text, elem.xpath('id')[0].text, elem.xpath('source_url')[0].text, 0)) db.session.commit() except: pass image = Images.query.filter_by(is_sent=0).first() users = Users.query.filter_by(active=1).all() for elem in users: client = TwilioRestClient(os.environ.get('TWILIO_ACCOUNT_SID'), os.environ.get('TWILIO_AUTH_TOKEN')) message = client.messages.create(to=elem.number, from_="+19733214779", media_url=[image.url]) image.is_sent = 1 db.session.commit()
def doStuff() : # get the image tts.say("Downloading image") myURL = mem.getData("LanguageLearner/ImageURL") urllib.urlretrieve(myURL, "C:/Users/Max/Documents/My Stuff/UK-NAO-hackathon/PepperPic.jpg") # image processing tts.say("Hmmmm let me see") clarifai_api = ClarifaiApi(language="fr") # assumes environment variables are set. result = clarifai_api.tag_images(open( "C:/Users/Max/Documents/My Stuff/UK-NAO-hackathon/PepperPic.jpg", 'rb')) resultList = result['results'][0]['result']['tag']['classes'] print resultList # Return the result to Pepper #print str(resultList[0]) #tts.say("I think this is a " + str(resultList[0])) print "sending word to Pepper:" try: mem.insertData("LanguageLearner/Object",str(resultList[0])) print resultList[0] except: mem.insertData("LanguageLearner/Object",str(resultList[1])) print resultList[1]
def clarifai(): if request_format_okay(request): s3_base_url = 'https://make-or-break.s3.amazonaws.com/' data = request.get_json() request_obj = Request.query.get(data["request_id"]) image_url = str(data["image_encoded"] + "") file_type = data["file_type"] if data["file_type"] else ".png" fileName = id_generator() + "." + file_type fh = open(fileName, "wb") fh.write(base64.b64decode(image_url)) fh.close() aws_key = os.environ.get('AWS_KEY') aws_secret = os.environ.get('AWS_SECRET') conn = tinys3.Connection(aws_key, aws_secret, tls=True) f = open(fileName, 'rb') conn.upload(fileName, f, 'make-or-break') f.close() s3_url = s3_base_url + fileName request_obj.image_url = s3_url db.session.commit() os.remove(fileName) clarifai_api = ClarifaiApi() # assumes environment variables are set result = clarifai_api.tag_image_urls(s3_url) return jsonify(result) else: return abort(415)
def get_tags(): #TODO: Error checking # global d # d = {} clarifai_api = ClarifaiApi() blob_service = BlobService('calhacks', 'mm7EmY+T+MGahePBDSDU5LHpZR5tRXuh4MSco4jFrzHovOPEf06e18c89pxtPIo4NDVhhjSeaQY/FQmKNxjjyA==') blob_name = request.form['blob_id'] # blob_name = blob_name.decode('utf-8') blob_service.get_blob_to_path('imagestore', blob_name, 'out.png') print("checkpoint 1") i = open ('out.png', 'r') strd = "" for line in i: strd += line.strip() fname = 'img.png' with open (fname, 'wb') as f: f.write (base64.b64decode(strd)) f.close() f = open (fname, 'rb') result = clarifai_api.tag_images(f) st = result['results'][0]['result']['tag']['classes'][0:6] print(st) for i in []:#['food', 'nobody', 'still life', 'meal', 'dish', 'plate', 'delicious', 'isolated', 'cutout', 'unhealthy', 'one', 'background']: while i in st: st.remove(i) js = json.dumps(search_terms(st)) print(js) return js
def tags(): url = request.args["url"] clarifai_api = ClarifaiApi() # assumes environment variables are set. result = clarifai_api.tag_image_urls(url) results = result['results'][0]["result"]["tag"]["classes"]#[:3] client = foursquare.Foursquare(client_id='JEK02X44TGMNJSE0VC1UBEB4FRNNW3UMFQ4IQOPI4BAR2GXA', \ client_secret='A2Z50VTUHHXEUYJBHCQKB1LXTNVVBYBQR4SDASVTXTWUMWXS') #foursquare shit address = request.args["address"] #address is currently a string geolocator = Nominatim() location = geolocator.geocode(address) newLocation = str(location.latitude) + str(", ") + str(location.longitude) # foursquare_dictionary = client.venues.explore(params={'ll': '40.7, -74', 'v': '20160402', 'query': results[0] + ',' + results[1] + ',' + results[2]}) foursquare_dictionary = client.venues.explore(params={'ll': newLocation, 'v': '20160402', 'query': results[0] + ',' + results[1] + ',' + results[2]}) #first place to eat # status1 = foursquare_dictionary['groups'][0]['items'][0]['venue']['hours']['status'] address1 = foursquare_dictionary['groups'][0]['items'][0]['venue']['location']['formattedAddress'] name = foursquare_dictionary['groups'][0]['items'][0]['venue']['name'] #second place to eat address2 = foursquare_dictionary['groups'][0]['items'][1]['venue']['location']['formattedAddress'] # status2 = foursquare_dictionary['groups'][0]['items'][1]['venue']['hours']['status'] name1 = foursquare_dictionary['groups'][0]['items'][1]['venue']['name'] return render_template('tags.html',\ newAddress1 = newAddress(address1), name = name,\ newAddress2 = newAddress(address2), name1 = name1,\ newLocation = newLocation)
def main(argv): # parse arguments parser = argparse.ArgumentParser() parser.add_argument("-t", "--tag", help="tag images", action='store_true') parser.add_argument("-c", "--color", help="color images", action='store_true') parser.add_argument("-u", "--usage", help="usage", action='store_true') args, argv = parser.parse_known_args() if len(argv) == 1: imageurl = argv[0] else: imageurl = 'http://clarifai-img.s3.amazonaws.com/test/toddler-flowers.jpeg' api = ClarifaiApi() if args.usage: response = api.get_usage() elif not args.color: response = tag_images(api, imageurl) elif args.color and not args.tag: response = color_images(api, imageurl) else: raise Exception('call with --tag or --color for the image') print(json.dumps(response))
def send_message(): image = Images.query.filter_by(is_sent=0).first() if image == None: imgur_client = ImgurClient(os.environ.get('IMGUR_CLIENT_ID'), os.environ.get('IMGUR_CLIENT_SECRET')) clarifai_api = ClarifaiApi() images = imgur_client.subreddit_gallery(subreddit='aww', page=0) for image in images: if image.is_album == False and ('jpeg' in image.type): try: result = clarifai_api.tag_image_urls(image.link) total_result = result['results'][0]['result']['tag']['classes'] if len(set(total_result).intersection(['dog', 'puppy', 'cat', 'kitten', 'bird'])) > 0: db.session.add(Images(image.link)) db.session.commit() except: pass image = Images.query.filter_by(is_sent=0).first() users = Users.query.filter_by(active=1).all() for user in users: try: client = TwilioRestClient(os.environ.get('TWILIO_ACCOUNT_SID'), os.environ.get('TWILIO_AUTH_TOKEN')) message = client.messages.create(to=user.number, from_="+19733214779", media_url=[image.link]) except: pass image.is_sent = 1 db.session.commit()
def greetings(bot, mask, target, data=None, **kw): # log.msg("Shortening URL %s" % url) if bot.nick == mask.nick: return url = None for match in __URL_REGEX.finditer(data): url = match.group(0) break if not url: return """Load configuration""" config = bot.config.get(__name__, {}) with aiohttp.Timeout(10): with aiohttp.ClientSession(loop=bot.loop) as session: headers = {"Content-Type": "application/json"} payload = {"url": url, "key": None} resp = yield from session.post(__URL_KRZUS, data=json.dumps(payload), headers=headers) if resp.status != 200: raise Exception r = yield from resp.read() data = json.loads(r.decode('utf-8')) short = data.get("url_short") if not url: return desc = None if url.endswith(__IMAGE_EXT) and ClarifaiApi is not None: api = ClarifaiApi(config['clarifai_app_id'], config['clarifai_app_secret']) tags = api.tag_image_urls(url) if tags['status_code'] == "OK": ret = ', '.join(tags['results'][0]['result']['tag']['classes']) desc = "Tags: {}".format(ret) elif BeautifulSoup is not None: with aiohttp.Timeout(10): with aiohttp.ClientSession(loop=bot.loop) as session: resp = yield from session.get(url) if resp.status == 200: r = yield from resp.read() soup = BeautifulSoup(r, "html.parser") title = soup.title.getText() if len(title) > 60: title = title[:60] + "…" desc = "Title: {}".format(title) if desc: bot.privmsg(target, desc) bot.privmsg(target, "\tURL: {}".format(short)) else: bot.privmsg(target, "URL: {}".format(short))
def call_vision_api(image_filename, api_keys): clarifai_api = ClarifaiApi( app_id=api_keys['clarifai']['client_id'], app_secret=api_keys['clarifai']['client_secret']) result = clarifai_api.tag_images(open(image_filename, 'rb')) text_result = json.dumps(result) return text_result
def main(argv): if len(sys.argv) > 1: api = ClarifaiApi() path = argv[1] ids = index_list(path + "/img") l = name_list(path + "/img", ids) resp = [] for elem in l: with open(elem['fullname'], 'rb') as image_file: print(elem['name']) response = api.tag_images(image_file) response['id'] = elem['id'] response['name'] = elem['name'] print(response) resp.append(response) save_obj(path, resp, "taglist") else: print("No path found") sys.exit()
def __init__(self, app_id=None, app_secret=None, model=None, select_classes=None): ImageExtractor.__init__(self) if app_id is None or app_secret is None: try: app_id = os.environ['CLARIFAI_APP_ID'] app_secret = os.environ['CLARIFAI_APP_SECRET'] except KeyError: raise ValueError("A valid Clarifai API APP_ID and APP_SECRET " "must be passed the first time a Clarifai " "extractor is initialized.") self.tagger = ClarifaiApi(app_id=app_id, app_secret=app_secret) if model is not None: self.tagger.set_model(model) self.model = model if select_classes is None: self.select_classes = None else: self.select_classes = ','.join(select_classes)
def get_words_url(url): clarifai_api = ClarifaiApi() # assumes environment variables are set. result = clarifai_api.tag_image_urls(url) tags=result['results'][0]['result']['tag']['classes'] probs=result['results'][0]['result']['tag']['probs'] return { tags[i] : probs[i] for i in range(len(tags)) }
class ClarifaiAPIExtractor(ImageExtractor): ''' Uses the Clarifai API to extract tags of images. Args: app_id (str): A valid APP_ID for the Clarifai API. Only needs to be passed the first time the extractor is initialized. app_secret (str): A valid APP_SECRET for the Clarifai API. Only needs to be passed the first time the extractor is initialized. model (str): The name of the Clarifai model to use. If None, defaults to the general image tagger. select_classes (list): List of classes (strings) to query from the API. For example, ['food', 'animal']. ''' _log_attributes = ('model', 'select_classes') def __init__(self, app_id=None, app_secret=None, model=None, select_classes=None): ImageExtractor.__init__(self) if app_id is None or app_secret is None: try: app_id = os.environ['CLARIFAI_APP_ID'] app_secret = os.environ['CLARIFAI_APP_SECRET'] except KeyError: raise ValueError("A valid Clarifai API APP_ID and APP_SECRET " "must be passed the first time a Clarifai " "extractor is initialized.") self.tagger = ClarifaiApi(app_id=app_id, app_secret=app_secret) if not (model is None): self.tagger.set_model(model) self.model = model if select_classes is None: self.select_classes = None else: self.select_classes = ','.join(select_classes) def _extract(self, stim): if stim.filename is None: file = tempfile.mktemp() + '.png' imsave(file, stim.data) else: file = stim.filename tags = self.tagger.tag_images(open(file, 'rb'), select_classes=self.select_classes) if stim.filename is None: os.remove(file) tagged = tags['results'][0]['result']['tag'] return ExtractorResult([tagged['probs']], stim, self, features=tagged['classes'])
def get_tags_and_probs(url): clarifai_api = ClarifaiApi() result = clarifai_api.tag_image_urls(url) tags = result['results'][0]['result']['tag']['classes'] probs = result['results'][0]['result']['tag']['probs'] #print tags #print probs return tags, probs
def test_tag_one_video_from_localfs(self): # video source: http://techslides.com/demos/sample-videos/small.mp4 video_file = "tests/data/small.mp4" api = ClarifaiApi() if os.path.exists(video_file): with open(video_file, "rb") as fb: response = api.tag_images(fb) self.assertTrue(response)
def test_tag_one_video(self): # video source: http://techslides.com/demos/sample-videos/small.mp4 video_url = "http://techslides.com/demos/sample-videos/small.mp4" api = ClarifaiApi() response = api.tag_image_urls(video_url) self.assertTrue(response) self.assertTrue(response["results"][0]["url"] == video_url)
def test_tag_one_video(self): # video source: http://techslides.com/demos/sample-videos/small.mp4 video_url = 'http://techslides.com/demos/sample-videos/small.mp4' api = ClarifaiApi() response = api.tag_image_urls(video_url) self.assertTrue(response) self.assertTrue(response['results'][0]['url'] == video_url)
def process_file_b64(b64, classes): clarifai_api = ClarifaiApi() data = {'encoded_data': b64} if (classes != ""): data = {'encoded_data': b64, 'select_classes': classes} json_resp = clarifai_api._base64_encoded_data_op(data, 'tag') queue.push(json_resp) return json_resp
def get_the_fucking_tags(image): """Uses clarifai api to get image tags.""" clarifai_api = ClarifaiApi() # assumes environment variables are set. result = clarifai_api.tag_images(open(image)) data = json.dumps(result) jdata = json.loads(data) jresults = jdata['results'][0]['result']['tag']['classes'][0] return jresults
def processClarifai(imgUrl): clarifai_api = ClarifaiApi("3RJprl1ls24WbI4X4FAfJe9zOL5TO_ogBU4druEW", "A7i9vHq95Kck9lbgsWZ_s4ZuLkUQBfL_iWE3J5Kt") # assumes environment variables are set. result = clarifai_api.tag_image_urls(imgUrl) classes = result['results'][0]['result']['tag']['classes'] total = [] for c in classes: total.append(c) return total
def test_tag_one_video_from_localfs(self): # video source: http://techslides.com/demos/sample-videos/small.mp4 video_file = 'tests/data/small.mp4' api = ClarifaiApi() if os.path.exists(video_file): with open(video_file, 'rb') as fb: response = api.tag_images(fb) self.assertTrue(response)
def __init__(self): client_id, client_secret = DATASOURCES['instagram'][ 'client_id'], DATASOURCES['instagram']['client_secret'] self.api = InstagramAPI(client_id=client_id, client_secret=client_secret) app_id, app_secret = DATASOURCES['clarifai']['app_id'], DATASOURCES[ 'clarifai']['app_secret'] self.image_api = ClarifaiApi(app_id=app_id, app_secret=app_secret)
def getTaggedPics(pictureURLsList): taggedPics = [] clarifai_api = ClarifaiApi() # assumes environment variables are set. for picURL in pictureURLsList: result = clarifai_api.tag_image_urls(picURL) tagsPic = result['results'][0]['result']['tag']['classes'] taggedPics.append(tagsPic) return taggedPics
def getTagsFromImages(images): clarifai_api = ClarifaiApi() # assumes environment variables are set. ans = [] y = clarifai_api.tag_image_urls(images) ans = {} for x in y["results"]: for i in range(len(x["result"]["tag"]["classes"])): ans[x["result"]["tag"]["classes"][i]] = x["result"]["tag"]["probs"][i]/3 return ans
def test_embed_one_image(self): image_url = 'https://samples.clarifai.com/toddler-flowers.jpeg' api = ClarifaiApi() try: response = api.embed_image_urls(image_url) self.assertTrue(response) self.assertTrue(response['results'][0]['url'] == image_url) except ApiError as e: # User does not have permission. self.check_unauth(e)
def process_image(urls): tags = dict() api = ClarifaiApi("KuQ2EWXhdosMQyktSI1tw6Z3be7c677oA11p1g9o","Kt84XYqK7nVnx3f05_B8pe2biv9bBlGZAHjFvEwF") for u in urls: response = api.tag_image_urls(u) tags[u] = response['results'][0]['result']['tag']['classes'] return tags
def get_keywords_for_image(img_url): """ Gets the keyword associated with the image through the Clarifai API """ clarifai_api = ClarifaiApi("_O7oVw0TVQ2SC7l3Bwkb_UJ95LAzE8amEMQp0VGr", "hAhcV7cMgFmXDq-91Bvbk-V5FEaL1dTfTxtVtRzz" ) # assumes environment variables are set. # result = clarifai_api.tag_images(open(img_url, "rb")) result = clarifai_api.tag_image_urls(img_url) return result['results'][0]['result']['tag']['classes']
def __init__(self, tags, band, album): self._tags, self.tags = tags, tags self.band = band self.album = album self.available_fonts = [ font for font in os.listdir('./fonts') if font.lower().endswith('ttf') ] self.flickr = FlickrAPI(FLICKR_API_KEY, FLICKR_API_SECRET) self.clarifai = ClarifaiApi(CLARIFAI_API_KEY, CLARIFAI_API_SECRET)
def getTags(gifUrl): cleanTags = [] clarifai_api = ClarifaiApi() # assumes environment variables are set. result = clarifai_api.tag_image_urls(gifUrl) tags = result['results'][0]['result']['tag']['classes'] #tags = map(str, tags) for element in tags: cleanTags += eval(element) return cleanTags
def test_i18n(self): api = ClarifaiApi(language='aaa') api = self.get_client() request_body = api._setup_multi_data([], ['urls'], language='en') self.assertEqual(request_body['language'], 'en', 'language field was not set') languages = api.get_languages() self.assertTrue(len(languages), 'did not return any languages') self.assertTrue('en' in languages, 'english code not included in languages')
def test_i18n(self): api = ClarifaiApi(language="aaa") api = self.get_client() request_body = api._setup_multi_data([], ["urls"], language="en") self.assertEqual(request_body["language"], "en", "language field was not set") languages = api.get_languages() self.assertTrue(len(languages), "did not return any languages") self.assertTrue("en" in languages, "english code not included in languages")
def getTags(url): ret = '' api = ClarifaiApi(BYTEBOT_PLUGIN_CONFIG['shorturl']['clarifai_app_id'], BYTEBOT_PLUGIN_CONFIG['shorturl']['clarifai_app_secret']) tags = api.tag_image_urls(url) if (tags[u'status_code'] == "OK"): ret = ', '.join(tags[u'results'][0][u'result'][u'tag'][u'classes']) return ret
def defineFood(fooditem): """Send picture to clarafai, return tags""" tags=[] clarifai_api = ClarifaiApi(app_id='tbndTvx-Mv_OGD4CKeOhPap1gfAFSSWDUzPT2X6x', app_secret='JQNnSLBftBwJLMkNtIsdhUdU7OQ0a5HZDKLdPTtR') # assumes environment variables are set. result = clarifai_api.tag_images(fooditem) #parse result i=0 while (i<len(result[u'results'][0][u'result'][u'tag'][u'classes'])): tags.append(result[u'results'][0][u'result'][u'tag'][u'classes'][i]) i=i+1 return tags
def get_words_here(path): clarifai_api = ClarifaiApi() # assumes environment variables are set. result = clarifai_api.tag_images(open(path)) #print(result) tags=result['results'][0]['result']['tag']['classes'] probs=result['results'][0]['result']['tag']['probs'] d={} for i in range(len(tags)): d[tags[i]]=probs[i] return d
def getTags(url): ret = '' api = ClarifaiApi( BYTEBOT_PLUGIN_CONFIG['shorturl']['clarifai_app_id'], BYTEBOT_PLUGIN_CONFIG['shorturl']['clarifai_app_secret'] ) tags = api.tag_image_urls(url) if(tags[u'status_code'] == "OK"): ret = ', '.join(tags[u'results'][0][u'result'][u'tag'][u'classes']) return ret
def test_tag_n_embed_one_image(self): image_url_base = 'http://clarifai-img.s3.amazonaws.com/test' image_files = [ 'metro-north.jpg', 'octopus.jpg', 'tahoe.jpg', 'thai-market.jpg' ] image_urls = [ os.path.join(image_url_base, one_file) for one_file in image_files ] api = ClarifaiApi() response = api.tag_and_embed_image_urls(image_urls) self.assertTrue(response)
def main(): clarifai_api = ClarifaiApi() #ADD ANY MORE IMAGES HERE, PROGRAM SHOULD ADJUST image_array = [open('output/rgb_img_' + str(x) + ".jpg", 'rb') for x in xrange(1,13)] results_json = clarifai_api.tag_images(image_array) results = results_json['results'] createHashmap(results) find_objects() text_results_string = str(namesOfObjects) say(text_results_string)
def test_tag_n_embed_one_image(self): image_url_base = 'https://samples.clarifai.com' image_files = ['metro-north.jpg', 'tahoe.jpg', 'thai-market.jpg'] image_urls = [ os.path.join(image_url_base, one_file) for one_file in image_files ] api = ClarifaiApi() try: response = api.tag_and_embed_image_urls(image_urls) self.assertTrue(response) except ApiError as e: self.check_unauth(e)
def test_no_resizing(self, mock_api): """ test with no image resizing """ image_file = 'tests/data/toddler-flowers.jpeg' api = ClarifaiApi(resize=False) self.assertFalse(api.resize) if os.path.exists(image_file): with open(image_file, 'rb') as fb: response = api.tag_images(fb) self.assertTrue(response) self.assertFalse(mock_api._resize_image_tuple.called)
def test_send_feedback(self): """ test sending various feedback """ urls = ['https://samples.clarifai.com/metro-north.jpg', \ 'https://samples.clarifai.com/metro-north.jpg'] api = ClarifaiApi() response = api.feedback(urls=urls[0], add_tags='train') self.assertTrue(response) response = api.feedback(urls=urls[0], remove_tags='speed,test') self.assertTrue(response) response = api.feedback(urls=urls[0], add_tags='train', remove_tags='speed,test') self.assertTrue(response) docids = [hashlib.md5(url.encode('utf-8')).hexdigest() for url in urls] response = api.feedback(urls=urls[:2], similar_docids=docids[:2]) self.assertTrue(response) response = api.feedback(urls=urls[1:], dissimilar_docids=docids[1:]) self.assertTrue(response) response = api.feedback(urls=urls, similar_docids=docids[:2], dissimilar_docids=docids[1:]) self.assertTrue(response)
def get_clarifai(filename): clarifai_api = ClarifaiApi() # assumes environment variables are set. result = clarifai_api.tag_images(open('uploads/' + filename, 'rb')) api_results = {} tag_results = result[u'results'][0][u'result'][u'tag'][u'classes'] probability_results = result[u'results'][0][u'result'][u'tag'][u'probs'] for index in enumerate(probability_results): probability = round(probability_results[index[0]] * 100) tag = str(tag_results[index[0]]) api_results[probability] = tag return api_results
class ClarifaiAPIExtractor(ImageExtractor): ''' Uses the Clarifai API to extract tags of images. Args: app_id (str): A valid APP_ID for the Clarifai API. Only needs to be passed the first time the extractor is initialized. app_secret (str): A valid APP_SECRET for the Clarifai API. Only needs to be passed the first time the extractor is initialized. model (str): The name of the Clarifai model to use. If None, defaults to the general image tagger. select_classes (list): List of classes (strings) to query from the API. For example, ['food', 'animal']. ''' def __init__(self, app_id=None, app_secret=None, model=None, select_classes=None): ImageExtractor.__init__(self) if app_id is None or app_secret is None: try: app_id = os.environ['CLARIFAI_APP_ID'] app_secret = os.environ['CLARIFAI_APP_SECRET'] except KeyError: raise ValueError("A valid Clarifai API APP_ID and APP_SECRET " "must be passed the first time a Clarifai " "extractor is initialized.") self.tagger = ClarifaiApi(app_id=app_id, app_secret=app_secret) if not (model is None): self.tagger.set_model(model) if select_classes is None: self.select_classes = None else: self.select_classes = ','.join(select_classes) def _extract(self, stim): if stim.filename is None: file = tempfile.mktemp() + '.png' imsave(file, stim.data) else: file = stim.filename tags = self.tagger.tag_images(open(file, 'rb'), select_classes=self.select_classes) if stim.filename is None: os.remove(temp_file) tagged = tags['results'][0]['result']['tag'] return ExtractorResult([tagged['probs']], stim, self, features=tagged['classes'])
def pull_tags_from_clarafai(img_urls): friends = [] clarifai_api = ClarifaiApi() clarifai_results = clarifai_api.tag_image_urls(img_urls) for image in clarifai_results["results"]: friend = {"url": image["url"], "tags": []} for i, tag_name in enumerate(image["result"]["tag"]["classes"]): if image["result"]["tag"]["probs"][i] >= 0.9: friend["tags"].append(tag_name) friends.append(friend) return friends
def check_pretty(imageurl): api = ClarifaiApi() #api.set_model("") if imageurl.startswith('http'): response = api.tag_image_urls(imageurl, select_classes="pretty,human") elif os.path.isdir(imageurl): response = tag_images_in_directory(imageurl) elif os.path.isfile(imageurl): with open(imageurl,'rb') as image_file: response = api.tag(image_file, select_classes="pretty,human,man,woman") else: raise Exception("Must input url, directory path, or file path") print json.dumps(response, indent=2) return response
def img_handler(): """ - Grabs the binary data from the WebRTC still grab attached to POST - WebRTC is intuitive--unicode or ASCII byte-encodings not-so-much. Manipulates the unicode that Python gets from the POST request form dictionary and turns it into the appropriate ASCII byte-encoding, which is then base-64-decoded, and then piped into a random UUID-named .png file. - As a hack, I used sshfs to mount the public_html directory of my UT Austin CS account address into my working dir, and sent the new .png files into that folder, chmodding per upload. - This renders the image into a resource that's easily accessible by APIs. (Although this obviously won't scale, I only have 2 GB as an undergrad.) - Finally, sends the URL via POST to the Microsoft Emotions API - tl;dr I changed an image data-uri to a publicly available URL so it'd play better with some ML libraries that didn't have native Python clients, but did have RESTful APIs. """ data = request.form.get('stillIn') data = data[22:].encode('latin-1') binary_data = a2b_base64(data) session['uuid'] = str(uuid4()) fn = session['uuid'] + ".png" with open('./models/mount/{}'.format(fn), 'wb') as fd: fd.write(binary_data) subprocess.call("chmod 755 ./models/mount/{}".format(fn), shell=True) resource = "http://cs.utexas.edu/~rainier/{}".format(fn) print json.dumps({'url': resource}) # msft request try: msft_url = "https://api.projectoxford.ai/emotion/v1.0/recognize" headers = {'Ocp-Apim-Subscription-Key': config['MSFT_EMOTION_KEY'], 'Content-Type': 'application/json'} msft_req = requests.post(url=msft_url, data=json.dumps({'url': resource}), headers=headers) print "msft {}".format(msft_req.json()) except: flash('No face was detected!') return redirect('/', messages=get_flashed_messages()) session['msft'] = msft_parse(msft_req.json()) # indicoio request session['indico'] = indicoio.fer(resource) # clarifai request clarifai_api = ClarifaiApi() clarifai_req = clarifai_api.tag_image_urls(resource) session['clarifai'] = clarifai_parse(clarifai_req) return redirect('/results')
class ClarifaiParse: def __init__(self, app_id=None, app_secret=None): self.api = ClarifaiApi(app_id, app_secret) def parse(self, url): try: api_result = self.api.tag_image_urls(url) if api_result["status_code"] == "OK": matches = [] for r in api_result['results']: tag = r["result"]["tag"] number_results = len(tag["classes"]) for i in range(number_results): data = { "tag": tag["classes"][i], "probs": tag["probs"][i], "id": tag["concept_ids"][i] } matches.append(data) return matches else: print(api_result["status_code"]) print(api_result["status_msg"]) return [] except IOError: print("can't open", filename) return [] except Exception as e: print(e) return []