-
Notifications
You must be signed in to change notification settings - Fork 5
/
accumulator.py
34 lines (28 loc) · 933 Bytes
/
accumulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Clarifai Image Tag Accumulator
# to run, follow instructions on: https://github.com/Clarifai/clarifai-python
import json
from collections import defaultdict
from clarifai.client import ClarifaiApi
clarifai_api = ClarifaiApi()
# initialize tag dictionary for tags and their frequencies
d = defaultdict(int)
taglist = []
iter = 0
# read and process each url
with open("imglist.txt") as f:
imglist = f.readlines()
# get tags using clarifai, add tags to taglist
for line in imglist:
result = clarifai_api.tag_image_urls(line.rstrip('\n'))
tags = result['results'][0]['result']['tag']['classes']
for i in range(0,len(tags)):
if type(tags[i]) is str:
taglist.append(tags[i])
print("next iter:" + str(iter))
iter += 1
# convert taglist to dictionary
for x in range(0,len(taglist)):
d[taglist[x]] += 1
# print dictionary
for w in sorted(d, key=d.get, reverse=True):
print(w, d[w])