diff --git a/main.py b/main.py index be941a1..7e1bc36 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ from unittest import result import torch import pyautogui +import pygetwindow import gc import numpy as np import cv2 @@ -13,9 +14,6 @@ import dxcam def main(): - # Window title of the game, don't need the entire name - videoGameWindowTitle = "Counter" - # Portion of screen to be captured (This forms a square/rectangle around the center of screen) screenShotHeight = 320 screenShotWidth = 320 @@ -44,21 +42,51 @@ def main(): # Selecting the correct game window try: - videoGameWindows = pyautogui.getWindowsWithTitle(videoGameWindowTitle) - videoGameWindow = videoGameWindows[0] - except: - print("The game window you are trying to select doesn't exist.") - print("Check variable videoGameWindowTitle (typically on line 17)") - exit() - - # Select that Window - try: - videoGameWindow.activate() + videoGameWindows = pygetwindow.getAllWindows() + print("=== All Windows ===") + for index, window in enumerate(videoGameWindows): + # only output the window if it has a meaningful title + if window.title != "": + print("[{}]: {}".format(index, window.title)) + # have the user select the window they want + try: + userInput = int(input( + "Please enter the number corresponding to the window you'd like to select: ")) + except ValueError: + print("You didn't enter a valid number. Please try again.") + return + # "save" that window as the chosen window for the rest of the script + videoGameWindow = videoGameWindows[userInput] except Exception as e: - print("Failed to activate game window: {}".format(str(e))) - print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow") + print("Failed to select game window: {}".format(e)) return + # Activate that Window + activationRetries = 30 + activationSuccess = False + while (activationRetries > 0): + try: + videoGameWindow.activate() + activationSuccess = True + break + except pygetwindow.PyGetWindowException as we: + print("Failed to activate game window: {}".format(str(we))) + print("Trying again... (you should switch to the game now)") + except Exception as e: + print("Failed to activate game window: {}".format(str(e))) + print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow") + activationSuccess = False + activationRetries = 0 + break + # wait a little bit before the next try + time.sleep(3.0) + activationRetries = activationRetries - 1 + # if we failed to activate the window then we'll be unable to send input to it + # so just exit the script now + if activationSuccess == False: + return + print("Successfully activated the game window...") + # Setting up the screen shots sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2, "left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2), diff --git a/main_onnx_amd.py b/main_onnx_amd.py index 49b9fac..6f4e200 100644 --- a/main_onnx_amd.py +++ b/main_onnx_amd.py @@ -1,6 +1,7 @@ import onnxruntime as ort import numpy as np import pyautogui +import pygetwindow import gc import numpy as np import cv2 @@ -15,9 +16,6 @@ import torch_directml def main(): - # Window title to go after and the height of the screenshots - videoGameWindowTitle = "Counter" - # Portion of screen to be captured (This forms a square/rectangle around the center of screen) screenShotHeight = 320 screenShotWidth = 320 @@ -55,21 +53,51 @@ def main(): # Selecting the correct game window try: - videoGameWindows = pyautogui.getWindowsWithTitle(videoGameWindowTitle) - videoGameWindow = videoGameWindows[0] - except: - print("The game window you are trying to select doesn't exist.") - print("Check variable videoGameWindowTitle (typically on line 13") - exit() - - # Select that Window - try: - videoGameWindow.activate() + videoGameWindows = pygetwindow.getAllWindows() + print("=== All Windows ===") + for index, window in enumerate(videoGameWindows): + # only output the window if it has a meaningful title + if window.title != "": + print("[{}]: {}".format(index, window.title)) + # have the user select the window they want + try: + userInput = int(input( + "Please enter the number corresponding to the window you'd like to select: ")) + except ValueError: + print("You didn't enter a valid number. Please try again.") + return + # "save" that window as the chosen window for the rest of the script + videoGameWindow = videoGameWindows[userInput] except Exception as e: - print("Failed to activate game window: {}".format(str(e))) - print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow") + print("Failed to select game window: {}".format(e)) return + # Activate that Window + activationRetries = 30 + activationSuccess = False + while (activationRetries > 0): + try: + videoGameWindow.activate() + activationSuccess = True + break + except pygetwindow.PyGetWindowException as we: + print("Failed to activate game window: {}".format(str(we))) + print("Trying again... (you should switch to the game now)") + except Exception as e: + print("Failed to activate game window: {}".format(str(e))) + print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow") + activationSuccess = False + activationRetries = 0 + break + # wait a little bit before the next try + time.sleep(3.0) + activationRetries = activationRetries - 1 + # if we failed to activate the window then we'll be unable to send input to it + # so just exit the script now + if activationSuccess == False: + return + print("Successfully activated the game window...") + # Setting up the screen shots sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2, "left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2), diff --git a/main_onnx_cpu.py b/main_onnx_cpu.py index 1e7a760..a006638 100644 --- a/main_onnx_cpu.py +++ b/main_onnx_cpu.py @@ -1,6 +1,7 @@ import onnxruntime as ort import numpy as np import pyautogui +import pygetwindow import gc import numpy as np import cv2 @@ -14,9 +15,6 @@ import torch def main(): - # Window title to go after and the height of the screenshots - videoGameWindowTitle = "Counter" - # Portion of screen to be captured (This forms a square/rectangle around the center of screen) screenShotHeight = 320 screenShotWidth = 320 @@ -45,21 +43,51 @@ def main(): # Selecting the correct game window try: - videoGameWindows = pyautogui.getWindowsWithTitle(videoGameWindowTitle) - videoGameWindow = videoGameWindows[0] - except: - print("The game window you are trying to select doesn't exist.") - print("Check variable videoGameWindowTitle (typically on line 13") - exit() - - # Select that Window - try: - videoGameWindow.activate() + videoGameWindows = pygetwindow.getAllWindows() + print("=== All Windows ===") + for index, window in enumerate(videoGameWindows): + # only output the window if it has a meaningful title + if window.title != "": + print("[{}]: {}".format(index, window.title)) + # have the user select the window they want + try: + userInput = int(input( + "Please enter the number corresponding to the window you'd like to select: ")) + except ValueError: + print("You didn't enter a valid number. Please try again.") + return + # "save" that window as the chosen window for the rest of the script + videoGameWindow = videoGameWindows[userInput] except Exception as e: - print("Failed to activate game window: {}".format(str(e))) - print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow") + print("Failed to select game window: {}".format(e)) return + # Activate that Window + activationRetries = 30 + activationSuccess = False + while (activationRetries > 0): + try: + videoGameWindow.activate() + activationSuccess = True + break + except pygetwindow.PyGetWindowException as we: + print("Failed to activate game window: {}".format(str(we))) + print("Trying again... (you should switch to the game now)") + except Exception as e: + print("Failed to activate game window: {}".format(str(e))) + print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow") + activationSuccess = False + activationRetries = 0 + break + # wait a little bit before the next try + time.sleep(3.0) + activationRetries = activationRetries - 1 + # if we failed to activate the window then we'll be unable to send input to it + # so just exit the script now + if activationSuccess == False: + return + print("Successfully activated the game window...") + # Setting up the screen shots sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2, "left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2), diff --git a/main_onnx_nvidia.py b/main_onnx_nvidia.py index 7c42845..6381978 100644 --- a/main_onnx_nvidia.py +++ b/main_onnx_nvidia.py @@ -2,6 +2,7 @@ import onnxruntime as ort import numpy as np import cupy as cp import pyautogui +import pygetwindow import gc import numpy as np import cv2 @@ -15,9 +16,6 @@ import torch def main(): - # Window title to go after and the height of the screenshots - videoGameWindowTitle = "Counter" - # Portion of screen to be captured (This forms a square/rectangle around the center of screen) screenShotHeight = 320 screenShotWidth = 320 @@ -46,21 +44,51 @@ def main(): # Selecting the correct game window try: - videoGameWindows = pyautogui.getWindowsWithTitle(videoGameWindowTitle) - videoGameWindow = videoGameWindows[0] - except: - print("The game window you are trying to select doesn't exist.") - print("Check variable videoGameWindowTitle (typically on line 13") - exit() - - # Select that Window - try: - videoGameWindow.activate() + videoGameWindows = pygetwindow.getAllWindows() + print("=== All Windows ===") + for index, window in enumerate(videoGameWindows): + # only output the window if it has a meaningful title + if window.title != "": + print("[{}]: {}".format(index, window.title)) + # have the user select the window they want + try: + userInput = int(input( + "Please enter the number corresponding to the window you'd like to select: ")) + except ValueError: + print("You didn't enter a valid number. Please try again.") + return + # "save" that window as the chosen window for the rest of the script + videoGameWindow = videoGameWindows[userInput] except Exception as e: - print("Failed to activate game window: {}".format(str(e))) - print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow") + print("Failed to select game window: {}".format(e)) return + # Activate that Window + activationRetries = 30 + activationSuccess = False + while (activationRetries > 0): + try: + videoGameWindow.activate() + activationSuccess = True + break + except pygetwindow.PyGetWindowException as we: + print("Failed to activate game window: {}".format(str(we))) + print("Trying again... (you should switch to the game now)") + except Exception as e: + print("Failed to activate game window: {}".format(str(e))) + print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow") + activationSuccess = False + activationRetries = 0 + break + # wait a little bit before the next try + time.sleep(3.0) + activationRetries = activationRetries - 1 + # if we failed to activate the window then we'll be unable to send input to it + # so just exit the script now + if activationSuccess == False: + return + print("Successfully activated the game window...") + # Setting up the screen shots sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2, "left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2), diff --git a/main_tensorrt_gpu.py b/main_tensorrt_gpu.py index f2107c1..d79da18 100644 --- a/main_tensorrt_gpu.py +++ b/main_tensorrt_gpu.py @@ -1,6 +1,7 @@ from unittest import result import torch import pyautogui +import pygetwindow import gc import numpy as np import cv2 @@ -15,9 +16,6 @@ import cupy as cp def main(): - # Window title to go after and the height of the screenshots - videoGameWindowTitle = "Counter" - # Portion of screen to be captured (This forms a square/rectangle around the center of screen) screenShotHeight = 320 screenShotWidth = 320 @@ -46,21 +44,51 @@ def main(): # Selecting the correct game window try: - videoGameWindows = pyautogui.getWindowsWithTitle(videoGameWindowTitle) - videoGameWindow = videoGameWindows[0] - except: - print("The game window you are trying to select doesn't exist.") - print("Check variable videoGameWindowTitle (typically on line 13") - exit() - - # Select that Window - try: - videoGameWindow.activate() + videoGameWindows = pygetwindow.getAllWindows() + print("=== All Windows ===") + for index, window in enumerate(videoGameWindows): + # only output the window if it has a meaningful title + if window.title != "": + print("[{}]: {}".format(index, window.title)) + # have the user select the window they want + try: + userInput = int(input( + "Please enter the number corresponding to the window you'd like to select: ")) + except ValueError: + print("You didn't enter a valid number. Please try again.") + return + # "save" that window as the chosen window for the rest of the script + videoGameWindow = videoGameWindows[userInput] except Exception as e: - print("Failed to activate game window: {}".format(str(e))) - print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow") + print("Failed to select game window: {}".format(e)) return + # Activate that Window + activationRetries = 30 + activationSuccess = False + while (activationRetries > 0): + try: + videoGameWindow.activate() + activationSuccess = True + break + except pygetwindow.PyGetWindowException as we: + print("Failed to activate game window: {}".format(str(we))) + print("Trying again... (you should switch to the game now)") + except Exception as e: + print("Failed to activate game window: {}".format(str(e))) + print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow") + activationSuccess = False + activationRetries = 0 + break + # wait a little bit before the next try + time.sleep(3.0) + activationRetries = activationRetries - 1 + # if we failed to activate the window then we'll be unable to send input to it + # so just exit the script now + if activationSuccess == False: + return + print("Successfully activated the game window...") + # Setting up the screen shots sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2, "left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2), diff --git a/main_torch_gpu.py b/main_torch_gpu.py index e0f5907..fc3ffc3 100644 --- a/main_torch_gpu.py +++ b/main_torch_gpu.py @@ -1,6 +1,7 @@ from unittest import result import torch import pyautogui +import pygetwindow import gc import numpy as np import cv2 @@ -13,9 +14,6 @@ import dxcam def main(): - # Window title to go after and the height of the screenshots - videoGameWindowTitle = "Counter" - # Portion of screen to be captured (This forms a square/rectangle around the center of screen) screenShotHeight = 320 screenShotWidth = 320 @@ -44,21 +42,51 @@ def main(): # Selecting the correct game window try: - videoGameWindows = pyautogui.getWindowsWithTitle(videoGameWindowTitle) - videoGameWindow = videoGameWindows[0] - except: - print("The game window you are trying to select doesn't exist.") - print("Check variable videoGameWindowTitle (typically on line 13") - exit() - - # Select that Window - try: - videoGameWindow.activate() + videoGameWindows = pygetwindow.getAllWindows() + print("=== All Windows ===") + for index, window in enumerate(videoGameWindows): + # only output the window if it has a meaningful title + if window.title != "": + print("[{}]: {}".format(index, window.title)) + # have the user select the window they want + try: + userInput = int(input( + "Please enter the number corresponding to the window you'd like to select: ")) + except ValueError: + print("You didn't enter a valid number. Please try again.") + return + # "save" that window as the chosen window for the rest of the script + videoGameWindow = videoGameWindows[userInput] except Exception as e: - print("Failed to activate game window: {}".format(str(e))) - print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow") + print("Failed to select game window: {}".format(e)) return + # Activate that Window + activationRetries = 30 + activationSuccess = False + while (activationRetries > 0): + try: + videoGameWindow.activate() + activationSuccess = True + break + except pygetwindow.PyGetWindowException as we: + print("Failed to activate game window: {}".format(str(we))) + print("Trying again... (you should switch to the game now)") + except Exception as e: + print("Failed to activate game window: {}".format(str(e))) + print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow") + activationSuccess = False + activationRetries = 0 + break + # wait a little bit before the next try + time.sleep(3.0) + activationRetries = activationRetries - 1 + # if we failed to activate the window then we'll be unable to send input to it + # so just exit the script now + if activationSuccess == False: + return + print("Successfully activated the game window...") + # Setting up the screen shots sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2, "left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2), diff --git a/requirements.txt b/requirements.txt index f4abfca..a188096 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,6 @@ PyAutoGUI +# should be auto-installed by pyautogui, but just in case... +PyGetWindow opencv-python numpy==1.23 pandas diff --git a/requirements_onnx_amd.txt b/requirements_onnx_amd.txt index 88f014a..0249507 100644 --- a/requirements_onnx_amd.txt +++ b/requirements_onnx_amd.txt @@ -4,6 +4,8 @@ onnxruntime_directml opencv_python pandas PyAutoGUI +# should be auto-installed by pyautogui, but just in case... +PyGetWindow pywin32 torch_directml # below is due to the scripts in the utils folder