-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseOps.py
170 lines (84 loc) · 3.32 KB
/
DatabaseOps.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -*- coding: utf-8 -*-
"""
Created on Thu May 23 16:35:25 2019
@author: GELab
"""
###one thread do the follwing :
### need to first signal the data not being present
### then need to take pictures and save it in the folder with labels
### generate pickle with required format with convertToRequired and juans code
## restore the partial model and train before making an inference
###another thread keep inferencing
### merge thread when training is done
import sqlite3
from datetime import datetime
import pickle
from ImgObjClass import Item,Img
def imgObjReverseDBConvertor(ImgObjStr):
return pickle.loads(ImgObjStr)
sqlite3.register_adapter(Img, imgObjReverseDBConvertor)
###time functions
def getTimeObjFrmStr(timestampString):
return datetime.strptime(timestampString, '%Y-%m-%d %H:%M:%S.%f')
def getConnection(database):
conn = sqlite3.connect('./Database/'+database+'.db')
return conn
# Insert a row of data
def insertIntoTable(conn,imageObj):
queryStr = 'INSERT INTO IMAGE_INFO \
(file_name,image_obj,obj_label,added_at_timestamp,used_by_model) \
VALUES (?,?,?,?,?)'
if len(imageObj.objects) != 0: ## if there are no objects dont store
fileName = imageObj.name
pickledImgObj = pickle.dumps(imageObj)
Label = imageObj.objects[0].label
timestamp = str(datetime.now())
used_by_model = ''
values = (fileName,pickledImgObj,Label,timestamp,used_by_model)
try :
conn.execute(queryStr,values)
except Exception as e:
print(e, imageObj.name)
return True
def fetchImgObjs(conn,fromTable):
conn.row_factory = sqlite3.Row
cursor = conn.execute('select * from '+ fromTable)
rows = cursor.fetchall()
ImgObjects = [pickle.loads(row['image_obj']) for row in rows]
return ImgObjects
def getUniqueLabels(conn,fromTable):
cursor = conn.execute('select distinct obj_label from '+ fromTable)
rows = cursor.fetchall()
reqFormat = [tup[0] for tup in rows]
return reqFormat
def dbInsertWrapper(imgObjList):
conn = getConnection('ObjectsDB')
for img in imgObjList:
insertIntoTable(conn,img)
# Save (commit) the changes
conn.commit()
conn.close()
return True
if __name__ == '__main__':
"""
# Create table
conn.execute('''CREATE TABLE IMAGE_INFO
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
file_name text UNIQUE,
image_obj blob,
obj_label text,
added_at_timestamp text,
used_by_model text)''')
"""
conn = getConnection('ObjectsDB')
conn.execute("delete from IMAGE_INFO where obj_label like '%lemon%'")
# getUniqueLabels(conn,'IMAGE_INFO')
AllImgs = pickle.load(open('./Data/AllCarrot.pickle','rb'))
for img in AllImgs:
insertIntoTable(conn,img)
# Save (commit) the changes
conn.commit()
# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.close()
pass