Skip to content

Commit

Permalink
Fix item swap posting
Browse files Browse the repository at this point in the history
  • Loading branch information
gutnar committed Oct 22, 2021
1 parent 4be2b40 commit d0b99f0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 45 deletions.
6 changes: 6 additions & 0 deletions analyze_inventory_csv.py
Expand Up @@ -21,6 +21,12 @@
slots[res][slot] = [int(x), int(y), int(
x) + int(w), int(y) + int(h)]

if slot == "primary_left":
slots[res]["secondary_left"] = slots[res][slot]

if slot == "primary_right":
slots[res]["secondary_right"] = slots[res][slot]

for res in slots:
print("RECTS_" + res + " = {")
for slot in slots[res]:
Expand Down
46 changes: 46 additions & 0 deletions diablorun_igt/calibration.py
@@ -0,0 +1,46 @@
import numpy as np

RECTS_1920_1080 = {
'head': [1527, 108, 1640, 221],
'primary_left': [1310, 140, 1423, 362],
'secondary_left': [1310, 140, 1423, 362],
'primary_right': [1746, 140, 1859, 362],
'secondary_right': [1746, 140, 1859, 362],
'body_armor': [1527, 249, 1640, 419],
'gloves': [1309, 391, 1422, 504],
'belt': [1527, 447, 1640, 503],
'boots': [1745, 391, 1858, 504],
'amulet': [1663, 206, 1719, 262],
'ring_left': [1447, 447, 1503, 503],
'ring_right': [1663, 447, 1719, 503],
'swap_on_primary_left': [1334, 131, 1337, 134],
'swap_on_primary_right': [1770, 131, 1773, 134],
'swap_on_secondary_left': [1396, 131, 1399, 134],
'swap_on_secondary_right': [1832, 131, 1835, 134],
}

RECTS_1366_768 = {
'head': [1086, 77, 1166, 157],
'primary_left': [932, 99, 1012, 257],
'secondary_left': [932, 99, 1012, 257],
'primary_right': [1242, 99, 1322, 257],
'secondary_right': [1242, 99, 1322, 257],
'body_armor': [1086, 177, 1166, 298],
'gloves': [932, 278, 1012, 358],
'belt': [1087, 318, 1167, 358],
'boots': [1242, 278, 1322, 358],
'amulet': [1184, 146, 1224, 186],
'ring_left': [1029, 318, 1069, 358],
'ring_right': [1184, 318, 1224, 358],
'swap_on_primary_left': [949, 92, 952, 95],
'swap_on_primary_right': [1259, 92, 1262, 95],
'swap_on_secondary_left': [993, 92, 996, 95],
'swap_on_secondary_right': [1303, 92, 1306, 95],
}


def get_calibrated_rects(bgr):
if bgr.shape[0] == 1080:
return RECTS_1920_1080
elif bgr.shape[0] == 768:
return RECTS_1366_768
50 changes: 7 additions & 43 deletions diablorun_igt/inventory_detection.py
@@ -1,5 +1,6 @@
import numpy as np
from numpy.lib.stride_tricks import sliding_window_view
from diablorun_igt.calibration import get_calibrated_rects

from diablorun_igt.utils import get_image_rect

Expand All @@ -10,40 +11,6 @@
ITEM_HOVER_COLOR = np.array((10, 30, 6))
EMPTY_SLOT_COLOR = np.array((20, 20, 20))

RECTS_1920_1080 = {
'head': [1527, 108, 1640, 221],
'primary_left': [1310, 140, 1423, 362],
'primary_right': [1746, 140, 1859, 362],
'body_armor': [1527, 249, 1640, 419],
'gloves': [1309, 391, 1422, 504],
'belt': [1527, 447, 1640, 503],
'boots': [1745, 391, 1858, 504],
'amulet': [1663, 206, 1719, 262],
'ring_left': [1447, 447, 1503, 503],
'ring_right': [1663, 447, 1719, 503],
'swap_on_primary_left': [1334, 131, 1337, 134],
'swap_on_primary_right': [1770, 131, 1773, 134],
'swap_on_secondary_left': [1396, 131, 1399, 134],
'swap_on_secondary_right': [1832, 131, 1835, 134],
}

RECTS_1366_768 = {
'head': [1086, 77, 1166, 157],
'primary_left': [932, 99, 1012, 257],
'primary_right': [1242, 99, 1322, 257],
'body_armor': [1086, 177, 1166, 298],
'gloves': [932, 278, 1012, 358],
'belt': [1087, 318, 1167, 358],
'boots': [1242, 278, 1322, 358],
'amulet': [1184, 146, 1224, 186],
'ring_left': [1029, 318, 1069, 358],
'ring_right': [1184, 318, 1224, 358],
'swap_on_primary_left': [949, 92, 952, 95],
'swap_on_primary_right': [1259, 92, 1262, 95],
'swap_on_secondary_left': [993, 92, 996, 95],
'swap_on_secondary_right': [1303, 92, 1306, 95],
}

ITEM_SLOT_EMPTY_CENTER = {
'head': ((26, 26, 25), 2),
'primary_left': ((13, 13, 13), 2),
Expand All @@ -61,25 +28,22 @@


def get_item_slot_rects(bgr):
rects = None

if bgr.shape[0] == 1080:
rects = RECTS_1920_1080.copy()
elif bgr.shape[0] == 768:
rects = RECTS_1366_768.copy()
rects = get_calibrated_rects(bgr)

if rects:
rects = rects.copy()

swap_primary_brightness = (get_image_rect(bgr, rects['swap_on_primary_left']) +
get_image_rect(bgr, rects['swap_on_primary_right'])).mean()
swap_secondary_brightness = (get_image_rect(bgr, rects['swap_on_secondary_left']) +
get_image_rect(bgr, rects['swap_on_secondary_right'])).mean()

if swap_secondary_brightness > swap_primary_brightness:
rects["secondary_left"] = rects["primary_left"]
rects["secondary_right"] = rects["primary_right"]

del rects["primary_left"]
del rects["primary_right"]
else:
del rects["secondary_left"]
del rects["secondary_right"]

del rects["swap_on_primary_left"]
del rects["swap_on_primary_right"]
Expand Down
8 changes: 6 additions & 2 deletions diablorun_igt/inventory_manager.py
Expand Up @@ -2,6 +2,7 @@
import urllib.request

from diablorun_igt import inventory_detection
from diablorun_igt.calibration import get_calibrated_rects
from diablorun_igt.utils import get_image_rect, get_jpg


Expand Down Expand Up @@ -51,13 +52,16 @@ def handle_frame(self, bgr, cursor, cursor_visible):
# Re-send item images that are out of sync with description
if len(self.send_images_when_readable) > 0:
rects = inventory_detection.get_item_slot_rects(bgr)
sent_slots = set()

for slot in self.send_images_when_readable:
if not slot in emptied_item_slots:
if not slot in emptied_item_slots and slot[1] in rects:
self.post_item(slot, get_image_rect(
bgr, rects[slot[1]]), None)
sent_slots.add(slot)

self.send_images_when_readable.clear()
for slot in sent_slots:
self.send_images_when_readable.remove(slot)

# Check if an item slot is currently hovered
item_slot_hover = inventory_detection.get_hovered_item_slot(
Expand Down

0 comments on commit d0b99f0

Please sign in to comment.