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'])
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 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_")
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})