changed save system to pickle instead of json, changed calibrate to class, added offset in crop coods for better ocr

This commit is contained in:
Adam Saudagar
2020-10-15 08:49:08 +05:30
parent b88cb8567c
commit 2edad8110f
4 changed files with 99 additions and 73 deletions

View File

@ -1,6 +1,7 @@
import math import math
import logging import logging
import time import time
from threading import Thread
from fishy.engine.fullautofisher.engine import FullAuto from fishy.engine.fullautofisher.engine import FullAuto
from pynput import keyboard, mouse from pynput import keyboard, mouse
@ -18,70 +19,76 @@ kb = keyboard.Controller()
1, waiting for reaching down and second f8, 1, waiting for reaching down and second f8,
2 done 2 done
""" """
_callibrate_state = -1
def callibrate(engine): class Calibrate():
global _callibrate_state def __init__(self, engine: FullAuto):
self._callibrate_state = -1
self.engine = engine
logging.debug("Callibrating...") def f8_pressed(self):
_callibrate_state = -1 self._callibrate_state += 1
walking_time = 3 def start(self):
rotate_times = 50 Thread(target=self.callibrate).start()
# region rotate and move def callibrate(self):
coods = engine.get_coods() logging.debug("Callibrating...")
if coods is None: _callibrate_state = -1
return
x1, y1, rot1 = coods walking_time = 3
rotate_times = 50
kb.press('w') # region rotate and move
time.sleep(walking_time) coods = self.engine.get_coods()
kb.release('w') if coods is None:
time.sleep(0.5) return
coods = engine.get_coods() x1, y1, rot1 = coods
if coods is None:
return
x2, y2, rot2 = coods
for _ in range(rotate_times): kb.press('w')
mse.move(FullAuto.rotate_by, 0) time.sleep(walking_time)
time.sleep(0.05) kb.release('w')
time.sleep(0.5)
coods = engine.get_coods() coods = self.engine.get_coods()
if coods is None: if coods is None:
return return
x3, y3, rot3 = coods x2, y2, rot2 = coods
if rot3 > rot2: for _ in range(rotate_times):
rot3 -= 360 mse.move(FullAuto.rotate_by, 0)
time.sleep(0.05)
move_factor = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) / walking_time coods = self.engine.get_coods()
rot_factor = (rot3 - rot2) / rotate_times if coods is None:
# endregion return
x3, y3, rot3 = coods
logging.info("Now loop up and press f8, then as soon as the character looks down, press f8 again") if rot3 > rot2:
rot3 -= 360
def f8_pressed(): move_factor = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) / walking_time
global _callibrate_state rot_factor = (rot3 - rot2) / rotate_times
_callibrate_state += 1 # endregion
hotkey.set_hotkey(Key.F8, f8_pressed) logging.info("Now loop up and press f8")
wait_until(lambda: _callibrate_state == 1) hotkey.set_hotkey(Key.F8, self.f8_pressed)
y_cal_start_time = time.time() wait_until(lambda: self._callibrate_state == 1)
while _callibrate_state == 1:
mse.move(0, FullAuto.rotate_by)
time.sleep(0.05)
time_to_reach_bottom = time.time() - y_cal_start_time logging.info("looking down now, as soon as you look on the floor, press f8 again")
engine._factors = move_factor, rot_factor, time_to_reach_bottom y_cal_start_time = time.time()
engine.config.set("full_auto_factors", engine._factors) while self._callibrate_state == 1:
logging.info(engine._factors) mse.move(0, FullAuto.rotate_by)
time.sleep(0.05)
hotkey.free_key(Key.F8) time_to_reach_bottom = time.time() - y_cal_start_time
self.engine.factors = move_factor, rot_factor, time_to_reach_bottom
self.engine.config.set("full_auto_factors", self.engine.factors)
logging.info(self.engine.factors)
hotkey.free_key(Key.F8)

View File

@ -7,12 +7,9 @@ import numpy as np
import pywintypes import pywintypes
import pytesseract import pytesseract
from fishy.engine.fullautofisher.player import Player
from fishy.engine.fullautofisher.recorder import Recorder
from fishy.engine.semifisher import fishing_event from fishy.engine.semifisher import fishing_event
from fishy.engine.IEngine import IEngine from fishy.engine.IEngine import IEngine
from fishy.engine.fullautofisher.calibrate import callibrate
from fishy.engine.window import Window from fishy.engine.window import Window
from pynput import keyboard, mouse from pynput import keyboard, mouse
@ -29,7 +26,7 @@ kb = keyboard.Controller()
def sign(x): def sign(x):
return -1 if x < 0 else 1 return -1 if x < 0 else 1
offset = 10
def get_crop_coods(window): def get_crop_coods(window):
Window.loop() Window.loop()
img = window.get_capture() img = window.get_capture()
@ -47,7 +44,7 @@ def get_crop_coods(window):
mask = np.zeros_like(img) mask = np.zeros_like(img)
cv2.drawContours(mask, cnt, i, 255, -1) cv2.drawContours(mask, cnt, i, 255, -1)
x, y, w, h = cv2.boundingRect(cnt[i]) x, y, w, h = cv2.boundingRect(cnt[i])
return x, y, x + w, y + h return x, y + offset, x + w, y + h - offset
def image_pre_process(img): def image_pre_process(img):
@ -85,11 +82,11 @@ class FullAuto(IEngine):
def __init__(self, config, gui_ref): def __init__(self, config, gui_ref):
super().__init__(config, gui_ref) super().__init__(config, gui_ref)
self._factors = self.config.get("full_auto_factors", None) self.factors = self.config.get("full_auto_factors", None)
self._tesseract_dir = None self._tesseract_dir = None
self._target = None self._target = None
if self._factors is None: if self.factors is None:
logging.warning("Please callibrate first") logging.warning("Please callibrate first")
self._hole_found_flag = False self._hole_found_flag = False
@ -119,11 +116,10 @@ class FullAuto(IEngine):
if self.get_gui is not None: if self.get_gui is not None:
self.gui.bot_started(True) self.gui.bot_started(True)
logging.info("Controlls:\nUP: Callibrate\nLEFT: Print Coordinates\nDOWN: Set target\nRIGHT: Move to target")
while self.start: while self.start:
Window.loop() Window.loop()
# self.window.show("test", func=image_pre_process) self.window.show("test", func=image_pre_process)
Window.loop_end() Window.loop_end()
self.gui.bot_started(False) self.gui.bot_started(False)
unassign_keys() unassign_keys()
@ -136,7 +132,7 @@ class FullAuto(IEngine):
logging.error("set target first") logging.error("set target first")
return return
if self._factors is None: if self.factors is None:
logging.error("you need to callibrate first") logging.error("you need to callibrate first")
return return
@ -148,7 +144,7 @@ class FullAuto(IEngine):
self.rotate_to(target_angle, from_angle) self.rotate_to(target_angle, from_angle)
walking_time = math.sqrt(move_vec[0] ** 2 + move_vec[1] ** 2) / self._factors[0] walking_time = math.sqrt(move_vec[0] ** 2 + move_vec[1] ** 2) / self.factors[0]
print(f"walking for {walking_time}") print(f"walking for {walking_time}")
kb.press('w') kb.press('w')
time.sleep(walking_time) time.sleep(walking_time)
@ -171,7 +167,7 @@ class FullAuto(IEngine):
if abs(angle_diff) > 180: if abs(angle_diff) > 180:
angle_diff = (360 - abs(angle_diff)) * sign(angle_diff) * -1 angle_diff = (360 - abs(angle_diff)) * sign(angle_diff) * -1
rotate_times = int(angle_diff / self._factors[1]) * -1 rotate_times = int(angle_diff / self.factors[1]) * -1
print(f"rotate_times: {rotate_times}") print(f"rotate_times: {rotate_times}")
@ -189,7 +185,7 @@ class FullAuto(IEngine):
fishing_event.subscribers.append(found_hole) fishing_event.subscribers.append(found_hole)
t = 0 t = 0
while not self._hole_found_flag or t <= self._factors[2]/2: while not self._hole_found_flag or t <= self.factors[2]/2:
mse.move(0, FullAuto.rotate_by) mse.move(0, FullAuto.rotate_by)
time.sleep(0.05) time.sleep(0.05)
t += 0.05 t += 0.05
@ -203,8 +199,10 @@ class FullAuto(IEngine):
return self._hole_found_flag return self._hole_found_flag
def initalize_keys(self): def initalize_keys(self):
# hotkey.set_hotkey(Key.LEFT, lambda: logging.info(self.get_coods())) hotkey.set_hotkey(Key.RIGHT, lambda: logging.info(self.get_coods()))
hotkey.set_hotkey(Key.UP, lambda: callibrate(self))
from fishy.engine.fullautofisher.calibrate import Calibrate
hotkey.set_hotkey(Key.UP, Calibrate(self).start)
# def down(): # def down():
# t = self.get_coods()[:-1] # t = self.get_coods()[:-1]
@ -213,9 +211,11 @@ class FullAuto(IEngine):
# hotkey.set_hotkey(Key.DOWN, down) # hotkey.set_hotkey(Key.DOWN, down)
# hotkey.set_hotkey(Key.RIGHT, lambda: self.move_to(self.config.get("target", None))) # hotkey.set_hotkey(Key.RIGHT, lambda: self.move_to(self.config.get("target", None)))
from fishy.engine.fullautofisher.recorder import Recorder
hotkey.set_hotkey(Key.LEFT, lambda: Recorder(self).start) from fishy.engine.fullautofisher.player import Player
hotkey.set_hotkey(Key.DOWN, lambda: Player(self).start) hotkey.set_hotkey(Key.LEFT, Recorder(self).start)
hotkey.set_hotkey(Key.DOWN, Player(self).start)
logging.info("STARTED")
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1,5 +1,6 @@
import json import json
import logging import logging
import pickle
import time import time
from threading import Thread from threading import Thread
from tkinter.filedialog import asksaveasfile, askopenfile from tkinter.filedialog import asksaveasfile, askopenfile
@ -11,16 +12,20 @@ from fishy.helper.hotkey import Key
class Player: class Player:
def __init__(self, engine: FullAuto): def __init__(self, engine: 'FullAuto'):
self.recording = False self.recording = False
self.engine = engine self.engine = engine
self.timeline = [] self.timeline = []
self.hole_complete_flag = False self.hole_complete_flag = False
self.start_moving_flag = False
def _mark_hole(self): def _mark_hole(self):
coods = self.engine.get_coods() coods = self.engine.get_coods()
self.timeline.append(("check_fish", coods)) self.timeline.append(("check_fish", coods))
def _start_moving(self):
self.start_moving_flag = True
def _stop_recording(self): def _stop_recording(self):
self.recording = False self.recording = False
@ -32,24 +37,34 @@ class Player:
Thread(target=self.start_route).start() Thread(target=self.start_route).start()
def start_route(self): def start_route(self):
file = askopenfile(mode='r', filetypes=[('Python Files', '*.py')]) file = askopenfile(mode='rb', filetypes=[('Python Files', '*.fishy')])
if not file: if not file:
logging.error("file not selected") logging.error("file not selected")
return return
data = json.load(file) data = pickle.load(file)
file.close()
print(data)
if "full_auto_path" not in data: if "full_auto_path" not in data:
logging.error("incorrect file") logging.error("incorrect file")
return return
self.timeline = data["full_auto_path"] self.timeline = data["full_auto_path"]
# wait until f8 is pressed
logging.info("press f8 to start")
self.start_moving_flag = False
hotkey.set_hotkey(Key.F8, self._start_moving)
helper.wait_until(lambda: self.start_moving_flag)
logging.info("starting")
for action in self.timeline: for action in self.timeline:
if action == "move_to": if action[0] == "move_to":
self.engine.move_to(action[1]) self.engine.move_to(action[1])
elif action == "check_fish": logging.info("moved")
elif action[0] == "check_fish":
self.engine.move_to(action[1]) self.engine.move_to(action[1])
self.engine.rotate_to(action[1][2]) self.engine.rotate_to(action[1][2])
# scan for fish hole # scan for fish hole
logging.info("scanning")
if self.engine.look_for_hole(): if self.engine.look_for_hole():
self.hole_complete_flag = False self.hole_complete_flag = False
helper.wait_until(lambda: self.hole_complete_flag) helper.wait_until(lambda: self.hole_complete_flag)

View File

@ -1,5 +1,6 @@
import json import json
import logging import logging
import pickle
import time import time
from threading import Thread from threading import Thread
from tkinter.filedialog import asksaveasfile from tkinter.filedialog import asksaveasfile
@ -38,7 +39,9 @@ class Recorder:
while self.recording: while self.recording:
start_time = time.time() start_time = time.time()
coods = self.engine.get_coods() coods = None
while not coods:
coods = self.engine.get_coods()
self.timeline.append(("move_to", (coods[0], coods[1]))) self.timeline.append(("move_to", (coods[0], coods[1])))
time_took = time.time() - start_time time_took = time.time() - start_time
@ -52,6 +55,7 @@ class Recorder:
while not file: while not file:
file = asksaveasfile(mode='wb', filetypes=files, defaultextension=files) file = asksaveasfile(mode='wb', filetypes=files, defaultextension=files)
data = {"full_auto_path": self.timeline} data = {"full_auto_path": self.timeline}
json.dump(data, file) print(data)
pickle.dump(data, file)
file.close() file.close()