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 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 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 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 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 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 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 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 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 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 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_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 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
class Classifier(): def __init__(self): os.environ['CLARIFAI_APP_ID']='QCk5pf9LmMXVxQ5bElovZaIl79kKlb8KcwDZzsmR' os.environ['CLARIFAI_APP_SECRET']='5iN32kJkOm-3ajqLmWxJuGLa8BzuoxMqTwOidYM3' self.cfa = ClarifaiApi() def tag(self, data): result = self.cfa.tag_images(data)['results'][0]['result']['tag']['classes'] return result
class Classifier(): def __init__(self, **kargs): os.environ['CLARIFAI_APP_ID']=kargs['app_id'] os.environ['CLARIFAI_APP_SECRET']=kargs['app_secret'] self.cfa = ClarifaiApi() def tag(self, data): result = self.cfa.tag_images(data)['results'][0]['result']['tag']['classes'] return result
class Classifier(): def __init__(self, **kargs): os.environ['CLARIFAI_APP_ID'] = kargs['app_id'] os.environ['CLARIFAI_APP_SECRET'] = kargs['app_secret'] self.cfa = ClarifaiApi() def tag(self, data): result = self.cfa.tag_images( data)['results'][0]['result']['tag']['classes'] return result
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 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_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)
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 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
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_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 = "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 add(): tagHolder = [] i = 0 #ClarifaiApi stuff clarifai_api = ClarifaiApi("5ufSEyCVOzwUzNX5nDWPFXaTHbOTkbS3NzAGyrNa", "JbqnMT_wofMV1IS-QZ0jIsYo-60tdHkQOFfqGxM7") path = 'front/uploads/' + argtext result = clarifai_api.tag_images(open(path, 'rb')) for tag in result['results'][0]['result']['tag']['classes']: tag = tag.encode('ascii', 'ignore') tagHolder.append(tag) #MongoDB stuff ##Adding stuff print path print tagHolder result = db.photos.insert_one({"path": path, "tags": tagHolder})
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_data(image_dir): ''' Get tags from Clarifai, which are either stored locally or have to be retrieved from the server. ''' filenames = os.listdir(image_dir) data = list() # Loop over all files in the storage directory. for filename in filenames: extension = parse_extension(filename) # Check if the file is an image. if extension in IMAGE_EXTENSIONS: image_filename = image_dir + "/image_" + filename + ".p" image_file = get_file(image_filename) # Check if an image file exists if image_file: image = pickle.load(image_file) data.append(image) else: tags_filename = image_dir + "/" + filename + ".p" tags_file = get_file(tags_filename) # Check if an tags file exists if tags_file: tags = pickle.load(tags_file) image = Image(filename, image_dir, tags) #save_file(image, image_filename) data.append(image) else: # Otherwise create both api_settings = Settings() api = ClarifaiApi(app_id=api_settings.app_id, app_secret=api_settings.app_secret) tags = None with open(image_dir + '/' + filename, 'rb') as image_file: tags = api.tag_images(image_file) save_file(tags, tags_filename) image = Image(filename, image_dir, tags) #save_file(image, image_filename) data.append(image) return data
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 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 get_tags(): #TODO: Error checking common_terms = ['food', 'nobody', 'still life', 'meal', 'dish', 'plate', 'delicious', 'isolated', 'cutout', 'unhealthy', 'one', 'background'] clarifai_api = ClarifaiApi() #img = request.files['file'] #assumes that this data is raw image data img = request.data i = open ('s.jpg', 'wb') i.write(base64.b64decode(img)) i.close() img = open ('s.jpg', 'rb') result = clarifai_api.tag_images(img) #print (str(result)) st = result['results'][0]['result']['tag']['classes'][0:6] for i in common_terms: while i in st: st.remove(i) return json.dumps(search_terms(st))
def test_pil_resizing(self): """ test with image resizing """ image_file = 'tests/data/toddler-flowers.jpeg' api = ClarifaiApi(resize=True) orig = api._resize_image_tuple def mock_resize(*args): return orig(args[1]) with mock.patch('clarifai.client.ClarifaiApi._resize_image_tuple', \ side_effect=mock_resize, autospec=True) as mock_some_method: self.assertTrue(api.resize) if os.path.exists(image_file): with open(image_file, 'rb') as fb: response = api.tag_images(fb) self.assertTrue(response) self.assertTrue(mock_some_method.called)
def imtags(tag_histogram): image_tag_lists = [] clarifai_api = ClarifaiApi() orig_photo_dir = 'pic' if orig_photo_dir[len(orig_photo_dir) - 1] != '/': orig_photo_dir += '/' image_paths = os.listdir(orig_photo_dir) tagdict = {} for path in range(0, len(image_paths)): result = clarifai_api.tag_images(open('pic' +'/'+ image_paths[path], 'rb'))['results'][0]['result']['tag']['classes'] image_tag_lists.append(result) print('Processing...') tagdict[image_paths[path]] = result for tag in result: if (tag in tag_histogram): tag_histogram[tag] += 1 else: tag_histogram[tag] = 1 return tagdict
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)
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. ''' 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.select_classes = select_classes def apply(self, img): data = img.data temp_file = tempfile.mktemp() + '.png' imsave(temp_file, data) tags = self.tagger.tag_images(open(temp_file, 'rb'), select_classes=self.select_classes) os.remove(temp_file) return Value(img, self, {'tags': tags})
def get_tags(): #TODO: Error checking common_terms = [ 'food', 'nobody', 'still life', 'meal', 'dish', 'plate', 'delicious', 'isolated', 'cutout', 'unhealthy', 'one', 'background' ] clarifai_api = ClarifaiApi() #img = request.files['file'] #assumes that this data is raw image data img = request.data i = open('s.jpg', 'wb') i.write(base64.b64decode(img)) i.close() img = open('s.jpg', 'rb') result = clarifai_api.tag_images(img) #print (str(result)) st = result['results'][0]['result']['tag']['classes'][0:6] for i in common_terms: while i in st: st.remove(i) return json.dumps(search_terms(st))
def test_tag_images(self): """ tag multiple images, from url and disk """ # tag images from online URL 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_image_urls(image_urls) self.assertTrue(response) # tag images frmo local fs image_dir = "tests/data" image_files = ["metro-north.jpg", "octopus.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()
def add(filename): tagHolder=[] i=0 #ClarifaiApi stuff pathC = os.path.join("images_raw/",filename) pathM = os.path.join("images/",filename) clarifai_api = ClarifaiApi("Pz-VQR1oFNAMgL8AoiD5zoUXZ3MxdgOP7OFO4TxS", "s0LGYZYf6JDanLdijlczkfAywYWxlVxL3wGLiG9e") result = clarifai_api.tag_images(open(pathC, 'rb')) for tag in result['results'][0]['result']['tag']['classes']: tag = tag.encode('ascii', 'ignore') tagHolder.append(tag) #MongoDB stuff ##Adding stuff print (pathM) print (tagHolder[:5]) result = db.photos.insert_one( { "path":pathM, "tags":tagHolder[:5] } )
def index(): clarifai_api = ClarifaiApi() # assumes environment variables are set. result = clarifai_api.tag_images(open('green_can2.jpg', 'rb')) # pp = pprint.PrettyPrinter(indent = 4) # pp.pprint(result) clarifai_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(tag_results): probability = round(probability_results[index[0]] * 100) tag = str(tag_results[index[0]]) clarifai_results[probability] = tag results = [] for probability in (sorted(clarifai_results, reverse=True)): results.append((clarifai_results[probability], probability)) print results return "o hai."
def main(argv): clarifai_api = ClarifaiApi() # assumes environment variables are set. result1 = clarifai_api.tag_images(open(r'TL.jpg', 'rb')) result2 = clarifai_api.tag_images(open(r'TM.jpg', 'rb')) result3 = clarifai_api.tag_images(open(r'TR.jpg', 'rb')) result4 = clarifai_api.tag_images(open(r'ML.jpg', 'rb')) result5 = clarifai_api.tag_images(open(r'MM.jpg', 'rb')) result6 = clarifai_api.tag_images(open(r'MR.jpg', 'rb')) result7 = clarifai_api.tag_images(open(r'LL.jpg', 'rb')) result8 = clarifai_api.tag_images(open(r'LM.jpg', 'rb')) result9 = clarifai_api.tag_images(open(r'LR.jpg', 'rb')) p1=result1['results'][0]['result']['tag']['classes'] p2=result2['results'][0]['result']['tag']['classes'] p3=result3['results'][0]['result']['tag']['classes'] p4=result4['results'][0]['result']['tag']['classes'] p5=result5['results'][0]['result']['tag']['classes'] p6=result6['results'][0]['result']['tag']['classes'] p7=result7['results'][0]['result']['tag']['classes'] p8=result8['results'][0]['result']['tag']['classes'] p9=result9['results'][0]['result']['tag']['classes'] f = open("tag","r") myItem = f.read() p = open("pos","r") pos = p.read().split('|') x0 = int(pos[0].split(',')[0]) y0 = int(pos[0].split(',')[1]) x1 = int(pos[1].split(',')[0]) y1 = int(pos[1].split(',')[1]) x2 = int(pos[2].split(',')[0]) y2 = int(pos[2].split(',')[1]) x3 = int(pos[3].split(',')[0]) y3 = int(pos[3].split(',')[1]) x4 = int(pos[4].split(',')[0]) y4 = int(pos[4].split(',')[1]) x5 = int(pos[5].split(',')[0]) y5 = int(pos[5].split(',')[1]) x6 = int(pos[6].split(',')[0]) y6 = int(pos[6].split(',')[1]) x7 = int(pos[7].split(',')[0]) y7 = int(pos[7].split(',')[1]) x8 = int(pos[8].split(',')[0]) y8 = int(pos[8].split(',')[1]) if myItem in p1: click(x0,y0) print("TopLeft") if myItem in p2: click(x1,x1) print("TopMiddle") if myItem in p3: click(x2,x2) print("TopRight") if myItem in p4: click(x3,x3) print("MiddleLeft") if myItem in p5: click(x4,x4) print("MiddleMiddle") if myItem in p6: click(x5,x5) print("MiddleRight") if myItem in p7: click(x6,x6) print("LeftLeft") if myItem in p8: click(x7,y7) print("LeftMiddle") if myItem in p9: click(x8,y8) print("LeftRight") click(x8,y8+60)
from clarifai.client import ClarifaiApi import os, json import PIL os.environ["CLARIFAI_APP_ID"]="QazpKOnoaYgNshSDVw4mGGENejQiVWTvWxJMxpyn" os.environ["CLARIFAI_APP_SECRET"]="8Lin5aSiWpTj201phfBlN5-Bg0lu7reJQFLHn5Zh" clarifai_api = ClarifaiApi() # assumes environment variables are set. #i=PIL.Image.open('./images/CRfFpSSVAAAtgZc.jpg') #i.show() result = clarifai_api.tag_images(open('./images/CRfFpSSVAAAtgZc.jpg')) #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] print(d)
""" You must have clarifai installed: pip install git+git://github.com/Clarifai/clarifai-python.git Do this before running: export CLARIFAI_APP_ID=<an_application_id_from_your_account> export CLARIFAI_APP_SECRET=<an_application_secret_from_your_account> """ from clarifai.client import ClarifaiApi clarifai_api = ClarifaiApi() # assumes environment variables are set. result = clarifai_api.tag_images(open('/home/marcela/Repos/vision/treeDetection/img_in/tree2.png', 'rb')) print result #import pdb; pdb.set_trace()
urlget = 'https://api.particle.io/v1/devices/310047000447343232363230/spot1?access_token=f8093528e7b81caceeaecd0569423df524dffbab' while True: if io.input(pir_pin): print "Motion Detected!" print "Taking Picture!" existing_files = glob.glob(DIR + '*.jpg') filename = DIR + 'image_%d.jpg' %(len(existing_files) + 1) camera.start_preview() camera.capture(filename, resize = (1024,768)) camera.stop_preview() print "Picture Obtained!" print "Analyzing!" result = clarifai_api.tag_images(open(filename, 'rb')) # print result print "done processing results from clarifai" print "results:" length_results = len(result['results'][0]['result']['tag']['classes']) for i in range(length_results): tag = result['results'][0]['result']['tag']['classes'][i] # print result['results'][0]['result']['tag']['classes'][i] for j in range(length_category): # print category[j] if tag == category[j]: print "Car Detected!" print "Triggering Photon!" res = requests.post(urltrigger, data=query)
import PIL, json, itertools from bson import json_util, ObjectId from pandas.io.json import json_normalize from pandas import DataFrame clarifai_api = ClarifaiApi('T3DIgYtEokkSCQnGMNpd58cofZZJ123zmpJpbiHy', 'LgpBH4KHlSx0MVtu1dte-N10eIxlBwm_HPhW627z') # result = clarifai_api.tag_image_urls(['http://www.gstatic.com/webp/gallery/1.jpg', # 'http://www.gstatic.com/webp/gallery/2.jpg', # 'http://www.gstatic.com/webp/gallery/3.jpg', # 'http://www.gstatic.com/webp/gallery/4.jpg', # 'http://www.gstatic.com/webp/gallery/5.jpg']) result = clarifai_api.tag_images([ open('D:\Kaggle\OCR\Watermark\chest-xray-111026-02.jpg', 'rb'), open('D:\\Kaggle\\OCR\\Watermark\\3.jpg', 'rb') ]) sanitized = json.loads(json_util.dumps(result['results'])) normalized = json_normalize(sanitized) df = pd.DataFrame(normalized) res = df.to_dict() data = [] for x in res['docid_str'].keys(): data.append( itertools.izip_longest([res['docid_str'][x]], res['result.tag.classes'][x], res['result.tag.concept_ids'][x], res['result.tag.probs'][x],
from clarifai.client import ClarifaiApi clarifai_api = ClarifaiApi() # assumes environment variables are set. result = clarifai_api.tag_images(open(‘capstone/tests/data/metro-north.jpg’, 'rb'))
nutri['Calorias'] = 1 else : nutri['Calorias'] = 0 if res['Grasas_total'] < 0.5: nutri['Grasas_total'] = 1 else: nutri['Grasas_total'] = 0 if ( res['VitaminB6'] + res['Potasio'] + res['VitaminF'] + res['VitaminD'] + res['VitaminE']+ res['VitaminC'] + res['VitaminA'] + res['VitaminB12'] + res['Sodio'] + res['Hierro'] + res['Magnesio'] + res['Calcio'] ) > 200: nutri["Vitaminas"] = 1 else: nutri["Vitaminas"] = 0 if res['Proteinas'] > 0.55: nutri['Proteinas'] = 1 else: nutri['Proteinas'] = 0 return nutri alimento = raw_input("Dame un alimento de la carpeta fotos:") try: result = clarifai_api.tag_images(open(photosdir + alimento, 'rb')) mylist = result['results'][0]['result']['tag']['classes'] res = get_nutri_facts(mylist) hly = is_healthy(res) print hly except: print "Error con la foto."
onlyfiles = [f for f in listdir(".") if isfile(join(".", f))] api = ClarifaiApi() imgs = [] for f in onlyfiles: if "jpg" in f: imgs.append("./" + f) beg = int(argv[1]) end = int(argv[2]) n = argv[3] own = argv[4] singular = argv[5] results = [] for i in imgs[beg:end]: results.append(api.tag_images([open(i)])) words = results[0]["results"][0]["result"]["tag"]["classes"] try: words += results[1]["results"][0]["result"]["tag"]["classes"] except: print "second image blows" sentence = "" for w in words: sentence += str(w) + " " text = nltk.word_tokenize(sentence) tags = nltk.pos_tag(text)
from clarifai.client import ClarifaiApi from random import randint #include OAUTH requirements clarifai_api = ClarifaiApi() result = clarifai_api.tag_images([ open('/Users/adamhalfaker/Documents/PhotosForAI/IMG_0835.JPG', 'rb'), open('/Users/adamhalfaker/Documents/PhotosForAI/IMG_0851.JPG', 'rb'), open('/Users/adamhalfaker/Documents/PhotosForAI/IMG_0873.JPG', 'rb'), open('/Users/adamhalfaker/Documents/PhotosForAI/IMG_0878.JPG', 'rb'), open('/Users/adamhalfaker/Documents/PhotosForAI/IMG_0912.JPG', 'rb'), open('/Users/adamhalfaker/Documents/PhotosForAI/IMG_0926.JPG', 'rb'), open('/Users/adamhalfaker/Documents/PhotosForAI/IMG_0943.JPG', 'rb') ]) wordList = [] finalWordList = [] for i in range(7): listInput = result['results'][i]['result']['tag']['classes'] for i in range(len(listInput) - 1): if listInput[i] != 'no person': finalWordList.append(listInput[i]) wordList.append(finalWordList) haikuWords1 = wordList[0] haikuWords2 = wordList[1] haikuWords3 = wordList[2] haikuWords4 = wordList[3] haikuWords5 = wordList[4] haikuWords6 = wordList[5]
directory = '/Users/RichaShah/Documents/LocalHackDay/Images/' # directory = '/Users/RichaShah/Documents/Personal/Photos/December 2014 Trip Sorted Photos/Richa\'s iPad/12-28 Big Sur' number_of_files = len([ item for item in os.listdir(directory) if os.path.isfile(os.path.join(directory, item)) ]) tags = "" for item in os.listdir(directory): if os.path.isfile(os.path.join(directory, item)): file_path = '/Users/RichaShah/Documents/LocalHackDay/Images/' file_path += str(item) # print file_path filter_words = [ 'women', 'woman', ' man', ' men', 'adult', 'wear', 'clothing', ' one', 'two', 'three', 'four', 'boy', 'girl' ] result = clarifai_api.tag_images(open(file_path, 'rb')) for word in result["results"][0]['result']['tag']['classes']: if word not in filter_words: tags += json.dumps(word).replace('"', "") tags += " " print tags wordcloud = WordCloud().generate(tags) img = plt.imshow(wordcloud) plt.axis("off") plt.show()
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
class ImageRecognition(object): ''' Class to recognize images using the ClarifaiAPI tag_image_url: tags a images that is given in a url (http://*.* or https://*.*) returns: json tag_image: tags a image from wich the path is given (/*/*.jpg, /*/*.jpeg, /*/*.png, etc...) returns: json tag_images_dir: tags all the images in a directory given (/*/) returns: json get_most_likely: returns the most likely result from the json object returned by on of the functions above returns: list Example: img_recon.get_most_likely(img_recon.tag_image('/root/Documents/dog1.jpeg'), r=4) returns: [u'dog', u'pet', u'canine', u'mammal', u'puppy'] ''' def __init__(self, app_id='NXSqUv3U17C0iglZLv0OQIb81h6-LBdWa5OLHDQG', app_secret='gDxYm_QgzUEu0_Dp20S0RzYWSF8EkFfP0ierQoPu'): """ Setsup the ClarifaiAPI :param app_id: ID of the clarifai app :param app_secret: SECRET of the clarifai app """ self.api = ClarifaiApi(app_id=app_id, app_secret=app_secret) def tag_image_url(self, url): """ Tags a image on the web :param url: A url (http://*.* or https://*.*) :return: returns a json string with the api output from the clarifaiAPI """ return self.api.tag_image_urls(url) def tag_image(self, image): """ Tags a image stored on the local machine :param image: Path to the image (/*/*/*.jpg, /*/*/*.jpeg, /*/*/*.png) :return: returns a json string with the api output from the clarifaiAPI """ with open(image, 'rb') as image_file: return self.api.tag_images(image_file) def tag_images_dir(self, path): """ Tags all images in a directory :param path: Path to the directory (/*/*/) :return: returns a json string with the api output from the clarifaiAPI """ images = [] path = path.rstrip(os.sep) for fname in os.listdir(path): images.append((open(os.path.join(path, fname), 'rb'), fname)) return self.api.tag_images(images) def get_most_likely(self, response): """ Filter a json response of the ClarifaiAPI for the most likeliest match :param response: A string with json :return: The most likeliest match """ data = json.loads(json.dumps(response)) most_likeliest = data["results"][0]['result']['tag']['classes'] return most_likeliest[0]
class Video_Tag_Extract(): def __init__(self, video_name): self.working_root = '..' self.videos_root = '../training_videos' self.images_root = '../training_images' self.json_root = '../jsons' self.video_name = video_name self.modulus = 20 self.api = ClarifaiApi() def extract_images_from_video(self, video_start, video_end, job_id): """ input self.video_name output json file """ # set up video_range = range(video_start, video_end) cap = cv2.VideoCapture(os.path.join(self.videos_root, self.video_name)) num_frames = cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT) fps = cap.get(cv2.cv.CV_CAP_PROP_FPS) self.modulus = int(1.5*fps) print self.modulus width = cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH) height = cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT) print width print height # assert False cur_frame = 0 while cur_frame < num_frames: print cur_frame has_frame, frame = cap.read() # frame is (height, width, channels) if cur_frame in video_range: cv2.imshow('frame', frame) # write frame if (cur_frame - video_start) % self.modulus == 0: img_filename = os.path.join(self.images_root, self.video_name[:self.video_name.find('.mp4')] + '_' + str(cur_frame) + '_job_id=' + str(job_id) + '.jpg') cv2.imwrite(img_filename, frame) print 'Written' k = cv2.waitKey(30) & 0xff if k == 27: break cur_frame += 1 cv2.destroyAllWindows() cap.release() def analyze_images(self, job_id): # analyze images in batch image_list = [x for x in os.listdir(self.images_root) if 'job_id=' + str(job_id) + '.jpg' in x][:self.api.get_info()['max_batch_size']] result = self.api.tag_images([open(os.path.join(self.images_root, x)) for x in image_list])['results'] info = {} for idx in range(len(result)): info[str(idx)] = result[idx]['result']['tag'] with open('data.json', 'w') as fp: json.dump(info, fp) def remove_images(self, video_start, video_end, job_id): image_list = [x for x in os.listdir(self.images_root) if 'job_id=' + str(job_id) + '.jpg' in x]#[:self.api.get_info()['max_batch_size']] for img_name in image_list: os.system('rm ' + os.path.join(self.images_root, img_name))
# print file_path filter_words = [ "women", "woman", " man", " men", "adult", "wear", "clothing", " one", "two", "three", "four", "boy", "girl", ] result = clarifai_api.tag_images(open(file_path, "rb")) for word in result["results"][0]["result"]["tag"]["classes"]: if word not in filter_words: tags += json.dumps(word).replace('"', "") tags += " " print tags wordcloud = WordCloud().generate(tags) img = plt.imshow(wordcloud) plt.axis("off") plt.show()
import json, urllib, unirest, re from sys import argv from random import randint from clarifai.client import ClarifaiApi api = ClarifaiApi() f_name = argv[1] matcher = re.compile('([^n\/]\w+)') results = api.tag_images([open(f_name)]) results = results['results'][0]['result']['tag']['classes'] titles = [] for r in results: encoded_tag = urllib.quote(r.lower(), '') # print encoded_tag uri = 'http://conceptnet5.media.mit.edu/data/5.4/c/en/' + encoded_tag limit = 10 response = unirest.get(uri, headers= { "Accept": "text/plain" }, params={ 'limit': limit }) try: for e in response.body['edges']:
from __future__ import print_function from clarifai.client import ClarifaiApi import sys for i in range(1, 5): imageName = sys.argv[1] + "_" + str(i) + ".png" opFileName = sys.argv[1] + "_" + str(i) + ".txt" clarifai_api = ClarifaiApi() # assumes environment variables are set. result = clarifai_api.tag_images(open(imageName, 'rb')) outputFile = open(opFileName, "w") print(result, file=outputFile)
#!/usr/bin/env python #-*- coding: utf-8 -*- from clarifai.client import ClarifaiApi clarifai_api = ClarifaiApi() # assumes environment variables are set. result = clarifai_api.tag_images(open('tests/data/metro-north.jpg', 'rb')) print result['results'][0]['result']['tag']['classes']
from clarifai.client import ClarifaiApi import sys import os import shelve shelf = shelve.open('tag_shelf') directory_path = str(sys.argv[1]) #get the directory path clarifai_api = ClarifaiApi() # assumes environment variables are set. for f in os.listdir(directory_path): if f.endswith(".png") or f.endswith(".jpeg") or f.endswith(".jpg"): image_path = str(os.path.join(directory_path, f)) response = clarifai_api.tag_images(open(image_path, 'rb')) tag_list = response['results'][0]['result']['tag']['classes'][:6] for t in tag_list: if shelf.has_key(t): if image_path not in shelf[t]: temp = shelf[t] temp.append(image_path) shelf[t] = temp else: shelf[t] = [image_path] shelf.close()