Skip to content

Commit

Permalink
Merge pull request #23 from occ-ai/roy.fix_loading_customs_boxes
Browse files Browse the repository at this point in the history
chore: Update storage.py and defaults.py
  • Loading branch information
royshil committed Jul 20, 2024
2 parents c22794d + ea05353 commit 65a2165
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
14 changes: 13 additions & 1 deletion defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class FieldType:
"obs_source_name": "Home score",
"format_regex": format_prefixes[5],
"ordinal_indicator": False,
"is_custom": False,
},
{
"name": "Away Score",
Expand All @@ -58,6 +59,7 @@ class FieldType:
"obs_source_name": "Away score",
"format_regex": format_prefixes[5],
"ordinal_indicator": False,
"is_custom": False,
},
{
"name": "Time",
Expand All @@ -69,6 +71,7 @@ class FieldType:
"obs_source_name": "Clock",
"format_regex": format_prefixes[0],
"ordinal_indicator": False,
"is_custom": False,
},
{
"name": "Period",
Expand All @@ -80,6 +83,7 @@ class FieldType:
"obs_source_name": "Period",
"format_regex": format_prefixes[7],
"ordinal_indicator": True,
"is_custom": False,
},
{
"name": "Home Fouls",
Expand All @@ -91,6 +95,7 @@ class FieldType:
"obs_source_name": "#Home Fouls",
"format_regex": format_prefixes[11],
"ordinal_indicator": False,
"is_custom": False,
},
{
"name": "Away Fouls",
Expand All @@ -102,6 +107,7 @@ class FieldType:
"obs_source_name": "#Away Fouls",
"format_regex": format_prefixes[11],
"ordinal_indicator": False,
"is_custom": False,
},
{
"name": "Shot Clock",
Expand All @@ -113,6 +119,7 @@ class FieldType:
"obs_source_name": "shotclock",
"format_regex": format_prefixes[4],
"ordinal_indicator": False,
"is_custom": False,
},
]
default_custom_box_info = {
Expand All @@ -124,10 +131,11 @@ class FieldType:
"obs_source_name": "",
"format_regex": format_prefixes[11],
"ordinal_indicator": False,
"is_custom": True,
}


def info_for_box_name(name):
def default_info_for_box_name(name):
# Get the info for a box name
for box in default_boxes:
if box["name"] == name:
Expand All @@ -145,8 +153,12 @@ def normalize_settings_dict(settings, box_info):
"format_regex": format_prefixes[11],
"type": FieldType.NUMBER,
"ordinal_indicator": False,
"is_custom": True,
}
return {
"is_custom": (
settings["is_custom"] if "is_custom" in settings else box_info["is_custom"]
),
"obs_source_name": (
settings["obs_source_name"]
if "obs_source_name" in settings
Expand Down
32 changes: 25 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from source_view import ImageViewer
from defaults import (
default_boxes,
info_for_box_name,
default_info_for_box_name,
normalize_settings_dict,
format_prefixes,
)
Expand Down Expand Up @@ -543,7 +543,7 @@ def editSettings(self, settingsMutatorCallback):
def restoreDefaults(self):
# restore the default settings for the selected item
def restoreDefaultsSettings(item_obj):
info = info_for_box_name(item_obj.name)
info = default_info_for_box_name(item_obj.name)
item_obj.settings = normalize_settings_dict({}, info)
return item_obj

Expand Down Expand Up @@ -684,15 +684,25 @@ def updatevMixTable(self, detectionTargets):

def detectionTargetsChanged(self, detectionTargets):
for box in detectionTargets:
logger.debug(f"Change: Detection target: {box.name}")
# change the list icon to green checkmark
items = self.ui.tableWidget_boxes.findItems(
box.name, Qt.MatchFlag.MatchExactly
)
if len(items) == 0:
logger.warning(f"Item not found: {box.name}. Adding it to the list.")
# add the item to the list
self.ui.tableWidget_boxes.insertRow(
self.ui.tableWidget_boxes.rowCount()
)
item = QTableWidgetItem(box.name)
self.ui.tableWidget_boxes.setItem(
self.ui.tableWidget_boxes.rowCount(), 0, item
self.ui.tableWidget_boxes.rowCount() - 1, 0, item
)
disabledItem = QTableWidgetItem()
disabledItem.setFlags(Qt.ItemFlag.NoItemFlags)
self.ui.tableWidget_boxes.setItem(
self.ui.tableWidget_boxes.rowCount() - 1, 1, disabledItem
)
else:
item = items[0]
Expand Down Expand Up @@ -763,7 +773,7 @@ def populateSettings(self, name):
self.ui.comboBox_binarizationMethod.setCurrentIndex(0)
else:
item_obj.settings = normalize_settings_dict(
item_obj.settings, info_for_box_name(item_obj.name)
item_obj.settings, default_info_for_box_name(item_obj.name)
)
self.ui.label_selectedInfo.setText(f"{item_obj.name}")
self.ui.lineEdit_format.setText(item_obj.settings["format_regex"])
Expand Down Expand Up @@ -1254,12 +1264,20 @@ def addBox(self):
if item.text() not in [o["name"] for o in default_boxes]:
custom_boxes.append(item.text())

store_custom_box_name("Custom")
i = len(custom_boxes)
new_box_name = f"Custom {i + 1}"
custom_boxes_names = fetch_data("scoresight.json", "custom_boxes_names", [])
# find if the name already exists
while new_box_name in custom_boxes or new_box_name in custom_boxes_names:
i += 1
new_box_name = f"Custom {i + 1}"

store_custom_box_name(new_box_name)
item = QTableWidgetItem(
QIcon(
path.abspath(path.join(path.dirname(__file__), "icons/circle-x.svg"))
),
"Custom",
new_box_name,
)
item.setData(Qt.ItemDataRole.UserRole, "unchecked")
self.ui.tableWidget_boxes.insertRow(self.ui.tableWidget_boxes.rowCount())
Expand Down Expand Up @@ -1325,7 +1343,7 @@ def makeBox(self):
self.listItemClicked(item)

# get the size of the box from the name
info = info_for_box_name(item.text())
info = default_info_for_box_name(item.text())

self.detectionTargetsStorage.add_item(
TextDetectionTarget(
Expand Down
14 changes: 10 additions & 4 deletions storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
from PySide6.QtCore import QObject, Signal
from platformdirs import user_data_dir
from defaults import info_for_box_name, normalize_settings_dict
from defaults import default_info_for_box_name, normalize_settings_dict

from text_detection_target import TextDetectionTarget
from sc_logging import logger
Expand Down Expand Up @@ -203,9 +203,11 @@ def loadBoxesFromDict(self, boxes) -> bool:
self._data.clear()
try:
for box in boxes:
box_info = info_for_box_name(box["name"])
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(
Expand All @@ -214,9 +216,11 @@ def loadBoxesFromDict(self, boxes) -> bool:
box["rect"]["width"],
box["rect"]["height"],
box["name"],
normalize_settings_dict(box["settings"], box_info),
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:
Expand All @@ -230,7 +234,8 @@ def getBoxesForStorage(self):
boxes = []
for detectionTarget in self._data:
detectionTarget.settings = normalize_settings_dict(
detectionTarget.settings, info_for_box_name(detectionTarget.name)
detectionTarget.settings,
default_info_for_box_name(detectionTarget.name),
)
boxes.append(
{
Expand All @@ -242,6 +247,7 @@ def getBoxesForStorage(self):
"height": detectionTarget.height(),
},
"settings": {
"is_custom": detectionTarget.settings.get("is_custom"),
"obs_source_name": detectionTarget.settings.get(
"obs_source_name"
),
Expand Down

0 comments on commit 65a2165

Please sign in to comment.