-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
72 lines (51 loc) · 1.42 KB
/
database.py
File metadata and controls
72 lines (51 loc) · 1.42 KB
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
70
71
72
import os
import argparse
import sqlite3
BASE_DIR = r'datasets'
def create_DB(db_name):
conn = sqlite3.connect(f"databases/{db_name}")
c = conn.cursor()
c.execute(f"DROP TABLE IF EXISTS images")
c.execute('''
CREATE TABLE images (
id INTEGER PRIMARY KEY AUTOINCREMENT,
image_name TEXT,
image BLOB
)
''')
conn.commit()
conn.close()
def insert_image(image_path, db_name):
conn = sqlite3.connect(f"databases/{db_name}")
connection = conn.cursor()
with open(image_path, 'rb') as f:
image_blob = f.read()
image_name = image_path.split("/")[-1]
connection.execute('''
INSERT INTO images (image_name, image)
VALUES (?, ?)
''', (image_name, image_blob))
conn.commit()
conn.close()
def parse():
parser = argparse.ArgumentParser()
parser.add_argument('dataset', type=str, help='Name of the dataset')
args = parser.parse_args()
return args
def main(name):
db_name = f"{name}.db"
create_DB(db_name)
images_dir = os.path.join(BASE_DIR, name)
for img in os.listdir(images_dir):
img_path = os.path.join(images_dir, img)
if not os.path.isfile(img_path):
continue
insert_image(img_path, db_name)
if __name__ == '__main__':
args = parse()
db_name = f"{args.dataset}.db"
create_DB(db_name)
images_dir = os.path.join(BASE_DIR, args.dataset)
for img in os.listdir(images_dir):
img_path = os.path.join(images_dir, img)
insert_image(img_path, db_name)