def test_unicode_urls(self): image_url = u'https://samples.clarifai.com/Álvaro-Noboa-y-Annabella-Azín-Votaciones.jpg' api = ClarifaiApi() response = api.tag_image_urls(image_url) self.assertTrue(response) self.assertTrue(response['results'][0]['url'] == image_url) # test with Chinese in URL image_url = u'https://samples.clarifai.com/长城.jpg' api = ClarifaiApi() response = api.tag_image_urls(image_url) self.assertTrue(response) self.assertTrue(response['results'][0]['url'] == image_url)
def test_concept_ids(self): """new models should return concept_ids""" api = ClarifaiApi() api.set_model('general-v1.3') response = api.tag_image_urls(self.image_url) tag = response['results'][0]['result']['tag'] self.assertTrue('concept_ids' in tag, 'concept_ids not included in new model') self.assertTrue(tag['concept_ids'][0].startswith('ai_'), 'concept id doesn\'t start with ai_') response = api.tag_image_urls(self.video_url) self.assertTrue( response['results'][0]['result']['tag']['concept_ids'][0] [0].startswith('ai_'), "video concept_ids didn't start wit ai_")
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 test_tag_images(self): """ tag multiple images, from url and disk """ # tag images from online URL 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() response = api.tag_image_urls(image_urls) self.assertTrue(response) # tag images frmo local fs image_dir = 'tests/data' image_files = ['metro-north.jpg', 'tahoe.jpg', 'thai-market.jpg'] api = ClarifaiApi() if os.path.exists(image_dir): image_files = [ open(os.path.join(image_dir, one_file), 'rb') for one_file in image_files ] response = api.tag_images(image_files) self.assertTrue(response) for fd in image_files: fd.close()
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 []
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 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 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 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 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 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)) }
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 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))
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 []
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 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 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 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(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 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 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 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 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
class SearchIndex(object): """Index search creator tool thing blah cool""" def __init__(self, image_list): """ Params: image_list(list[ImageLink]) """ self.clarifai = ClarifaiApi() self.index = defaultdict(set) self.image_list = image_list def add_image(self, imgurl, user="******"): if imgurl in all_images: users[user].addImageToUser(imgurl) return all_images.add(imgurl) users[user].addImageToUser(imgurl) tags = self.tag_photo(imgurl) for tag in tags: self.index[tag].add(imgurl) with open('urls.txt', 'w') as f: pickle.dump(self.index, f) f.close() def gen_index(self): for img in self.image_list: tags = self.tag_photo(img) for tag in tags: self.index[tag].add(img) def tag_photo(self, image): result = self.clarifai.tag_image_urls(image) tags = result['results'][0]['result']['tag']['classes'] print tags return tags def search(self, search_term, user="******"): terms = search_term.split() counts = defaultdict(int) for term in terms: (closest, num) = process.extractOne(term, self.index.keys()) print closest, num if num > 90: results = self.index[closest] for img in results: if img in users[user].imgurls: counts[img] += 1 sorted_counts = sorted(counts.items(), key=operator.itemgetter(1)) print users return sorted_counts
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
class CoverMaker(object): 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 make_cover(self): # Generate random images img1 = self._get_random_photo() img2 = self._get_random_photo() # Blend images, with a level between 0.25 and 0.75 to make sure both can be seen cover = Image.blend(img1, img2, random.uniform(LOW_BLEND, HIGH_BLEND)) # Add band name and title band_position = (random.choice(MARGINS), random.choice(MARGINS)) band_font = self._get_random_font(TITLE_SIZE) ImageDraw.Draw(cover).text(band_position, self.band, font=band_font) album_position = (random.choice(MARGINS), TOP_ALBUM+random.choice(MARGINS)) album_font = self._get_random_font(ALBUM_SIZE) ImageDraw.Draw(cover).text(album_position, self.album, font=album_font) # Return cover return cover def _get_random_font(self, size): return ImageFont.truetype("./fonts/{font}".format(**{ 'font' : random.choice(self.available_fonts) }), size) def _check_with_clarifai(self, url, tag): data = self.clarifai.tag_image_urls(url)['results'][0]['result']['tag'] classes, probs = data['classes'], data['probs'] tags = [class_ for (i, class_) in enumerate(classes) if probs[i] > LOW_PROB] return tag in tags def _get_random_photo(self, size=IMG_SIZE): """ Get a random photo from Flickr and returns as a PIL image of a given size. """ tag = random.choice(tags) data = self.flickr.photos.search(text=tag, per_page='500', content_type=1, sort='relevance', format="json") photos = json.loads(data).get('photos').get('photo') photo = random.choice(photos) url = "https://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg".format(**{ 'farm-id' : photo.get('farm', ''), 'server-id' : photo.get('server', ''), 'id' : photo.get('id', ''), 'secret' : photo.get('secret', ''), }) if self._check_with_clarifai(url, tag): self.tags.remove(tag) file = StringIO(urlopen(url).read()) return Image.open(file).resize(size) else: return self._get_random_photo()
class InstagramService(object): 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 posts_in_new_york(self): media = [] for row in zips: lat, lng = float(row[0]), float(row[1]) posts = self.api.media_search(lat=lat, lng=lng, distance=800) for post in posts: temp_list = [post.id, post.images['standard_resolution'].url, post.created_time, post.location.point.latitude, post.location.point.longitude] media.append(temp_list) print temp_list return media def location(self, lat, lng): posts = self.api.media_search(lat=lat, lng=lng, distance=800) media = [] for post in posts: temp_list = [post.id, post.images['standard_resolution'].url, post.created_time, post.location.point.latitude, post.location.point.longitude] media.append(temp_list) print temp_list return media def hashtag(self, lat, lng): posts = self.api.media_search(lat=lat, lng=lng, distance=800) hashtags = {} for post in posts: tokens = str(post.caption).split(' ') for token in tokens: if token and len(token) > 1: if token[0] == '#': count = hashtags.setdefault(token, 0) hashtags[token] = count + 1 elif token[0] == '"' and token[1] == '#': count = hashtags.setdefault(token[1:], 0) hashtags[token[1:]] = count + 1 return hashtags def get_tags(self, place): tags = {} lat, lng = Geocode().geocode(place) posts = self.api.media_search(lat=lat, lng=lng, distance=800) for post in posts: result = self.image_api.tag_image_urls(post.images['standard_resolution'].url) image_tags = result['results'][0]['result']['tag']['classes'][:5] for tag in image_tags: count = tags.setdefault(tag, 0) tags[tag] = count + 1 return tags
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 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 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
class Classify: def __init__(self): self.clarifai = ClarifaiApi() # assumes environment variables are set. self.wordsToCounts = {} def classify(self, url): try: return self.clarifai.tag_image_urls(url) except: print 'Error classifying image with Clarifai API' return {} # returns a list of classes def parseClassification(self, response): # if good response if bool(response): status = response['status_code'] classes = response['results'][0]['result']['tag']['classes'] # if type is gif, get the nested classes if any(isinstance(i, list) for i in classes): allClasses = [] for sublist in classes: for item in sublist: allClasses.append(item) return allClasses else: return classes else: return [] def classifyUrls(self, urls): for url in urls: classes = self.parseClassification(self.classify(url)) for className in classes: if className not in self.wordsToCounts: self.wordsToCounts[className] = 1 else: self.wordsToCounts[className] += 1 def countWords(self): words = [] for word in self.wordsToCounts: count = self.wordsToCounts[word] wordDict = {} # expected by jqcloud wordDict['text'] = str(word) wordDict['weight'] = count words.append(wordDict) return words
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 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 home(request): """Renders the home page.""" assert isinstance(request, HttpRequest) if request.method == 'GET': form = PostForm() else: form = PostForm(request.POST) # Bind data from request.POST into a PostForm if form.is_valid(): imgURL = form.cleaned_data['content'] app_id = "DbZ4NzfrPL-K_CHHf4y4srnvBUSgMo4Dz9BIbeXt" app_secret = "crjTy-8St_kiFkL0wZZCFyrcoWJyOdets8Fa1BNi" clarifai_api = ClarifaiApi(app_id,app_secret) tags = '' embedLink = '' try: result = clarifai_api.tag_image_urls(imgURL) except: #if url is invalid based on clarifai API call tags = 'invalid url' imgURL = '' if tags!='invalid url': tagList = result['results'][0]['result']['tag']['classes'] bestGenre = imgscore(tagList,genres) r = requests.get('https://api.spotify.com/v1/search?q=%22'+bestGenre+'%22&type=playlist') jsonStuff = r.json() uri = jsonStuff['playlists']['items'][0]['uri'] embedLink = "https://embed.spotify.com/?uri="+uri return render( request, 'app/index.html', { 'form': form, 'imgsrc': imgURL, 'debugText': tags, 'playlistURI': embedLink, 'year':datetime.now().year, } ) return render( request, 'app/index.html', { 'form': form, 'imgsrc': '', 'debugText': '', 'playlistURI': '', 'year':datetime.now().year, } )
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')
def get_keywords(sources): clarifai_api = ClarifaiApi() keywords = [] count = 0 for i in sources: result = clarifai_api.tag_image_urls(i) keywords.append([]) for i in range(len(result['results'][0]["result"]["tag"]["classes"])): aKeyword = result['results'][0]["result"]["tag"]["classes"][i] keywords[count].append(aKeyword) count+=1 print '@@@@@@@@@@@@@ get_keywords' print keywords print '@@@@@@@@@@@@@@' return keywords
def test_tag_one_image(self): """ tag one image, from url and disk """ # tag image from online URL image_url = "http://clarifai-img.s3.amazonaws.com/test/toddler-flowers.jpeg" api = ClarifaiApi() response = api.tag_image_urls(image_url) self.assertTrue(response) self.assertTrue(response["results"][0]["url"] == image_url) # tag image from local fs image_file = "tests/data/toddler-flowers.jpeg" api = ClarifaiApi() if os.path.exists(image_file): with open(image_file, "rb") as fb: response = api.tag_images(fb) self.assertTrue(response)
def test_tag_gif(self): """ tag one GIF animation file """ # source: http://media.giphy.com/media/fRZn2vraBGiA0/giphy.gif image_url = 'https://samples.clarifai.com/giphy.gif' api = ClarifaiApi() response = api.tag_image_urls(image_url) self.assertTrue(response) self.assertTrue(response['results'][0]['url'] == image_url) image_file = 'tests/data/water-ocean-turtle.gif' api = ClarifaiApi() if os.path.exists(image_file): with open(image_file, 'rb') as fb: response = api.tag_images(fb) self.assertTrue(response)
def get_keywords(self, sources): clarifai_api = ClarifaiApi() keywords = [] count = 0 for i in sources: result = clarifai_api.tag_image_urls(i) keywords.append([]) for i in range(len(result['results'][0]["result"]["tag"]["classes"])): aKeyword = result['results'][0]["result"]["tag"]["classes"][i] keywords[count].append(aKeyword) count+=1 print '@@@@@@@@@@@@@ get_keywords' print keywords print '@@@@@@@@@@@@@@' return keywords
def test_tag_one_image(self): """ tag one image, from url and disk """ # tag image from online URL image_url = 'https://samples.clarifai.com/toddler-flowers.jpeg' api = ClarifaiApi() response = api.tag_image_urls(image_url) self.assertTrue(response) self.assertTrue(response['results'][0]['url'] == image_url) # tag image from local fs image_file = 'tests/data/toddler-flowers.jpeg' api = ClarifaiApi() if os.path.exists(image_file): with open(image_file, 'rb') as fb: response = api.tag_images(fb) self.assertTrue(response)
def test_tag_gif(self): """ tag one GIF animation file """ # source: http://media.giphy.com/media/fRZn2vraBGiA0/giphy.gif image_url = "http://media.giphy.com/media/fRZn2vraBGiA0/giphy.gif" api = ClarifaiApi() response = api.tag_image_urls(image_url) self.assertTrue(response) self.assertTrue(response["results"][0]["url"] == image_url) image_file = "tests/data/water-ocean-turtle.gif" api = ClarifaiApi() if os.path.exists(image_file): with open(image_file, "rb") as fb: response = api.tag_images(fb) self.assertTrue(response)
def toClarifai(image): #returns a list of phrases to be searched clarifai_api = ClarifaiApi(app_id = 'WmQfZ90p6JsaxHbUGmFjtDtr3UDPOOM4AW8v_ezU',app_secret = 'EKq9zb8zjc-K0Iq6B9dTL1T613Um5XPiR2-rC9pR') resultDict = clarifai_api.tag_image_urls(image) result = resultDict['results'][0]['result']['tag'] tagList = result['classes'] probList = result['probs'] numTags = len(tagList) toSearch = [] # print tagList for i in range(numTags): if probList[i] < .8: break if ' ' in tagList[i]: if tagList[i] != 'no person': toSearch.append(tagList[i]) elif searchNouns(tagList[i]): toSearch.append(tagList[i]) return toSearch
def main(argv): if len(argv) > 1: imageurl = argv[1] else: imageurl = 'http://clarifai-img.s3.amazonaws.com/test/toddler-flowers.jpeg' api = ClarifaiApi() if imageurl.startswith('http'): response = api.tag_image_urls(imageurl) elif os.path.isdir(imageurl): response = tag_images_in_directory(imageurl, api) 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") print response
def check_image(browser, clarifai_id, clarifai_secret, img_tags, full_match=False): """Uses the link to the image to check for invalid content in the image""" clarifai_api = ClarifaiApi(clarifai_id, clarifai_secret) img_link = get_imagelink(browser) result = clarifai_api.tag_image_urls(img_link) result_tags = result['results'][0]['result']['tag']['classes'] for (tags, should_comment, comments) in img_tags: if should_comment: if given_tags_in_result(tags, clarifia_tags, full_match): return True, comments else: if given_tags_in_result(tags, clarifia_tags, full_match): print('Inappropriate content in Image, not commenting') return False, [] return True, []
def check_image(browser, clarifai_id, clarifai_secret, img_tags, full_match=False): """Uses the link to the image to check for invalid content in the image""" clarifai_api = ClarifaiApi(clarifai_id, clarifai_secret) img_link = get_imagelink(browser) result = clarifai_api.tag_image_urls(img_link) clarifai_tags = result['results'][0]['result']['tag']['classes'] for (tags, should_comment, comments) in img_tags: if should_comment: if given_tags_in_result(tags, clarifai_tags, full_match): return True, comments else: if given_tags_in_result(tags, clarifai_tags, full_match): print('Inappropriate content in Image, not commenting') return False, [] return True, []
def check_image(browser, clarifai_id, clarifai_secret, img_tags): """Uses the link to the image to check for invalid content in the image""" clarifai_api = ClarifaiApi(clarifai_id, clarifai_secret) img_link = get_imagelink(browser) result = clarifai_api.tag_image_urls(img_link) result_tags = result['results'][0]['result']['tag']['classes'] for pair in img_tags: if not pair[1]: if given_tags_in_result(pair[0], result_tags): print('Inappropriate content in Image, not commenting') return False, [] else: if given_tags_in_result(pair[0], result_tags): return True, pair[2] return True, []
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 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 main(argv): imageurl = argv[1] api = ClarifaiApi() if imageurl.startswith('http'): response = api.tag_image_urls(imageurl) elif os.path.isdir(imageurl): response = tag_images_in_directory(imageurl, api) 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") results = ((((response['results'])[0])['result'])['tag'])['classes'] print json.dumps(results)
counter) print url_string counter += 4 url = (url_string) request = urllib2.Request(url, None, {}) response = urllib2.urlopen(request) # Process the JSON string. results = simplejson.load(response) for i in range(0, len(results), 1): try: resp = results.get('responseData').get('results')[i].get('url') values = clarifai_api.tag_image_urls(resp) tags = values.get('results')[0].get('result').get('tag').get( 'classes') for each in tags: if (each == "men" or each == "boy" or each == "man"): menCounter += 1 elif (each == "women" or each == "girl" or each == "woman"): womenCounter += 1 except: pass # now have some fun with the results... print(menCounter) print(womenCounter)
def get_json_obj_remote(url): api = ClarifaiApi("GniW5mCUYJX8b11rI8VnoLncgwUeORRNkl9R-q_J", "H-YPKDaogchMM7Wv6hKIC2qTA9uqXy9tWjxZXFJn") result = api.tag_image_urls(url) return json.dumps(result)
import re import requests import json import cgi import cgitb from clarifai.client import ClarifaiApi print("Content-type:text/html\r\n\r\n") import cgitb cgitb.enable() img_url = input() clarifai_api = ClarifaiApi() result = clarifai_api.tag_image_urls(img_url) tags = result['results'][0]['result']['tag']['classes'] user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11' header = {'User-Agent': user_agent} vocab_url = "https://www.vocabulary.com/dictionary/" facts_url = "http://www.thefactsite.com/?s=" session = requests.session() html = session.get(vocab_url + tags[0]) defs = re.findall('''<a title="(.*?)" name="\w\d{3,5}" class="anchor">\w</a>\s*(.*?)</h3>''', html.text) print("<br><h2>Definitions of {:s}</h2><br>".format(tags[0])) for i in defs: print('<u><i>'+ i[0] + '</u></i>', i[1] + '<br>') for word in tags:
#ClarifaiApi stuff clarifai_api = ClarifaiApi("5ufSEyCVOzwUzNX5nDWPFXaTHbOTkbS3NzAGyrNa", "JbqnMT_wofMV1IS-QZ0jIsYo-60tdHkQOFfqGxM7") urls = [ 'https://digitalmarketing.blob.core.windows.net/6305/images/thumbs/image124265.jpg', 'http://media.vanityfair.com/photos/56cb5d18ab73e22d6d9321f6/master/h_590,c_limit/donald-trump-short-fingered-vulgarian-fingers-bruce-handy-ss13.jpg' ] tagHolder = [] i = 0 while i < len(urls): result = clarifai_api.tag_image_urls(urls[i]) imageTags = [] for tag in result['results'][0]['result']['tag']['classes']: tag = tag.encode('ascii', 'ignore') imageTags.append(tag) tagHolder.append(imageTags) i += 1 j = 0 while j < i: #print urls[j] #for imageTags in tagHolder[j]: # print imageTags j += 1 #MongoDB stuff
class InstagramService(object): 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 posts_in_new_york(self): media = [] for row in zips: lat, lng = float(row[0]), float(row[1]) posts = self.api.media_search(lat=lat, lng=lng, distance=800) for post in posts: temp_list = [ post.id, post.images['standard_resolution'].url, post.created_time, post.location.point.latitude, post.location.point.longitude ] media.append(temp_list) print temp_list return media def location(self, lat, lng): posts = self.api.media_search(lat=lat, lng=lng, distance=800) media = [] for post in posts: temp_list = [ post.id, post.images['standard_resolution'].url, post.created_time, post.location.point.latitude, post.location.point.longitude ] media.append(temp_list) print temp_list return media def hashtag(self, lat, lng): posts = self.api.media_search(lat=lat, lng=lng, distance=800) hashtags = {} for post in posts: tokens = str(post.caption).split(' ') for token in tokens: if token and len(token) > 1: if token[0] == '#': count = hashtags.setdefault(token, 0) hashtags[token] = count + 1 elif token[0] == '"' and token[1] == '#': count = hashtags.setdefault(token[1:], 0) hashtags[token[1:]] = count + 1 return hashtags def get_tags(self, place): tags = {} lat, lng = Geocode().geocode(place) posts = self.api.media_search(lat=lat, lng=lng, distance=800) for post in posts: result = self.image_api.tag_image_urls( post.images['standard_resolution'].url) image_tags = result['results'][0]['result']['tag']['classes'][:5] for tag in image_tags: count = tags.setdefault(tag, 0) tags[tag] = count + 1 return tags
class CoverMaker(object): 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 make_cover(self): # Generate random images img1 = self._get_random_photo() img2 = self._get_random_photo() # Blend images, with a level between 0.25 and 0.75 to make sure both can be seen cover = Image.blend(img1, img2, random.uniform(LOW_BLEND, HIGH_BLEND)) # Add band name and title band_position = (random.choice(MARGINS), random.choice(MARGINS)) band_font = self._get_random_font(TITLE_SIZE) ImageDraw.Draw(cover).text(band_position, self.band, font=band_font) album_position = (random.choice(MARGINS), TOP_ALBUM + random.choice(MARGINS)) album_font = self._get_random_font(ALBUM_SIZE) ImageDraw.Draw(cover).text(album_position, self.album, font=album_font) # Return cover return cover def _get_random_font(self, size): return ImageFont.truetype( "./fonts/{font}".format( **{'font': random.choice(self.available_fonts)}), size) def _check_with_clarifai(self, url, tag): data = self.clarifai.tag_image_urls(url)['results'][0]['result']['tag'] classes, probs = data['classes'], data['probs'] tags = [ class_ for (i, class_) in enumerate(classes) if probs[i] > LOW_PROB ] return tag in tags def _get_random_photo(self, size=IMG_SIZE): """ Get a random photo from Flickr and returns as a PIL image of a given size. """ tag = random.choice(tags) data = self.flickr.photos.search(text=tag, per_page='500', content_type=1, sort='relevance', format="json") photos = json.loads(data).get('photos').get('photo') photo = random.choice(photos) url = "https://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg".format( **{ 'farm-id': photo.get('farm', ''), 'server-id': photo.get('server', ''), 'id': photo.get('id', ''), 'secret': photo.get('secret', ''), }) if self._check_with_clarifai(url, tag): self.tags.remove(tag) file = StringIO(urlopen(url).read()) return Image.open(file).resize(size) else: return self._get_random_photo()
def test_tag_one_video(self): api = ClarifaiApi() response = api.tag_image_urls(self.video_url) self.assertTrue(response) self.assertTrue(response['results'][0]['url'] == self.video_url)