-
Notifications
You must be signed in to change notification settings - Fork 4
/
storage.py
295 lines (243 loc) · 10.6 KB
/
storage.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import json
import os
from PySide6.QtCore import QObject, Signal
from platformdirs import user_data_dir
from defaults import default_info_for_box_name, normalize_settings_dict
from text_detection_target import TextDetectionTarget
from sc_logging import logger
data_subscribers = {}
def subscribe_to_data(file_path, document_name, callback):
# Subscribe to data changes in a JSON file
# prepend the user data directory
file_path = os.path.join(user_data_dir("scoresight"), file_path)
if file_path not in data_subscribers:
data_subscribers[file_path] = {}
if document_name not in data_subscribers[file_path]:
data_subscribers[file_path][document_name] = []
data_subscribers[file_path][document_name].append(callback)
def store_data(file_path, document_name, data):
# Store data into a JSON file
# get the user data directory
data_dir = user_data_dir("scoresight")
if not os.path.exists(data_dir):
os.makedirs(data_dir)
# prepend the user data directory
file_path = os.path.join(data_dir, file_path)
if os.path.exists(file_path):
with open(file_path, "r") as f:
try:
documents = json.load(f)
except json.JSONDecodeError:
documents = {}
else:
documents = {}
documents[document_name] = data
# notify subscribers
if file_path in data_subscribers and document_name in data_subscribers[file_path]:
for callback in data_subscribers[file_path][document_name]:
callback(data)
with open(file_path, "w") as f:
json.dump(documents, f, indent=2)
def remove_data(file_path, document_name):
# Remove data from a JSON file
# prepend the user data directory
file_path = os.path.join(user_data_dir("scoresight"), file_path)
if not os.path.exists(file_path):
return
with open(file_path, "r") as f:
documents = json.load(f)
if document_name in documents:
del documents[document_name]
# notify subscribers
if file_path in data_subscribers and document_name in data_subscribers[file_path]:
for callback in data_subscribers[file_path][document_name]:
callback(None)
with open(file_path, "w") as f:
json.dump(documents, f, indent=2)
def fetch_data(file_path, document_name, default=None):
# Fetch data from a JSON file
# prepend the user data directory
file_path = os.path.join(user_data_dir("scoresight"), file_path)
if not os.path.exists(file_path):
return default
with open(file_path, "r") as f:
try:
documents = json.load(f)
except json.JSONDecodeError:
return default
if document_name in documents:
return documents[document_name]
else:
return default
def store_custom_box_name(custom_box_name: str):
# get the current custom box names
custom_boxes_names = fetch_data("scoresight.json", "custom_boxes_names", [])
if custom_box_name in custom_boxes_names:
return
custom_boxes_names.append(custom_box_name)
# Store the custom box name in the scoresight.json file
store_data("scoresight.json", "custom_boxes_names", custom_boxes_names)
def rename_custom_box_name_in_storage(old_name: str, new_name: str):
# get the current custom box names
custom_boxes_names = fetch_data("scoresight.json", "custom_boxes_names", [])
if old_name in custom_boxes_names:
custom_boxes_names.remove(old_name)
custom_boxes_names.append(new_name)
# Store the custom box name in the scoresight.json file
store_data("scoresight.json", "custom_boxes_names", custom_boxes_names)
def remove_custom_box_name_in_storage(custom_box_name: str):
# get the current custom box names
custom_boxes_names = fetch_data("scoresight.json", "custom_boxes_names", [])
if custom_box_name in custom_boxes_names:
custom_boxes_names.remove(custom_box_name)
# Store the custom box name in the scoresight.json file
store_data("scoresight.json", "custom_boxes_names", custom_boxes_names)
def fetch_custom_box_names():
return fetch_data("scoresight.json", "custom_boxes_names", [])
class TextDetectionTargetMemoryStorage(QObject):
# This class is used to store the text detection targets in memory
data_changed = Signal(list)
def __init__(self):
super().__init__()
self._data: list[TextDetectionTarget] = []
def add_item(self, item: TextDetectionTarget):
self._data.append(item)
self.data_changed.emit(self._data)
def remove_item(self, item_name: str):
for i, item in enumerate(self._data):
if item.name == item_name:
del self._data[i]
break
self.data_changed.emit(self._data)
def clear(self):
self._data.clear()
self.data_changed.emit(self._data)
def edit_item(self, item_name: str, new_item: TextDetectionTarget):
for i, item in enumerate(self._data):
if item.name == item_name:
self._data[i].setRect(
new_item.x(), new_item.y(), new_item.width(), new_item.height()
)
self._data[i].settings = new_item.settings
self.data_changed.emit(self._data)
return
logger.warn("unable to find item to edit in storage: " + item_name)
def rename_item(self, old_name: str, new_name: str):
for i, item in enumerate(self._data):
if item.name == old_name:
self._data[i].name = new_name
self.data_changed.emit(self._data)
return True
logger.warn("unable to find item to rename in storage: " + old_name)
return False
def get_data(self):
return self._data
def is_empty(self):
return len(self._data) == 0
def find_item_by_name(self, name: str):
for item in self._data:
if item.name == name:
return item
return None
def loadBoxesFromStorage(self) -> bool:
# load the boxes from scoresight.json
boxes = fetch_data("scoresight.json", "boxes")
if not boxes:
return False
return self.loadBoxesFromDict(boxes)
def loadBoxesFromFile(self, file_path) -> bool:
# load the boxes from a file
with open(file_path, "r") as f:
boxes = json.load(f)
return self.loadBoxesFromDict(boxes)
def loadBoxesFromDict(self, boxes) -> bool:
data_backup = self._data.copy()
self._data.clear()
try:
for box in boxes:
logger.debug("loading box: " + box["name"])
if "settings" not in box:
box["settings"] = {}
default_box_info = default_info_for_box_name(box["name"])
# set the position of the box
self._data.append(
TextDetectionTarget(
box["rect"]["x"],
box["rect"]["y"],
box["rect"]["width"],
box["rect"]["height"],
box["name"],
normalize_settings_dict(box["settings"], default_box_info),
)
)
if "is_custom" in box["settings"] and box["settings"]["is_custom"]:
store_custom_box_name(box["name"])
logger.debug("loaded boxes")
self.data_changed.emit(self._data)
except Exception as e:
logger.error("error loading boxes: " + str(e))
self._data = data_backup
return False
return True
def getBoxesForStorage(self):
# save all the boxes to scoresight.json
boxes = []
for detectionTarget in self._data:
detectionTarget.settings = normalize_settings_dict(
detectionTarget.settings,
default_info_for_box_name(detectionTarget.name),
)
boxes.append(
{
"name": detectionTarget.name,
"rect": {
"x": detectionTarget.x(),
"y": detectionTarget.y(),
"width": detectionTarget.width(),
"height": detectionTarget.height(),
},
"settings": {
"is_custom": detectionTarget.settings.get("is_custom"),
"obs_source_name": detectionTarget.settings.get(
"obs_source_name"
),
"format_regex": detectionTarget.settings.get("format_regex"),
"smoothing": detectionTarget.settings.get("smoothing"),
"skip_empty": detectionTarget.settings.get("skip_empty"),
"conf_thresh": detectionTarget.settings.get("conf_thresh"),
"cleanup_thresh": detectionTarget.settings.get(
"cleanup_thresh"
),
"dilate": detectionTarget.settings.get("dilate"),
"skew": detectionTarget.settings.get("skew"),
"vscale": detectionTarget.settings.get("vscale"),
"autocrop": detectionTarget.settings.get("autocrop"),
"skip_similar_image": detectionTarget.settings.get(
"skip_similar_image"
),
"remove_leading_zeros": detectionTarget.settings.get(
"remove_leading_zeros"
),
"rescale_patch": detectionTarget.settings.get("rescale_patch"),
"normalize_wh_ratio": detectionTarget.settings.get(
"normalize_wh_ratio"
),
"invert_patch": detectionTarget.settings.get("invert_patch"),
"dot_detector": detectionTarget.settings.get("dot_detector"),
"ordinal_indicator": detectionTarget.settings.get(
"ordinal_indicator"
),
"binarization_method": detectionTarget.settings.get(
"binarization_method"
),
},
}
)
return boxes
def saveBoxesToFile(self, file_path):
boxes = self.getBoxesForStorage()
with open(file_path, "w") as f:
json.dump(boxes, f, indent=2)
def saveBoxesToStorage(self):
boxes = self.getBoxesForStorage()
store_data("scoresight.json", "boxes", boxes)