added selection w/ retry logic (#77)

This commit is contained in:
kotae4 2023-03-30 15:57:02 -04:00 committed by GitHub
parent 4852365592
commit bdb560e33e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 262 additions and 90 deletions

58
main.py
View File

@ -1,6 +1,7 @@
from unittest import result from unittest import result
import torch import torch
import pyautogui import pyautogui
import pygetwindow
import gc import gc
import numpy as np import numpy as np
import cv2 import cv2
@ -13,9 +14,6 @@ import dxcam
def main(): 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) # Portion of screen to be captured (This forms a square/rectangle around the center of screen)
screenShotHeight = 320 screenShotHeight = 320
screenShotWidth = 320 screenShotWidth = 320
@ -44,21 +42,51 @@ def main():
# Selecting the correct game window # Selecting the correct game window
try: try:
videoGameWindows = pyautogui.getWindowsWithTitle(videoGameWindowTitle) videoGameWindows = pygetwindow.getAllWindows()
videoGameWindow = videoGameWindows[0] print("=== All Windows ===")
except: for index, window in enumerate(videoGameWindows):
print("The game window you are trying to select doesn't exist.") # only output the window if it has a meaningful title
print("Check variable videoGameWindowTitle (typically on line 17)") if window.title != "":
exit() print("[{}]: {}".format(index, window.title))
# have the user select the window they want
# Select that Window try:
try: userInput = int(input(
videoGameWindow.activate() "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: except Exception as e:
print("Failed to activate game window: {}".format(str(e))) print("Failed to select game window: {}".format(e))
print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow")
return 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 # Setting up the screen shots
sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2, sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2,
"left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2), "left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2),

View File

@ -1,6 +1,7 @@
import onnxruntime as ort import onnxruntime as ort
import numpy as np import numpy as np
import pyautogui import pyautogui
import pygetwindow
import gc import gc
import numpy as np import numpy as np
import cv2 import cv2
@ -15,9 +16,6 @@ import torch_directml
def main(): 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) # Portion of screen to be captured (This forms a square/rectangle around the center of screen)
screenShotHeight = 320 screenShotHeight = 320
screenShotWidth = 320 screenShotWidth = 320
@ -55,21 +53,51 @@ def main():
# Selecting the correct game window # Selecting the correct game window
try: try:
videoGameWindows = pyautogui.getWindowsWithTitle(videoGameWindowTitle) videoGameWindows = pygetwindow.getAllWindows()
videoGameWindow = videoGameWindows[0] print("=== All Windows ===")
except: for index, window in enumerate(videoGameWindows):
print("The game window you are trying to select doesn't exist.") # only output the window if it has a meaningful title
print("Check variable videoGameWindowTitle (typically on line 13") if window.title != "":
exit() print("[{}]: {}".format(index, window.title))
# have the user select the window they want
# Select that Window try:
try: userInput = int(input(
videoGameWindow.activate() "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: except Exception as e:
print("Failed to activate game window: {}".format(str(e))) print("Failed to select game window: {}".format(e))
print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow")
return 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 # Setting up the screen shots
sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2, sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2,
"left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2), "left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2),

View File

@ -1,6 +1,7 @@
import onnxruntime as ort import onnxruntime as ort
import numpy as np import numpy as np
import pyautogui import pyautogui
import pygetwindow
import gc import gc
import numpy as np import numpy as np
import cv2 import cv2
@ -14,9 +15,6 @@ import torch
def main(): 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) # Portion of screen to be captured (This forms a square/rectangle around the center of screen)
screenShotHeight = 320 screenShotHeight = 320
screenShotWidth = 320 screenShotWidth = 320
@ -45,21 +43,51 @@ def main():
# Selecting the correct game window # Selecting the correct game window
try: try:
videoGameWindows = pyautogui.getWindowsWithTitle(videoGameWindowTitle) videoGameWindows = pygetwindow.getAllWindows()
videoGameWindow = videoGameWindows[0] print("=== All Windows ===")
except: for index, window in enumerate(videoGameWindows):
print("The game window you are trying to select doesn't exist.") # only output the window if it has a meaningful title
print("Check variable videoGameWindowTitle (typically on line 13") if window.title != "":
exit() print("[{}]: {}".format(index, window.title))
# have the user select the window they want
# Select that Window try:
try: userInput = int(input(
videoGameWindow.activate() "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: except Exception as e:
print("Failed to activate game window: {}".format(str(e))) print("Failed to select game window: {}".format(e))
print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow")
return 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 # Setting up the screen shots
sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2, sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2,
"left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2), "left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2),

View File

@ -2,6 +2,7 @@ import onnxruntime as ort
import numpy as np import numpy as np
import cupy as cp import cupy as cp
import pyautogui import pyautogui
import pygetwindow
import gc import gc
import numpy as np import numpy as np
import cv2 import cv2
@ -15,9 +16,6 @@ import torch
def main(): 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) # Portion of screen to be captured (This forms a square/rectangle around the center of screen)
screenShotHeight = 320 screenShotHeight = 320
screenShotWidth = 320 screenShotWidth = 320
@ -46,21 +44,51 @@ def main():
# Selecting the correct game window # Selecting the correct game window
try: try:
videoGameWindows = pyautogui.getWindowsWithTitle(videoGameWindowTitle) videoGameWindows = pygetwindow.getAllWindows()
videoGameWindow = videoGameWindows[0] print("=== All Windows ===")
except: for index, window in enumerate(videoGameWindows):
print("The game window you are trying to select doesn't exist.") # only output the window if it has a meaningful title
print("Check variable videoGameWindowTitle (typically on line 13") if window.title != "":
exit() print("[{}]: {}".format(index, window.title))
# have the user select the window they want
# Select that Window try:
try: userInput = int(input(
videoGameWindow.activate() "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: except Exception as e:
print("Failed to activate game window: {}".format(str(e))) print("Failed to select game window: {}".format(e))
print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow")
return 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 # Setting up the screen shots
sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2, sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2,
"left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2), "left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2),

View File

@ -1,6 +1,7 @@
from unittest import result from unittest import result
import torch import torch
import pyautogui import pyautogui
import pygetwindow
import gc import gc
import numpy as np import numpy as np
import cv2 import cv2
@ -15,9 +16,6 @@ import cupy as cp
def main(): 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) # Portion of screen to be captured (This forms a square/rectangle around the center of screen)
screenShotHeight = 320 screenShotHeight = 320
screenShotWidth = 320 screenShotWidth = 320
@ -46,21 +44,51 @@ def main():
# Selecting the correct game window # Selecting the correct game window
try: try:
videoGameWindows = pyautogui.getWindowsWithTitle(videoGameWindowTitle) videoGameWindows = pygetwindow.getAllWindows()
videoGameWindow = videoGameWindows[0] print("=== All Windows ===")
except: for index, window in enumerate(videoGameWindows):
print("The game window you are trying to select doesn't exist.") # only output the window if it has a meaningful title
print("Check variable videoGameWindowTitle (typically on line 13") if window.title != "":
exit() print("[{}]: {}".format(index, window.title))
# have the user select the window they want
# Select that Window try:
try: userInput = int(input(
videoGameWindow.activate() "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: except Exception as e:
print("Failed to activate game window: {}".format(str(e))) print("Failed to select game window: {}".format(e))
print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow")
return 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 # Setting up the screen shots
sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2, sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2,
"left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2), "left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2),

View File

@ -1,6 +1,7 @@
from unittest import result from unittest import result
import torch import torch
import pyautogui import pyautogui
import pygetwindow
import gc import gc
import numpy as np import numpy as np
import cv2 import cv2
@ -13,9 +14,6 @@ import dxcam
def main(): 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) # Portion of screen to be captured (This forms a square/rectangle around the center of screen)
screenShotHeight = 320 screenShotHeight = 320
screenShotWidth = 320 screenShotWidth = 320
@ -44,21 +42,51 @@ def main():
# Selecting the correct game window # Selecting the correct game window
try: try:
videoGameWindows = pyautogui.getWindowsWithTitle(videoGameWindowTitle) videoGameWindows = pygetwindow.getAllWindows()
videoGameWindow = videoGameWindows[0] print("=== All Windows ===")
except: for index, window in enumerate(videoGameWindows):
print("The game window you are trying to select doesn't exist.") # only output the window if it has a meaningful title
print("Check variable videoGameWindowTitle (typically on line 13") if window.title != "":
exit() print("[{}]: {}".format(index, window.title))
# have the user select the window they want
# Select that Window try:
try: userInput = int(input(
videoGameWindow.activate() "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: except Exception as e:
print("Failed to activate game window: {}".format(str(e))) print("Failed to select game window: {}".format(e))
print("Read the relevant restrictions here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow")
return 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 # Setting up the screen shots
sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2, sctArea = {"mon": 1, "top": videoGameWindow.top + (videoGameWindow.height - screenShotHeight) // 2,
"left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2), "left": aaRightShift + ((videoGameWindow.left + videoGameWindow.right) // 2) - (screenShotWidth // 2),

View File

@ -1,4 +1,6 @@
PyAutoGUI PyAutoGUI
# should be auto-installed by pyautogui, but just in case...
PyGetWindow
opencv-python opencv-python
numpy==1.23 numpy==1.23
pandas pandas

View File

@ -4,6 +4,8 @@ onnxruntime_directml
opencv_python opencv_python
pandas pandas
PyAutoGUI PyAutoGUI
# should be auto-installed by pyautogui, but just in case...
PyGetWindow
pywin32 pywin32
torch_directml torch_directml
# below is due to the scripts in the utils folder # below is due to the scripts in the utils folder