-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
48 lines (43 loc) · 1.22 KB
/
api.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import sys
from pymongo import MongoClient
from clarifai.client import ClarifaiApi
client = MongoClient()
db = client.ThinkingGame
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 getAllPaths():
paths = []
cursor = db.photos.find({})
for document in cursor:
paths.append(document["path"])
return paths
def getAllTags(path):
cursor = db.photos.find({"path":path})
return cursor[0]['tags']
# if __name__ == "__main__":
# #for adding to the db
# for file in os.listdir('images_raw'):
# print(file)
# add(file)
# print(getAllPaths())