Skip to content
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

More template matching refactoring #358

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/lang/enUS/corrections.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"tier7": "tier 7",
"tier8": "tier 8",
"tier9": "tier 9",
"tmlelligence": "intelligence",
"tomado": "tornado",
"ttem": "item",
"two- handed": "two-handed",
Expand Down
6 changes: 6 additions & 0 deletions assets/lang/enUS/uniques.json
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,12 @@
0
]
},
"the_umbacrux": {
"desc": "your subterfuge skills create an attackable shade totem for seconds",
"num_idx": [
0
]
},
"tibaults_will": {
"desc": "you deal increased damage while unstoppable and for seconds after. when you become unstoppable, gain of your primary resource.",
"full": "You deal {c_random}[Affix_Value_1|%x|]{/c} increased damage while {c_important}{u}Unstoppable{/u}{/c} and for {c_number}[Affix.\"Static Value 1\"]{/c} seconds after. When you become {c_important}{u}Unstoppable{/u}{/c}, gain {c_number}[Affix.\"Static Value 0\"]{/c} of your Primary Resource.",
Expand Down
Binary file removed assets/templates/item_descr/aspect_bullet_point.png
Binary file not shown.
Binary file not shown.
Binary file removed assets/templates/item_descr/empty_socket.png
Binary file not shown.
Binary file removed assets/templates/item_descr/empty_socket_medium.png
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

TP = concurrent.futures.ThreadPoolExecutor()

__version__ = "5.7.3"
__version__ = "5.7.4"
27 changes: 17 additions & 10 deletions src/config/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,19 @@ def _transform_list_of_tuples(self, value: list[tuple[int, int]]) -> list[tuple[
return [self._transform_tuples(value=v) for v in value]

def _transform_templates(self, templates: dict[str, Template]) -> dict[str, Template]:
return {
key: Template(
name=value.name,
img_bgra=self._resize_image(src=value.img_bgra),
img_bgr=self._resize_image(src=value.img_bgr),
img_gray=self._resize_image(src=value.img_gray),
alpha_mask=self._resize_image(src=value.alpha_mask) if value.alpha_mask is not None else None,
)
for key, value in templates.items()
}
result = {}
for key, value in templates.items():
if key.endswith("_special"): # do not transform templates that end with _special
result[key] = value
else:
result[key] = Template(
name=value.name,
img_bgra=self._resize_image(src=value.img_bgra),
img_bgr=self._resize_image(src=value.img_bgr),
img_gray=self._resize_image(src=value.img_gray),
alpha_mask=self._resize_image(src=value.alpha_mask) if value.alpha_mask is not None else None,
)
return result

def _transform_tuples(self, value: tuple[int, int]) -> tuple[int, int]:
values = self._transform_array(value=np.array(value, dtype=int))
Expand Down Expand Up @@ -109,6 +112,10 @@ def offsets(self) -> UiOffsetsModel:
def pos(self) -> UiPosModel:
return self._pos

@property
def resolution(self) -> str:
return self._current_resolution

@property
def roi(self) -> UiRoiModel:
return self._roi
Expand Down
1 change: 0 additions & 1 deletion src/item/descr/find_affixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def find_affixes(
affix_height = bottom_limit - affix_top_left[1] - int(line_height * 0.75)
full_affix_region = [*affix_top_left, affix_width, affix_height]
crop_full_affix = crop(img_item_descr, full_affix_region)
# cv2.imwrite("crop_full_affix.png", crop_full_affix)
do_pre_proc = not (is_sigil or not do_pre_proc_flag)
res, line_pos = image_to_text(crop_full_affix, line_boxes=True, do_pre_proc=do_pre_proc)
affix_lines = res.text.lower().split("\n")
Expand Down
15 changes: 5 additions & 10 deletions src/item/descr/read_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from src.item.descr.find_affixes import find_affixes
from src.item.descr.find_aspect import find_aspect
from src.item.descr.item_type import read_item_type
from src.item.descr.texture import find_affix_bullets, find_aspect_bullet, find_codex_upgrade_icon, find_empty_sockets, find_seperator_short
from src.item.descr.texture import find_affix_bullets, find_aspect_bullet, find_codex_upgrade_icon, find_seperator_short
from src.item.models import Item
from src.utils.window import screenshot

Expand Down Expand Up @@ -46,15 +46,12 @@ def read_descr(rarity: ItemRarity, img_item_descr: np.ndarray, show_warnings: bo
):
return item

# Find textures for bullets and sockets
# Find textures for bullets
# =========================
affix_bullets = find_affix_bullets(img_item_descr, sep_short_match)
futures["aspect_bullet"] = (
TP.submit(find_aspect_bullet, img_item_descr, sep_short_match)
if rarity in [ItemRarity.Legendary, ItemRarity.Unique, ItemRarity.Mythic]
else None
TP.submit(find_aspect_bullet, img_item_descr, sep_short_match) if rarity in [ItemRarity.Unique, ItemRarity.Mythic] else None
)
empty_sockets = find_empty_sockets(img_item_descr, sep_short_match)

# Split affix bullets into inherent and others
# =========================
Expand Down Expand Up @@ -112,10 +109,8 @@ def _get_inherent():
# Find normal affixes
# =========================
def _get_affixes():
if aspect_bullet is not None:
bottom_limit = aspect_bullet.center[1]
elif len(empty_sockets) > 0:
bottom_limit = empty_sockets[0].center[1]
if affix_bullets:
bottom_limit = affix_bullets[-1].region[1] + affix_bullets[-1].region[3] + ResManager().offsets.item_descr_line_height
else:
bottom_limit = img_item_descr.shape[0]
i_affixes, debug_str = find_affixes(
Expand Down
23 changes: 7 additions & 16 deletions src/item/descr/texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,21 @@ def find_affix_bullets(img_item_descr: np.ndarray, sep_short_match: TemplateMatc
)


def find_empty_sockets(img_item_descr: np.ndarray, sep_short_match: TemplateMatch) -> list[TemplateMatch]:
template_list = ["empty_socket"]
all_templates = [f"{x}_medium" for x in template_list] + template_list
empty_sockets = _find_bullets(
img_item_descr=img_item_descr,
sep_short_match=sep_short_match,
template_list=all_templates,
threshold=0.8,
mode="all",
)
return sorted(empty_sockets, key=lambda match: match.center[1])


def find_aspect_bullet(img_item_descr: np.ndarray, sep_short_match: TemplateMatch) -> TemplateMatch | None:
template_list = ["aspect_bullet_point", "unique_bullet_point", "mythic_bullet_point"]
template_list = ["unique_bullet_point", "mythic_bullet_point"]
all_templates = [f"{x}_medium" for x in template_list] + template_list
if ResManager().resolution == "1920x1080":
all_templates += ["mythic_bullet_point_1080p_special", "mythic_bullet_point_medium_1080p_special"]
aspect_bullets = _find_bullets(
img_item_descr=img_item_descr,
sep_short_match=sep_short_match,
template_list=all_templates,
threshold=0.8,
mode="first",
mode="all",
)
return aspect_bullets[0] if aspect_bullets else None
if aspect_bullets:
return [match for match in aspect_bullets if match.score == max([match.score for match in aspect_bullets])][0]
return None


def find_aspect_search_area(img_item_descr: np.ndarray, aspect_bullet: TemplateMatch) -> list[int]:
Expand Down
4 changes: 4 additions & 0 deletions src/tools/data/custom_uniques_enUS.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
"desc": "your weapon mastery skills are now also core skills that have no cooldowns but",
"num_idx": [0]
},
"the_umbacrux": {
"desc": "your subterfuge skills create an attackable shade totem for seconds",
"num_idx": [0]
},
"vox_omnium": {
"desc": "casting a core skill additionally fire instances of fire bolt frost bolt or spark",
"num_idx": [2]
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Diff not rendered.
2 changes: 1 addition & 1 deletion tests/config/ui_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ def test_colors():


def test_templates():
assert len(ResManager().templates) == 48
assert len(ResManager().templates) == 46
2 changes: 1 addition & 1 deletion tests/item/find_descr_season5_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@pytest.mark.parametrize(
("img_res", "input_img", "anchor", "expected_success", "expected_top_left", "expected_rarity"),
[
((3840, 2160), f"{BASE_PATH}/find_descr_mythic_2160p.png", (3017, 1560), True, (2230, 200), ItemRarity.Mythic),
((3840, 2160), f"{BASE_PATH}/2160p_small_find_descr_mythic_1.png", (3017, 1560), True, (2230, 200), ItemRarity.Mythic),
],
)
def test_find_descr(img_res, input_img, anchor, expected_success, expected_top_left, expected_rarity):
Expand Down
84 changes: 59 additions & 25 deletions tests/item/read_descr_season5_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
items = [
(
(3840, 2160),
f"{BASE_PATH}/read_descr_1_2160p_medium.png",
f"{BASE_PATH}/2160p_medium_read_descr_1.png",
item1 := Item(
affixes=[
Affix(name="intelligence", value=101, type=AffixType.rerolled),
Expand All @@ -29,10 +29,10 @@
rarity=ItemRarity.Legendary,
),
),
((3840, 2160), f"{BASE_PATH}/read_descr_1_2160p_small.png", item1),
((3840, 2160), f"{BASE_PATH}/2160p_small_read_descr_1.png", item1),
(
(3840, 2160),
f"{BASE_PATH}/read_descr_2_2160p_medium.png",
f"{BASE_PATH}/2160p_medium_read_descr_2.png",
item2 := Item(
affixes=[
Affix(name="intelligence", value=87),
Expand All @@ -46,10 +46,10 @@
rarity=ItemRarity.Legendary,
),
),
((3840, 2160), f"{BASE_PATH}/read_descr_2_2160p_small.png", item2),
((3840, 2160), f"{BASE_PATH}/2160p_small_read_descr_2.png", item2),
(
(3840, 2160),
f"{BASE_PATH}/read_descr_3_2160p_medium.png",
f"{BASE_PATH}/2160p_medium_read_descr_3.png",
item3 := Item(
affixes=[
Affix(name="intelligence", value=90),
Expand All @@ -63,10 +63,10 @@
rarity=ItemRarity.Legendary,
),
),
((3840, 2160), f"{BASE_PATH}/read_descr_3_2160p_small.png", item3),
((3840, 2160), f"{BASE_PATH}/2160p_small_read_descr_3.png", item3),
(
(3840, 2160),
f"{BASE_PATH}/read_descr_4_2160p_medium.png",
f"{BASE_PATH}/2160p_medium_read_descr_4.png",
item4 := Item(
affixes=[
Affix(name="chance_for_chain_lightning_projectiles_to_cast_twice", value=20.9),
Expand All @@ -80,10 +80,10 @@
rarity=ItemRarity.Unique,
),
),
((3840, 2160), f"{BASE_PATH}/read_descr_4_2160p_small.png", item4),
((3840, 2160), f"{BASE_PATH}/2160p_small_read_descr_4.png", item4),
(
(3840, 2160),
f"{BASE_PATH}/read_descr_5_2160p_medium.png",
f"{BASE_PATH}/2160p_medium_read_descr_5.png",
item5 := Item(
affixes=[
Affix(name="maximum_life", value=768),
Expand All @@ -98,10 +98,10 @@
rarity=ItemRarity.Legendary,
),
),
((3840, 2160), f"{BASE_PATH}/read_descr_5_2160p_small.png", item5),
((3840, 2160), f"{BASE_PATH}/2160p_small_read_descr_5.png", item5),
(
(3840, 2160),
f"{BASE_PATH}/read_descr_6_2160p_medium.png",
f"{BASE_PATH}/2160p_medium_read_descr_6.png",
item6 := Item(
affixes=[
Affix(name="intelligence", value=94),
Expand All @@ -116,10 +116,10 @@
rarity=ItemRarity.Legendary,
),
),
((3840, 2160), f"{BASE_PATH}/read_descr_6_2160p_small.png", item6),
((3840, 2160), f"{BASE_PATH}/2160p_small_read_descr_6.png", item6),
(
(3840, 2160),
f"{BASE_PATH}/read_descr_7_2160p_medium.png",
f"{BASE_PATH}/2160p_medium_read_descr_7.png",
item7 := Item(
affixes=[
Affix(name="intelligence", value=149, type=AffixType.greater),
Expand All @@ -134,10 +134,10 @@
rarity=ItemRarity.Legendary,
),
),
((3840, 2160), f"{BASE_PATH}/read_descr_7_2160p_small.png", item7),
((3840, 2160), f"{BASE_PATH}/2160p_small_read_descr_7.png", item7),
(
(3840, 2160),
f"{BASE_PATH}/read_descr_8_2160p_medium.png",
f"{BASE_PATH}/2160p_medium_read_descr_8.png",
item8 := Item(
affixes=[
Affix(name="intelligence", value=92),
Expand All @@ -155,10 +155,10 @@
rarity=ItemRarity.Legendary,
),
),
((3840, 2160), f"{BASE_PATH}/read_descr_8_2160p_small.png", item8),
((3840, 2160), f"{BASE_PATH}/2160p_small_read_descr_8.png", item8),
(
(3840, 2160),
f"{BASE_PATH}/read_descr_9_2160p_medium.png",
f"{BASE_PATH}/2160p_medium_read_descr_9.png",
item9 := Item(
affixes=[
Affix(name="intelligence", value=6.3),
Expand All @@ -173,10 +173,10 @@
rarity=ItemRarity.Legendary,
),
),
((3840, 2160), f"{BASE_PATH}/read_descr_9_2160p_small.png", item9),
((3840, 2160), f"{BASE_PATH}/2160p_small_read_descr_9.png", item9),
(
(3840, 2160),
f"{BASE_PATH}/read_descr_10_2160p_medium.png",
f"{BASE_PATH}/2160p_medium_read_descr_10.png",
item10 := Item(
affixes=[
Affix(name="lucky_hit_chance", value=14.4, type=AffixType.greater),
Expand All @@ -194,10 +194,10 @@
rarity=ItemRarity.Unique,
),
),
((3840, 2160), f"{BASE_PATH}/read_descr_10_2160p_medium.png", item10),
((3840, 2160), f"{BASE_PATH}/2160p_small_read_descr_10.png", item10),
(
(3840, 2160),
f"{BASE_PATH}/read_descr_11_2160p_medium.png",
f"{BASE_PATH}/2160p_medium_read_descr_11.png",
item11 := Item(
affixes=[
Affix(name="movement_speed", value=19),
Expand All @@ -212,10 +212,10 @@
rarity=ItemRarity.Mythic,
),
),
((3840, 2160), f"{BASE_PATH}/read_descr_11_2160p_small.png", item11),
((3840, 2160), f"{BASE_PATH}/2160p_small_read_descr_11.png", item11),
(
(2160, 1440),
f"{BASE_PATH}/read_descr_1_1440p_small.png",
f"{BASE_PATH}/1440p_small_read_descr_1.png",
Item(
affixes=[
Affix(name="all_stats", value=3),
Expand All @@ -232,7 +232,7 @@
),
(
(1920, 1080),
f"{BASE_PATH}/read_descr_1_1080p_small.png",
f"{BASE_PATH}/1080p_small_read_descr_1.png",
Item(
affixes=[
Affix(name="intelligence", value=87),
Expand All @@ -249,7 +249,41 @@
),
(
(1920, 1080),
f"{BASE_PATH}/read_descr_2_1080p_medium.png",
f"{BASE_PATH}/1080p_small_read_descr_2.png",
Item(
affixes=[
Affix(name="movement_speed", value=23.1),
Affix(name="maximum_resistance_to_all_elements", value=8.6),
Affix(name="resistance_to_all_elements", value=69),
Affix(name="damage_reduction", value=23),
],
aspect=Aspect(name="tyraels_might", value=5213),
inherent=[Affix(name="ignore_durability_loss", value=None, type=AffixType.inherent)],
item_type=ItemType.ChestArmor,
power=925,
rarity=ItemRarity.Mythic,
),
),
(
(1920, 1080),
f"{BASE_PATH}/1080p_medium_read_descr_2.png",
Item(
affixes=[
Affix(name="movement_speed", value=21.4),
Affix(name="maximum_resistance_to_all_elements", value=9.8),
Affix(name="resistance_to_all_elements", value=78),
Affix(name="damage_reduction", value=36),
],
aspect=Aspect(name="tyraels_might", value=5213),
inherent=[Affix(name="ignore_durability_loss", value=None, type=AffixType.inherent)],
item_type=ItemType.ChestArmor,
power=925,
rarity=ItemRarity.Mythic,
),
),
(
(1920, 1080),
f"{BASE_PATH}/1080p_medium_read_descr_1.png",
Item(
affixes=[
Affix(name="strength", value=77),
Expand Down
Loading