Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

feat(npc): Option to auto label vendors while botty is running #950

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions config/params.ini
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,4 @@ override_capabilities=
pathing_delay_factor=4
;If you want to control Hyper-V window from host use 0,51 here
window_client_area_offset=0,0
auto_label=1
1 change: 1 addition & 0 deletions src/auto_label/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .vendors import label_vendor
50 changes: 50 additions & 0 deletions src/auto_label/vendors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import cv2
import time
import os
import json
from utils.misc import wait
from requests import JSONDecodeError
from screen import grab, convert_monitor_to_screen
from logger import Logger
from utils.custom_mouse import mouse
from ui_manager import center_mouse


def label_vendor(npc_name: str):
Logger.debug(f"Label NPC: {npc_name}")
center_pos_monitor = mouse.get_position()
center_pos_screen = convert_monitor_to_screen(center_pos_monitor)
center_mouse()

# create folders
base_path = "log/labels/vendors"
ground_truth_path = f"{base_path}/ground_truth.json"
for dir_name in ["log", "log/labels", base_path, f"{base_path}/imgs"]:
os.makedirs(dir_name, exist_ok=True)
if not os.path.exists(ground_truth_path):
with open(ground_truth_path, "w+") as f:
pass # just to create file if it does not yet exist

# label it
img_name = "vendor_" + time.strftime("%Y%m%d_%H%M%S") + ".png"
cv2.imwrite(f"{base_path}/imgs/" + img_name, grab())
label = {
"objects": [{"name": npc_name, "center": center_pos_screen}],
"img": img_name
}
data = None
with open(ground_truth_path, "r") as f:
try:
data = json.load(f)
except ValueError as e:
print(e)
Logger.debug("Creating vendor ground_truth json file")
data = []
data.append(label)
if data is not None:
with open(ground_truth_path, "w") as f:
json.dump(data, f)

# move back mouse
mouse.move(*center_pos_monitor)
wait(0.2, 0.3)
1 change: 1 addition & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ def load_data(self):
"ocr_during_pickit": bool(int(self._select_val("advanced_options", "ocr_during_pickit"))),
"launch_options": self._select_val("advanced_options", "launch_options").replace("<name>", only_lowercase_letters(self.general["name"].lower())),
"override_capabilities": _default_iff(Config()._select_optional("advanced_options", "override_capabilities"), ""),
"auto_label": bool(int(self._select_val("advanced_options", "auto_label"))),
}

self.colors = {}
Expand Down
10 changes: 8 additions & 2 deletions src/npc_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from logger import Logger
from utils.custom_mouse import mouse
from math import sqrt
from auto_label import label_vendor

class Npc:
#A1
Expand Down Expand Up @@ -269,6 +270,10 @@ def open_npc_menu(npc_key: Npc) -> bool:
res_w = template_finder.search(npcs[npc_key]["name_tag_white"], filtered_inp_w, 0.9, roi=roi).valid
res_g = template_finder.search(npcs[npc_key]["name_tag_gold"], filtered_inp_g, 0.9, roi=roi).valid
if res_w:
# If we have auto label set, label this frame
if Config().advanced_options["auto_label"]:
label_vendor(npc_key)

mouse.click(button="left")
attempts += 1
wait(0.7, 1.0)
Expand Down Expand Up @@ -308,10 +313,11 @@ def press_npc_btn(npc_key: Npc, action_btn_key: str):

# Testing: Stand close to Qual-Kehk or Malah and run
if __name__ == "__main__":
from screen import grab
from screen import grab, find_and_set_window_position
from config import Config
import os
import keyboard
keyboard.add_hotkey('f12', lambda: os._exit(1))
keyboard.wait("f11")
open_npc_menu(Npc.MALAH)
find_and_set_window_position()
open_npc_menu(Npc.CAIN)