本文整理汇总了Python中clarifai.client.ClarifaiApi.set_model方法的典型用法代码示例。如果您正苦于以下问题:Python ClarifaiApi.set_model方法的具体用法?Python ClarifaiApi.set_model怎么用?Python ClarifaiApi.set_model使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类clarifai.client.ClarifaiApi
的用法示例。
在下文中一共展示了ClarifaiApi.set_model方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ClarifaiAPIExtractor
# 需要导入模块: from clarifai.client import ClarifaiApi [as 别名]
# 或者: from clarifai.client.ClarifaiApi import set_model [as 别名]
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'])