Skip to content

Commit cdcccad

Browse files
feat: iou calculation for elements
1 parent 63bffab commit cdcccad

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

change_analyzer/spaces/discrete_app_action_space.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from change_analyzer.spaces.actions.app_action import AppAction
1212
from change_analyzer.spaces.actions.click_app_action import ClickAppAction
13+
from change_analyzer.utils.elements import iou
1314

1415

1516
class DiscreteAppActionSpace(Space):
@@ -62,9 +63,9 @@ def update_actionable_elements(self):
6263
if self._is_el_clickable(el)
6364
}
6465
for el0, el1 in itertools.combinations(self.actionable_elements, 2):
65-
if self._overlap_percentage(el0, el1) > self.OVERLAPING_PERCENTAGE:
66+
if iou(el0.rect, el1.rect) > self.OVERLAPING_PERCENTAGE:
6667
self.actionable_elements.remove(el1)
67-
if self._overlap_percentage(el1, el0) > self.OVERLAPING_PERCENTAGE:
68+
if iou(el1.rect, el0.rect) > self.OVERLAPING_PERCENTAGE:
6869
self.actionable_elements.remove(el0)
6970
except Exception as e:
7071
self._logger.info(

change_analyzer/tests/test_utils.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from PIL import ImageGrab
22

3-
from change_analyzer.utils import image_pad_resize_to
3+
from change_analyzer.utils import image_pad_resize_to, iou
44

55

66
def test_image_pad_resize_to_wide():
@@ -22,3 +22,15 @@ def test_image_pad_resize_to_wide():
2222
# im = image_pad_resize_to(ImageGrab.grab(), size)
2323
# # im.show()
2424
# assert im.size == size
25+
26+
27+
def test_zero_iou():
28+
rect0 = {"x": 0, "y": 0, "width": 100, "height": 100}
29+
rect1 = {"x": 101, "y": 101, "width": 100, "height": 100}
30+
assert iou(rect0, rect1) == 0
31+
32+
33+
def test_full_iou():
34+
rect0 = {"x": 0, "y": 0, "width": 100, "height": 100}
35+
rect1 = {"x": 0, "y": 0, "width": 100, "height": 100}
36+
assert iou(rect0, rect1) == 1

change_analyzer/utils.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from typing import Tuple
1+
from typing import Tuple, Dict
22

33
from PIL import Image, ImageOps
4+
from shapely.geometry import Polygon
45

56

67
def image_pad_resize_to(im: Image, new_dims: Tuple[int, int]) -> Image:
@@ -17,3 +18,21 @@ def image_pad_resize_to(im: Image, new_dims: Tuple[int, int]) -> Image:
1718

1819
padding = (round(delta_w / 2), round(delta_h / 2))
1920
return ImageOps.expand(im, padding, "black").resize(new_dims, Image.ANTIALIAS)
21+
22+
23+
def iou(rect0: Dict[str, int], rect1: Dict[str, int]) -> float:
24+
# self._logger.info(f"checking rects: {el0.rect}, {el1.rect}")
25+
# https://stackoverflow.com/a/42874377 or more compact: https://stackoverflow.com/a/57247833
26+
poly0 = Polygon([
27+
[rect0["x"], rect0["y"]],
28+
[rect0["x"] + rect0["width"], rect0["y"]],
29+
[rect0["x"] + rect0["width"], rect0["y"] + rect0["height"]],
30+
[rect0["x"], rect0["y"] + rect0["height"]]
31+
])
32+
poly1 = Polygon([
33+
[rect1["x"], rect1["y"]],
34+
[rect1["x"] + rect1["width"], rect1["y"]],
35+
[rect1["x"] + rect1["width"], rect1["y"] + rect1["height"]],
36+
[rect1["x"], rect1["y"] + rect1["height"]]
37+
])
38+
return poly0.intersection(poly1).area / poly0.union(poly1).area

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def _read_long_description():
2020
"jinja2",
2121
"pandas",
2222
"numpy",
23-
"matplotlib"
23+
"matplotlib",
24+
"shapely",
2425
]
2526
DEV_REQUIRE = [
2627
"black",

0 commit comments

Comments
 (0)