From df4deda1f252c6c211011fb9006d875be1951412 Mon Sep 17 00:00:00 2001 From: Adam Saudagar Date: Tue, 7 Mar 2023 15:00:16 +0530 Subject: [PATCH] added debug option to save capture --- fishy/engine/common/window.py | 83 ++++++++++++---------------- fishy/engine/common/window_server.py | 14 ++++- fishy/gui/main_gui.py | 10 +++- fishy/helper/helper.py | 13 +++++ 4 files changed, 70 insertions(+), 50 deletions(-) diff --git a/fishy/engine/common/window.py b/fishy/engine/common/window.py index 393ee4e..44b84b3 100644 --- a/fishy/engine/common/window.py +++ b/fishy/engine/common/window.py @@ -1,4 +1,5 @@ import logging +import uuid from typing import List import cv2 @@ -7,6 +8,7 @@ import imutils from fishy.engine.common import window_server from fishy.engine.common.window_server import Status, WindowServer from fishy.helper import helper +from fishy.helper.config import config class WindowClient: @@ -22,11 +24,37 @@ class WindowClient: self.color = color self.crop = crop self.scale = scale - self.show_name = show_name + self.show_name = f"window client {len(WindowClient.clients)}" - if len(WindowClient.clients) == 0: - window_server.start() WindowClient.clients.append(self) + if len(WindowClient.clients) > 0 and WindowServer.status != Status.RUNNING: + window_server.start() + + @staticmethod + def running(): + return WindowServer.status == Status.RUNNING + + def processed_image(self, func=None): + """ + processes the image using the function provided + :param func: function to process image + :return: processed image + """ + if WindowServer.status == Status.CRASHED: + return None + + img = self._get_capture() + + if img is None: + return None + + if func: + img = func(img) + + if config.get("show_grab", 0): + self._show(img) + + return img def destroy(self): if self in WindowClient.clients: @@ -34,11 +62,7 @@ class WindowClient: if len(WindowClient.clients) == 0: window_server.stop() - @staticmethod - def running(): - return WindowServer.status == Status.RUNNING - - def get_capture(self): + def _get_capture(self): """ copies the recorded screen and then pre processes its :return: game window image @@ -71,49 +95,12 @@ class WindowClient: return temp_img - def processed_image(self, func=None): - """ - processes the image using the function provided - :param func: function to process image - :return: processed image - """ - if WindowServer.status == Status.CRASHED: - return None - - img = self.get_capture() - - if img is None: - return None - - if func is None: - return img - - return func(img) - - def show(self, to_show, resize=None, func=None): + # noinspection PyUnresolvedReferences + def _show(self, img): """ Displays the processed image for debugging purposes - :param to_show: false to destroy the window - :param resize: scale the image to make small images more visible - :param func: function to process the image """ if WindowServer.status == Status.CRASHED: return - if not self.show_name: - logging.warning("You need to assign a name first") - return - - if not to_show: - cv2.destroyWindow(self.show_name) - return - - img = self.processed_image(func) - - if img is None: - return - - if resize is not None: - img = imutils.resize(img, width=resize) - cv2.imshow(self.show_name, img) - cv2.waitKey(25) + helper.save_img(self.show_name, img) diff --git a/fishy/engine/common/window_server.py b/fishy/engine/common/window_server.py index f7ee27a..66f60a1 100644 --- a/fishy/engine/common/window_server.py +++ b/fishy/engine/common/window_server.py @@ -2,10 +2,13 @@ import logging from enum import Enum from threading import Thread +import cv2 import numpy as np from mss.base import MSSBase from fishy.engine.common import screenshot +from fishy.helper import helper +from fishy.helper.config import config from fishy.helper.helper import print_exc from fishy.osservices.os_services import os_services @@ -37,17 +40,26 @@ def init(): WindowServer.crop = os_services.get_game_window_rect() if WindowServer.crop is None or not WindowServer.sslib.setup(): - logging.error("Game window not found") + logging.error("Game window not found by window_server") WindowServer.status = Status.CRASHED return def get_cropped_screenshot(): ss = WindowServer.sslib.grab() + + if config.get("show_grab", 0): + helper.save_img("full screen", ss) + crop = WindowServer.crop cropped_ss = ss[crop[1]:crop[3], crop[0]:crop[2]] + if cropped_ss.size == 0: return None + + if config.get("show_grab", 0): + helper.save_img("Game window", cropped_ss) + return cropped_ss diff --git a/fishy/gui/main_gui.py b/fishy/gui/main_gui.py index 2494483..21c5555 100644 --- a/fishy/gui/main_gui.py +++ b/fishy/gui/main_gui.py @@ -83,6 +83,14 @@ def _create(gui: 'GUI'): debug_menu.add_command(label="Check QR Value", command=lambda: gui.engine.check_qr_val()) + def toggle_show_grab(): + new_val = 1 - config.get("show_grab", 0) + show_grab_var.set(new_val) + config.set("show_grab", new_val) + show_grab_var = tk.IntVar() + show_grab_var.set(config.get("show_grab", 0)) + debug_menu.add_checkbutton(label="Show Grab Window", variable=show_grab_var, command=lambda: toggle_show_grab(), onvalue=1) + def select_sslib(selected_i): config.set("sslib", selected_i) sslib_var.set(selected_i) @@ -93,7 +101,7 @@ def _create(gui: 'GUI'): for i, lib in enumerate(screenshot.LIBS): sslib.add_checkbutton(label=lib.__name__, variable=sslib_var, command=partial(select_sslib, i), onvalue=i) - debug_menu.add_cascade(label="ScreenshotLib", menu=sslib) + debug_menu.add_cascade(label="Screenshot Lib", menu=sslib) debug_var = tk.IntVar() debug_var.set(int(config.get('debug', False))) diff --git a/fishy/helper/helper.py b/fishy/helper/helper.py index 99fb43f..64d8c02 100644 --- a/fishy/helper/helper.py +++ b/fishy/helper/helper.py @@ -7,12 +7,14 @@ import threading import time import traceback import webbrowser +from datetime import datetime from hashlib import md5 from io import BytesIO from threading import Thread from uuid import uuid1 from zipfile import ZipFile +import cv2 import requests from playsound import playsound @@ -205,3 +207,14 @@ def kill_thread(thread): def print_exc(): logging.error(traceback.format_exc()) traceback.print_exc() + + +def save_img(show_name, img): + img_path = os.path.join(os_services.get_documents_path(), "fishy_debug", "imgs", show_name) + if not os.path.exists(img_path): + os.makedirs(img_path) + + t = time.strftime("%Y.%m.%d.%H.%M.%S") + cv2.imwrite( + os.path.join(img_path, f"{t}.png"), + img)