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
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),

View File

@ -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),

View File

@ -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),

View File

@ -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),

View File

@ -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),

View File

@ -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),

View File

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

View File

@ -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