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