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 logging
import time
from threading import Thread
from fishy.engine.fullautofisher.engine import FullAuto
from pynput import keyboard, mouse
@ -18,70 +19,76 @@ kb = keyboard.Controller()
1, waiting for reaching down and second f8,
2 done
"""
_callibrate_state = -1
def callibrate(engine):
global _callibrate_state
class Calibrate():
def __init__(self, engine: FullAuto):
self._callibrate_state = -1
self.engine = engine
logging.debug("Callibrating...")
_callibrate_state = -1
def f8_pressed(self):
self._callibrate_state += 1
walking_time = 3
rotate_times = 50
def start(self):
Thread(target=self.callibrate).start()
# region rotate and move
coods = engine.get_coods()
if coods is None:
return
def callibrate(self):
logging.debug("Callibrating...")
_callibrate_state = -1
x1, y1, rot1 = coods
walking_time = 3
rotate_times = 50
kb.press('w')
time.sleep(walking_time)
kb.release('w')
time.sleep(0.5)
# region rotate and move
coods = self.engine.get_coods()
if coods is None:
return
coods = engine.get_coods()
if coods is None:
return
x2, y2, rot2 = coods
x1, y1, rot1 = coods
for _ in range(rotate_times):
mse.move(FullAuto.rotate_by, 0)
time.sleep(0.05)
kb.press('w')
time.sleep(walking_time)
kb.release('w')
time.sleep(0.5)
coods = engine.get_coods()
if coods is None:
return
x3, y3, rot3 = coods
coods = self.engine.get_coods()
if coods is None:
return
x2, y2, rot2 = coods
if rot3 > rot2:
rot3 -= 360
for _ in range(rotate_times):
mse.move(FullAuto.rotate_by, 0)
time.sleep(0.05)
move_factor = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) / walking_time
rot_factor = (rot3 - rot2) / rotate_times
# endregion
coods = self.engine.get_coods()
if coods is None:
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():
global _callibrate_state
_callibrate_state += 1
move_factor = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) / walking_time
rot_factor = (rot3 - rot2) / rotate_times
# 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()
while _callibrate_state == 1:
mse.move(0, FullAuto.rotate_by)
time.sleep(0.05)
wait_until(lambda: self._callibrate_state == 1)
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
engine.config.set("full_auto_factors", engine._factors)
logging.info(engine._factors)
y_cal_start_time = time.time()
while self._callibrate_state == 1:
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 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.IEngine import IEngine
from fishy.engine.fullautofisher.calibrate import callibrate
from fishy.engine.window import Window
from pynput import keyboard, mouse
@ -29,7 +26,7 @@ kb = keyboard.Controller()
def sign(x):
return -1 if x < 0 else 1
offset = 10
def get_crop_coods(window):
Window.loop()
img = window.get_capture()
@ -47,7 +44,7 @@ def get_crop_coods(window):
mask = np.zeros_like(img)
cv2.drawContours(mask, cnt, i, 255, -1)
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):
@ -85,11 +82,11 @@ class FullAuto(IEngine):
def __init__(self, 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._target = None
if self._factors is None:
if self.factors is None:
logging.warning("Please callibrate first")
self._hole_found_flag = False
@ -119,11 +116,10 @@ class FullAuto(IEngine):
if self.get_gui is not None:
self.gui.bot_started(True)
logging.info("Controlls:\nUP: Callibrate\nLEFT: Print Coordinates\nDOWN: Set target\nRIGHT: Move to target")
while self.start:
Window.loop()
# self.window.show("test", func=image_pre_process)
self.window.show("test", func=image_pre_process)
Window.loop_end()
self.gui.bot_started(False)
unassign_keys()
@ -136,7 +132,7 @@ class FullAuto(IEngine):
logging.error("set target first")
return
if self._factors is None:
if self.factors is None:
logging.error("you need to callibrate first")
return
@ -148,7 +144,7 @@ class FullAuto(IEngine):
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}")
kb.press('w')
time.sleep(walking_time)
@ -171,7 +167,7 @@ class FullAuto(IEngine):
if abs(angle_diff) > 180:
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}")
@ -189,7 +185,7 @@ class FullAuto(IEngine):
fishing_event.subscribers.append(found_hole)
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)
time.sleep(0.05)
t += 0.05
@ -203,8 +199,10 @@ class FullAuto(IEngine):
return self._hole_found_flag
def initalize_keys(self):
# hotkey.set_hotkey(Key.LEFT, lambda: logging.info(self.get_coods()))
hotkey.set_hotkey(Key.UP, lambda: callibrate(self))
hotkey.set_hotkey(Key.RIGHT, lambda: logging.info(self.get_coods()))
from fishy.engine.fullautofisher.calibrate import Calibrate
hotkey.set_hotkey(Key.UP, Calibrate(self).start)
# def down():
# t = self.get_coods()[:-1]
@ -213,9 +211,11 @@ class FullAuto(IEngine):
# hotkey.set_hotkey(Key.DOWN, down)
# hotkey.set_hotkey(Key.RIGHT, lambda: self.move_to(self.config.get("target", None)))
hotkey.set_hotkey(Key.LEFT, lambda: Recorder(self).start)
hotkey.set_hotkey(Key.DOWN, lambda: Player(self).start)
from fishy.engine.fullautofisher.recorder import Recorder
from fishy.engine.fullautofisher.player import Player
hotkey.set_hotkey(Key.LEFT, Recorder(self).start)
hotkey.set_hotkey(Key.DOWN, Player(self).start)
logging.info("STARTED")
if __name__ == '__main__':

View File

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

View File

@ -1,5 +1,6 @@
import json
import logging
import pickle
import time
from threading import Thread
from tkinter.filedialog import asksaveasfile
@ -38,7 +39,9 @@ class Recorder:
while self.recording:
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])))
time_took = time.time() - start_time
@ -52,6 +55,7 @@ class Recorder:
while not file:
file = asksaveasfile(mode='wb', filetypes=files, defaultextension=files)
data = {"full_auto_path": self.timeline}
json.dump(data, file)
print(data)
pickle.dump(data, file)
file.close()