-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
69 lines (54 loc) · 1.77 KB
/
utils.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
import os
import json
import hashlib
from functools import lru_cache
import pymongo
from pymongo.collection import Collection
import clip
@lru_cache(maxsize=1)
def get_config():
with open('config.json') as json_data_file:
c = json.load(json_data_file)
if "clip-model" in c:
assert c["clip-model"] in clip.available_models()
if "device" in c:
assert c["device"] in ["cuda", "cpu"]
return c
def get_feature_size(model_name):
if model_name == "ViT-B/32":
return 512
elif model_name == "ViT-L/14":
return 768
else:
raise ValueError("Unknown model") # TODO: complete this table
def get_file_type(image_path):
libmagic_output = os.popen("file '" + image_path + "'").read().strip()
libmagic_output = libmagic_output.split(":", 1)[1]
if "PNG" in libmagic_output:
return "png"
if "JPEG" in libmagic_output:
return "jpg"
if "GIF" in libmagic_output:
return "gif"
if "PC bitmap" in libmagic_output:
return "bmp"
return None
@lru_cache(maxsize=1)
def get_mongo_collection() -> Collection:
config = get_config()
mongo_client = pymongo.MongoClient("mongodb://{}:{}/".format(config['mongodb-host'], config['mongodb-port']))
mongo_collection = mongo_client[config['mongodb-database']][config['mongodb-collection']]
return mongo_collection
def calc_md5(filepath):
with open(filepath, 'rb') as f:
md5 = hashlib.md5()
while True:
data = f.read(4096)
if not data:
break
md5.update(data)
return md5.hexdigest()
def get_full_path(basedir, basename):
md5hash, ext = basename.split(".")
return "{}/{}/{}/{}".format(basedir, ext, md5hash[:2], basename)