This repository has been archived by the owner on Aug 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson2sql.py
100 lines (94 loc) · 2.72 KB
/
json2sql.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import json
import sqlite3
# import json2sql
jsonFile = "./data/annotations.json"
sqliteFile = "./data/data.sqlite"
logFile = "./data/json2sqlLog.txt"
jsonDict = json.load(open(jsonFile,"r"))
connection = sqlite3.connect(sqliteFile)
log = open(logFile,"w")
annotations = jsonDict["annotations"]
licenses = jsonDict["licenses"]
images = jsonDict["images"]
categories = jsonDict["categories"]
session = connection.cursor()
# session.execute("""
# CREATE TABLE ANNONTATION(
# ID INT PRIMARY KEY NOT NULL,
# imageID INT,
# categoryID INT,
# segmentation BLOB,
# area REAL,
# bbox BLOB
# )
# """)
# session.execute("""
# CREATE TABLE IMAGES(
# ID INT PRIMARY KEY NOT NULL,
# width INT,
# height INT,
# fileName TEXT,
# license TEXT,
# flickrUrl TEXT,
# cocoUrl TEXT,
# dataCaptured BLOB,
# flickr640Url TEXT
# )
# """)
# session.execute("""
# CREATE TABLE CATEGORIES(
# ID INT PRIMARY KEY NOT NULL,
# supercategory TEXT,
# name TEXT
# )
# """)
# print("insert annotations.")
# for line in annotations:
# ID = line["id"]
# imagesID = line["image_id"]
# categoryID = line["category_id"]
# segmentation = line["segmentation"]
# area = line["area"]
# bbox = line["bbox"]
# data = [ID,imagesID,categoryID,json.dumps(segmentation),area,json.dumps(bbox)]
# try:
# session.execute("INSERT INTO ANNONTATION VALUES (?,?,?,?,?,?)",data)
# except Exception as e:
# print("when INSERT INTO ANNONTATION :",data[0]," error\n",str(e),file=log)
# continue
# connection.commit()
# print(data)
print("insert images.")
for line in images:
data = [
line["id"],
line["width"],
line["height"],
line["file_name"],
line["license"],
line["flickr_url"],
line["coco_url"],
line["date_captured"],
line["flickr_640_url"]
]
try:
session.execute("INSERT INTO IMAGES VALUES (?,?,?,?,?,?,?,?,?)", data)
except Exception as e:
print("when INSERT INTO IMAGES: ",data[0]," error\n",str(e),file=log)
continue
connection.commit()
print(data)
print("insert categories.")
for line in categories:
data = [
line["id"],
line["supercategory"],
line["name"]
]
try:
session.execute("INSERT INTO CATEGORIES VALUES (?,?,?)", data)
except Exception as e:
print("when INSERT INTO CATEGORIES: ",data[0]," error\n",str(e),file=log)
continue
connection.commit()
print(data)