-
Notifications
You must be signed in to change notification settings - Fork 1
/
load.py
53 lines (42 loc) · 1.42 KB
/
load.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
import psycopg2
import os
import time
def process_cal256(root_dir):
subdirs_files_dict = {}
for dirpath, dirnames, filenames in os.walk(root_dir):
if dirpath == root_dir:
continue
subdir_name = os.path.basename(dirpath)
file_list = filenames
subdirs_files_dict[subdir_name] = file_list
return subdirs_files_dict
if __name__=='__main__':
files = process_cal256('256_ObjectCategories')
conn = psycopg2.connect(
host="localhost",
database="",
user="",
password="",
port=5432
)
cur = conn.cursor()
cur.execute('''
CREATE TABLE IF NOT EXISTS image_table (
v REAL[],
location VARCHAR,
id SERIAL PRIMARY KEY
);
''')
conn.commit()
abs_start = time.time()
for i, k in enumerate(sorted(list(files.keys()))):
start = time.time()
for im in files[k]:
path = f'{os.getcwd()}/256/{k}/{im}'
insert_query = f'INSERT INTO image_table (v, location) VALUES (clip_image(\'{path}\'), \'{path}\');'
cur.execute(insert_query)
conn.commit()
end = time.time()
print(f'completed {k} in {end-start} second, {end-abs_start} elapsed so far')
cur.execute('CREATE INDEX semantic_image ON image_table USING hnsw (v dist_cos_ops) WITH (M=5, ef=30, ef_construction=30);')
conn.commit()