Skip to content
Open
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
11 changes: 4 additions & 7 deletions botcity/core/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,17 +443,14 @@ def deduplicate(
elems: list[Generator[cv2find.Box, Any, None]]
) -> list[Generator[cv2find.Box, Any, None]]:
def find_same(item, items):
x_start = item.left
x_end = item.left + item.width
y_start = item.top
y_end = item.top + item.height
similars = []
for itm in items:
if itm == item:
continue
if (itm.left >= x_start and itm.left < x_end) and (
itm.top >= y_start and itm.top < y_end
):
# Boxes overlap no matter which side of `item` they are on.
if abs(itm.left - item.left) < item.width and abs(
itm.top - item.top
) < item.height:
similars.append(itm)
continue
return similars
Expand Down
50 changes: 50 additions & 0 deletions botcity/core/tests/test_find_all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import numpy
from PIL import Image

from botcity.core import DesktopBot, cv2find


def _bot_with_screen(tmp_path, monkeypatch, needle, screen):
needle_path = tmp_path / "needle.png"
Image.fromarray(needle).save(needle_path)

bot = DesktopBot()
bot.add_image("needle", str(needle_path))
monkeypatch.setattr(bot, "_fix_display_size", lambda: (200, 100))
monkeypatch.setattr(bot, "get_screenshot", lambda *a, **k: Image.fromarray(screen))
monkeypatch.setattr("botcity.core.bot.is_retina", lambda: False)
return bot


def test_find_all_drops_overlapping_matches_above_or_left_of_the_best(
tmp_path, monkeypatch
):
rng = numpy.random.default_rng(0)
screen = rng.integers(0, 256, (100, 200, 3), dtype=numpy.uint8)
# A smooth element also scores well one or more pixels away from where
# it really is, in every direction.
needle = numpy.zeros((20, 20, 3), numpy.uint8)
needle[:] = numpy.linspace(0, 255, 20, dtype=numpy.uint8)[None, :, None]
screen[40:60, 50:70] = needle

bot = _bot_with_screen(tmp_path, monkeypatch, needle, screen)

found = list(bot.find_all("needle", matching=0.8, waiting_time=1000))

assert found == [cv2find.Box(50, 40, 20, 20)]


def test_find_all_keeps_elements_that_touch_but_do_not_overlap(
tmp_path, monkeypatch
):
rng = numpy.random.default_rng(1)
screen = rng.integers(0, 256, (100, 200, 3), dtype=numpy.uint8)
needle = rng.integers(0, 256, (10, 10, 3), dtype=numpy.uint8)
screen[20:30, 40:50] = needle
screen[20:30, 50:60] = needle

bot = _bot_with_screen(tmp_path, monkeypatch, needle, screen)

found = sorted(bot.find_all("needle", matching=0.9, waiting_time=1000))

assert found == [cv2find.Box(40, 20, 10, 10), cv2find.Box(50, 20, 10, 10)]