Skip to content

Image comparison method and default delay time #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# Caches
.cache/

# Byte-compiled / optimized / DLL files
__pycache__/

# Distribution / packaging
env/
build/
dist/
*.prof
# Generated
**/gen/*.py
!**/gen/*.pyi
Expand All @@ -19,3 +23,4 @@ dist/

# Dev settings
*.pkl
settings.toml
8 changes: 4 additions & 4 deletions src/AutoSplit.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ def __start_image_function(self):
self.start_image_split_below_threshold = False

# delay start image if needed
if self.start_image.delay > 0:
if self.start_image.get_delay_time(self) > 0:
self.start_image_status_value_label.setText("delaying start...")
delay_start_time = time()
start_delay = self.start_image.delay / 1000
start_delay = self.start_image.get_delay_time(self) / 1000
while time() - delay_start_time < start_delay:
delay_time_left = start_delay - (time() - delay_start_time)
self.current_split_image.setText(
Expand Down Expand Up @@ -391,7 +391,7 @@ def __take_screenshot(self):

# save and open image
cv2.imwrite(screenshot_path, capture)
os.startfile(screenshot_path)
os.startfile(screenshot_path) # nosec

def __check_fps(self):
self.fps_value_label.clear()
Expand Down Expand Up @@ -616,7 +616,7 @@ def __auto_splitter(self):
if not self.split_image.check_flag(DUMMY_FLAG):
# If it's a delayed split, check if the delay has passed
# Otherwise calculate the split time for the key press
split_delay = self.split_image.delay / 1000
split_delay = self.split_image.get_delay_time(self) / 1000
if split_delay > 0 and not self.waiting_for_split_delay:
split_time = round(time() + split_delay * 1000)
self.waiting_for_split_delay = True
Expand Down
40 changes: 29 additions & 11 deletions src/AutoSplitImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,40 @@ class AutoSplitImage():
filename: str
flags: int
loops: int
delay: float
image_type: ImageType
bytes: Optional[cv2.ndarray] = None
mask: Optional[cv2.ndarray] = None
# This value is internal, check for mask instead
_has_transparency: bool
# These values should be overriden by Defaults if None. Use getters instead
__delay_time: Optional[float] = None
__comparison_method: Optional[int] = None
__pause_time: Optional[float] = None
__similarity_threshold: Optional[float] = None

def get_delay_time(self, default: Union[AutoSplit, int]):
"""
Get image's delay time or fallback to the default value from spinbox
"""
default_value = default \
if isinstance(default, int) \
else default.settings_dict["default_delay_time"]
return default_value if self.__delay_time is None else self.__delay_time

def __get_comparison_method(self, default: Union[AutoSplit, int]):
"""
Get image's comparison or fallback to the default value from combobox
"""
default_value = default \
if isinstance(default, int) \
else default.settings_dict["default_comparison_method"]
return default_value if self.__comparison_method is None else self.__comparison_method

def get_pause_time(self, default: Union[AutoSplit, float]):
"""
Get image's pause time or fallback to the default value from spinbox
"""
default_value: float = default \
default_value = default \
if isinstance(default, float) \
else default.settings_dict["default_pause_time"]
return default_value if self.__pause_time is None else self.__pause_time
Expand All @@ -53,7 +72,7 @@ def get_similarity_threshold(self, default: Union[AutoSplit, float]):
"""
Get image's similarity threashold or fallback to the default value from spinbox
"""
default_value: float = default \
default_value = default \
if isinstance(default, float) \
else default.settings_dict["default_similarity_threshold"]
return default_value if self.__similarity_threshold is None else self.__similarity_threshold
Expand All @@ -63,7 +82,8 @@ def __init__(self, path: str):
self.filename = os.path.split(path)[-1].lower()
self.flags = flags_from_filename(self.filename)
self.loops = loop_from_filename(self.filename)
self.delay = delay_from_filename(self.filename)
self.__delay_time = delay_time_from_filename(self.filename)
self.__comparison_method = comparison_method_from_filename(self.filename)
self.__pause_time = pause_from_filename(self.filename)
self.__similarity_threshold = threshold_from_filename(self.filename)
self.__read_image_bytes(path)
Expand Down Expand Up @@ -101,18 +121,16 @@ def check_flag(self, flag: int):

def compare_with_capture(
self,
comparison: Union[AutoSplit, int],
default: Union[AutoSplit, int],
capture: Optional[cv2.ndarray]
):
"""
Compare image with capture using comparison method from combobox
Compare image with capture using image's comparison method. Falls back to combobox
"""
comparison_method: int = comparison \
if isinstance(comparison, int) \
else comparison.settings_dict["default_comparison_method"]

if self.bytes is None or capture is None:
return 0.0
comparison_method = self.__get_comparison_method(default)
if comparison_method == 0:
return compare_l2_norm(self.bytes, capture, self.mask)
if comparison_method == 1:
Expand All @@ -122,5 +140,5 @@ def compare_with_capture(
return 0.0


from split_parser import delay_from_filename, flags_from_filename, loop_from_filename, pause_from_filename, \
threshold_from_filename
from split_parser import comparison_method_from_filename, delay_time_from_filename, flags_from_filename, \
loop_from_filename, pause_from_filename, threshold_from_filename
71 changes: 46 additions & 25 deletions src/split_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, TypeVar
if TYPE_CHECKING:
from AutoSplit import AutoSplit

Expand All @@ -14,6 +14,22 @@
PAUSE_FLAG,
*_] = [1 << i for i in range(31)] # 32 bits of flags

T = TypeVar("T", str, int, float)


def __value_from_filename(
filename: str,
delimiters: str,
default_value: T
) -> T:
if len(delimiters) != 2:
raise ValueError("delimiters parameter must contain exactly 2 characters")
try:
value_type = type(default_value)
return value_type(filename.split(delimiters[0], 1)[1].split(delimiters[1])[0])
except (IndexError, ValueError):
return default_value


def threshold_from_filename(filename: str):
"""
Expand All @@ -26,13 +42,10 @@ def threshold_from_filename(filename: str):

# Check to make sure there is a valid floating point number between
# parentheses of the filename
try:
threshold = float(filename.split("(", 1)[1].split(")")[0])
except (IndexError, ValueError):
return None
value = __value_from_filename(filename, "()", -1.0)

# Check to make sure if it is a valid threshold
return threshold if 0.0 < threshold < 1.0 else None
return value if 0.0 < value < 1.0 else None


def pause_from_filename(filename: str):
Expand All @@ -46,16 +59,13 @@ def pause_from_filename(filename: str):

# Check to make sure there is a valid pause time between brackets
# of the filename
try:
pause = float(filename.split("[", 1)[1].split("]")[0])
except (IndexError, ValueError):
return None
value = __value_from_filename(filename, "[]", -1.0)

# Pause times should always be positive or zero
return pause if pause >= 0.0 else None
return value if value >= 0.0 else None


def delay_from_filename(filename: str):
def delay_time_from_filename(filename: str):
"""
Retrieve the delay time from the filename, if there is no delay time or the delay time
isn't a valid number, then 0 is returned
Expand All @@ -66,13 +76,10 @@ def delay_from_filename(filename: str):

# Check to make sure there is a valid delay time between brackets
# of the filename
try:
delay = float(filename.split("#", 1)[1].split("#")[0])
except (IndexError, ValueError):
return 0.0
value = __value_from_filename(filename, "##", 0)

# Delay times should always be positive or zero
return delay if delay >= 0.0 else 0.0
return value if value >= 0 else None


def loop_from_filename(filename: str):
Expand All @@ -86,13 +93,27 @@ def loop_from_filename(filename: str):

# Check to make sure there is a valid delay time between brackets
# of the filename
try:
loop = int(filename.split("@", 1)[1].split("@")[0])
except (IndexError, ValueError):
return 1
value = __value_from_filename(filename, "@@", 1)

# Loop should always be positive
return loop if loop >= 1 else 1
return value if value >= 1 else 1


def comparison_method_from_filename(filename: str):
"""
Retrieve the number of loops from filename, if there is no loop number or the loop number isn't valid,
then 1 is returned.

@param filename: String containing the file's name
@return: A valid loop number, if not then 1
"""

# Check to make sure there is a valid delay time between brackets
# of the filename
value = __value_from_filename(filename, "<>", -1)

# Comparison method should always be positive or zero
return value if value >= 0 else None


def flags_from_filename(filename: str):
Expand All @@ -110,9 +131,9 @@ def flags_from_filename(filename: str):

# Check to make sure there are flags between curly braces
# of the filename
try:
flags_str = filename.split("{", 1)[1].split("}")[0]
except (IndexError, ValueError):
flags_str = __value_from_filename(filename, "{}", "")

if not flags_str:
return 0

flags = 0x00
Expand Down