def setup_gui(): if is_linux(): print( "Before continuing with the GUI module setup, please read and perform the installation steps from the wiki: https://github.com/SerpentAI/SerpentAI/wiki/Linux-Installation-Guide#gui" ) elif is_macos(): print( "Before continuing with the GUI module setup, please read and perform the installation steps from the wiki: https://github.com/SerpentAI/SerpentAI/wiki/macOS-Installation-Guide#gui" ) elif is_windows(): print( "Before continuing with the GUI module setup, please read and perform the installation steps from the wiki: https://github.com/SerpentAI/SerpentAI/wiki/Windows-Installation-Guide#gui" ) print("") input("Press Enter to continue...") if is_linux(): subprocess.call(shlex.split("pip install Kivy==1.10.0")) elif is_macos(): subprocess.call(shlex.split("pip install pygame Kivy==1.10.0")) elif is_windows(): subprocess.call( shlex.split( "pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew" )) subprocess.call(shlex.split("pip install Kivy==1.10.0")) print("") print("GUI module setup complete!")
def setup_dashboard(): if is_linux(): print( "Before continuing with the Dashboard module setup, please read and perform the installation steps from the wiki: https://github.com/SerpentAI/SerpentAI/wiki/Linux-Installation-Guide#dashboard" ) elif is_macos(): print( "Before continuing with the Dashboard module setup, please read and perform the installation steps from the wiki: https://github.com/SerpentAI/SerpentAI/wiki/macOS-Installation-Guide#dashboard" ) elif is_windows(): print( "Before continuing with the Dashboard module setup, please read and perform the installation steps from the wiki: https://github.com/SerpentAI/SerpentAI/wiki/Windows-Installation-Guide#dashboard" ) print("") input("Press Enter to continue...") # Copy the base dashboard directory to the install location shutil.copytree( os.path.join(os.path.dirname(os.path.dirname(__file__)), "dashboard"), os.path.join(os.getcwd(), "dashboard")) # Copy the WAMP components to the install location dashboard directory shutil.copy( os.path.join(os.path.dirname(__file__), "wamp_components", "analytics_component.py"), os.path.join(os.getcwd(), "dashboard", "analytics_component.py")) shutil.copy( os.path.join(os.path.dirname(__file__), "wamp_components", "dashboard_api_component.py"), os.path.join(os.getcwd(), "dashboard", "dashboard_api_component.py")) # Install Kivy if is_linux(): subprocess.call(shlex.split("pip install Kivy==1.10.0")) elif is_macos(): subprocess.call(shlex.split("pip install pygame Kivy==1.10.0")) elif is_windows(): subprocess.call( shlex.split( "pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew" )) subprocess.call(shlex.split("pip install Kivy==1.10.0")) # Install CEFPython subprocess.call(shlex.split("pip install cefpython3==57.1")) # Install Pony ORM subprocess.call(shlex.split("pip install pony==0.7.3"))
async def onJoin(self, details): self.redis_client = await self._initialize_redis_client() game_class_name = await self.redis_client.get("SERPENT:GAME") game_class = offshoot.discover("Game")[game_class_name.decode("utf-8")] game = game_class() game.launch(dry_run=True) backend_string = config["input_controller"]["backend"] backend = InputControllers.NATIVE_WIN32 if is_windows( ) else InputControllers.PYAUTOGUI if backend_string != "DEFAULT": try: backend = InputControllers[backend_string] except KeyError: pass self.input_controller = InputController(game=game, backend=backend) while True: payload = await self.redis_client.brpop( config["input_controller"]["redis_key"]) payload = pickle.loads(payload[1]) input_controller_func_string, *args, kwargs = payload getattr(self.input_controller, input_controller_func_string)(*args, **kwargs) await asyncio.sleep(0.01)
def perform_ocr(image, scale=10, order=5, horizontal_closing=10, vertical_closing=5): image = skimage.transform.resize( image, (image.shape[0] * scale, image.shape[1] * scale), mode="edge", order=order) image = image > skimage.filters.threshold_otsu(image) black_pixel_count = image[image == 0].size white_pixel_count = image[image == 1].size if black_pixel_count > white_pixel_count: image = skimage.util.invert(image) image = skimage.morphology.closing( image, skimage.morphology.rectangle(1, horizontal_closing)) image = skimage.morphology.closing( image, skimage.morphology.rectangle(vertical_closing, 1)) image = skimage.util.img_as_ubyte(image) if is_unix(): return tesserocr.image_to_text( Image.fromarray(image), psm=tesserocr.PSM.SINGLE_LINE, oem=tesserocr.OEM.TESSERACT_ONLY).strip() elif is_windows(): return pytesseract.image_to_string(Image.fromarray(image))
def _load_adapter(self): if is_linux(): from serpent.window_controllers.linux_window_controller import LinuxWindowController return LinuxWindowController elif is_windows(): from serpent.window_controllers.win32_window_controller import Win32WindowController return Win32WindowController
def setup_ml(): if is_linux(): print( "Before continuing with the ML module setup, please read and perform the installation steps from the wiki: https://github.com/SerpentAI/SerpentAI/wiki/Linux-Installation-Guide#ml" ) elif is_macos(): print( "Before continuing with the ML module setup, please read and perform the installation steps from the wiki: https://github.com/SerpentAI/SerpentAI/wiki/macOS-Installation-Guide#ml" ) elif is_windows(): print( "Before continuing with the ML module setup, please read and perform the installation steps from the wiki: https://github.com/SerpentAI/SerpentAI/wiki/Windows-Installation-Guide#ml" ) print("") input("Press Enter to continue...") # Decide on CPU or GPU Tensorflow tensorflow_backend = input( "\nWhich backend do you plan to use for Tensorflow (One of: 'CPU', 'GPU' - Note: GPU backend can only be used on NVIDIA GTX 600 series and up): \n" ) if tensorflow_backend not in ["CPU", "GPU"]: tensorflow_backend = "CPU" if tensorflow_backend == "GPU": subprocess.call(shlex.split("pip install tensorflow-gpu==1.5.1")) elif tensorflow_backend == "CPU": subprocess.call(shlex.split("pip install tensorflow==1.5.1")) subprocess.call(shlex.split("pip install Keras tensorforce==0.3.5.1")) print("") print("ML module setup complete!")
def setup_ocr(): if is_linux(): print( "Before continuing with the OCR module setup, please read and perform the installation steps from the wiki: https://github.com/SerpentAI/SerpentAI/wiki/Linux-Installation-Guide#ocr" ) elif is_windows(): print( "Before continuing with the OCR module setup, please read and perform the installation steps from the wiki: https://github.com/SerpentAI/SerpentAI/wiki/Windows-Installation-Guide#ocr" ) print("") input("Press Enter to continue...") if is_linux(): subprocess.call(shlex.split("pip install tesserocr")) elif is_windows(): subprocess.call(shlex.split("pip install pytesseract")) print("") print("OCR module setup complete!")
def launch(self, **kwargs): executable_path = kwargs.get("executable_path") if executable_path is None: raise GameLauncherException( "An 'executable_path' kwarg is required...") if is_linux(): subprocess.Popen(shlex.split(executable_path)) elif is_windows(): subprocess.Popen(shlex.split(executable_path))
def launch(self, **kwargs): url = kwargs.get("url") browser = kwargs.get("browser") or WebBrowser.DEFAULT if url is None: raise GameLauncherException("An 'url' kwarg is required...") if is_linux(): webbrowser.get(self.web_browsers.get(browser.name)).open_new(url) elif is_windows(): webbrowser.get(self.web_browsers.get(browser.name)).open_new(url)
def is_tesseract_available(): # On Windows, using a portable version is supported if tesseract.exe # can be found in 'tools/tesseract' if is_windows(): source_path = pathlib.Path(__file__).parent.parent.absolute() current_path = pathlib.Path(".").absolute() for path in (source_path, current_path): if path.joinpath("tools/tesseract/tesseract.exe").exists(): return True return shutil.which("tesseract") is not None
def launch(self, **kwargs): executable_path = kwargs.get("executable_path") current_work_directory = kwargs.get("current_work_directory") if executable_path is None: raise GameLauncherException( "An 'executable_path' kwarg is required...") if is_linux(): subprocess.Popen(shlex.split(executable_path), cwd=current_work_directory) elif is_macos(): subprocess.Popen(shlex.split(executable_path), cwd=current_work_directory) elif is_windows(): subprocess.Popen(shlex.split(executable_path), cwd=current_work_directory)
def launch(self, **kwargs): app_id = kwargs.get("app_id") app_args = kwargs.get("app_args") if app_id is None: raise GameLauncherException("An 'app_id' kwarg is required...") protocol_string = f"steam://run/{app_id}" if app_args is not None: args_list = [f"--{k}={v}" for k, v in app_args.items()] protocol_string += "/en/" + " ".join(args_list) if is_linux(): subprocess.call(shlex.split(f"xdg-open '{protocol_string}'")) elif is_windows(): webbrowser.open(f"{protocol_string}")
def get_tesseract_executable(): if not is_tesseract_available(): return None tesseract_executable = "tesseract" if is_windows(): source_path = pathlib.Path(__file__).parent.parent.absolute() current_path = pathlib.Path(".").absolute() for path in (source_path, current_path): path = path.joinpath("tools/tesseract/tesseract.exe") if path.exists(): tesseract_executable = str(path.as_posix()) break return tesseract_executable
def update(): clear_terminal() display_serpent_logo() print("") print("Updating Serpent.AI to the latest version...") subprocess.call(shlex.split("pip install --upgrade SerpentAI")) if is_linux(): shutil.copy( os.path.join(os.path.dirname(__file__), "requirements.linux.txt"), os.path.join(os.getcwd(), "requirements.txt")) elif is_macos(): shutil.copy( os.path.join(os.path.dirname(__file__), "requirements.darwin.txt"), os.path.join(os.getcwd(), "requirements.txt")) elif is_windows(): shutil.copy( os.path.join(os.path.dirname(__file__), "requirements.win32.txt"), os.path.join(os.getcwd(), "requirements.txt")) subprocess.call(shlex.split("pip install -r requirements.txt")) import yaml with open(os.path.join(os.path.dirname(__file__), "config", "config.yml"), "r") as f: serpent_config = yaml.safe_load(f) or {} with open(os.path.join(os.getcwd(), "config", "config.yml"), "r") as f: user_config = yaml.safe_load(f) or {} config_changed = False for key, value in serpent_config.items(): if key not in user_config: user_config[key] = value config_changed = True if config_changed: with open(os.path.join(os.getcwd(), "config", "config.yml"), "w") as f: f.write(yaml.dump(user_config))
async def onJoin(self, details): #self.redis_client = await self._initialize_redis_client() #game_class_name = await self.redis_client.get("SERPENT:GAME") #game_class = offshoot.discover("Game")[game_class_name.decode("utf-8")] from serpent.game import Game game = Game() #game.launch(dry_run=True) backend_string=config["input_controller"]["backend"] backend = InputControllers.NATIVE_WIN32 if is_windows() else InputControllers.PYAUTOGUI if backend_string != "DEFAULT": try: backend = InputControllers[backend_string] except KeyError: pass self.input_controller = InputController(game=game, backend=backend) #while True: #payload = await self.redis_client.brpop(config["input_controller"]["redis_key"]) #payload = pickle.loads(payload[1]) #input_controller_func_string, *args, kwargs = payload #getattr(self.input_controller, input_controller_func_string)(*args, **kwargs) #await asyncio.sleep(0.01) def input_controller_sub_func(payload): input_controller_func_string, *args, kwargs = payload getattr(self.input_controller, input_controller_func_string)(*args, **kwargs) self.subscribe(input_controller_sub_func, config["input_controller"]["redis_key"]) print("subscribed to topic")
def __init__(self, **kwargs): super().__init__(**kwargs) self.config = config.get(f"{self.__class__.__name__}Plugin", dict()) self.platform = kwargs.get("platform") default_input_controller_backend = InputControllers.NATIVE_WIN32 if is_windows( ) else InputControllers.PYAUTOGUI self.input_controller = kwargs.get( "input_controller") or default_input_controller_backend self.window_id = None self.window_name = kwargs.get("window_name") self.window_geometry = None self.window_controller = WindowController() self.is_launched = False self.frame_grabber_process = None self.frame_transformation_pipeline_string = None self.game_frame_limiter = GameFrameLimiter( fps=self.config.get("fps", 30)) self.api_class = None self.api_instance = None self.environments = dict() self.environment_data = dict() self.sprites = self._discover_sprites() self.redis_client = StrictRedis(**config["redis"]) self.kwargs = kwargs
import skimage.color import skimage.segmentation import skimage.filters import skimage.morphology import skimage.util import skimage.transform import skimage.measure import skimage.io from serpent.utilities import is_unix, is_windows from serpent.utilities import SerpentError try: if is_unix(): import tesserocr elif is_windows(): import pytesseract except ImportError: raise SerpentError( "Setup has not been been performed for the OCR module. Please run 'serpent setup ocr'" ) import editdistance from PIL import Image def locate_string(query_string, image, fuzziness=0, ocr_preset=None,
def setup_base(): # Copy Config Templates print("Creating Configuration Files...") first_run = True for root, directories, files in os.walk(os.getcwd()): for file in files: if file == "offshoot.yml": first_run = False break if not first_run: break if not first_run: confirm = input( "It appears that the setup process had already been performed. Are you sure you want to proceed? Some important files will be overwritten! (One of: 'YES', 'NO'):\n" ) if confirm not in ["YES", "NO"]: confirm = "NO" if confirm == "NO": sys.exit() shutil.rmtree(os.path.join(os.getcwd(), "config"), ignore_errors=True) shutil.copytree(os.path.join(os.path.dirname(__file__), "config"), os.path.join(os.getcwd(), "config")) # Copy Offshoot Files shutil.copy(os.path.join(os.path.dirname(__file__), "offshoot.yml"), os.path.join(os.getcwd(), "offshoot.yml")) shutil.copy( os.path.join(os.path.dirname(__file__), "offshoot.manifest.json"), os.path.join(os.getcwd(), "offshoot.manifest.json")) # Generate Platform-Specific requirements.txt if is_linux(): shutil.copy( os.path.join(os.path.dirname(__file__), "requirements.linux.txt"), os.path.join(os.getcwd(), "requirements.txt")) elif is_macos(): shutil.copy( os.path.join(os.path.dirname(__file__), "requirements.darwin.txt"), os.path.join(os.getcwd(), "requirements.txt")) elif is_windows(): shutil.copy( os.path.join(os.path.dirname(__file__), "requirements.win32.txt"), os.path.join(os.getcwd(), "requirements.txt")) # Install the dependencies print("Installing dependencies...") if is_linux(): subprocess.call(shlex.split("pip install python-xlib")) elif is_macos(): subprocess.call( shlex.split( "pip install python-xlib pyobjc-framework-Quartz py-applescript" )) elif is_windows(): # Anaconda Packages subprocess.call(shlex.split( "conda install numpy scipy scikit-image scikit-learn h5py -y"), shell=True) subprocess.call(shlex.split("pip install -r requirements.txt")) # Install Crossbar subprocess.call(shlex.split("pip install crossbar==18.6.1")) # Create Dataset Directories os.makedirs(os.path.join(os.getcwd(), "datasets/collect_frames"), exist_ok=True) os.makedirs(os.path.join(os.getcwd(), "datasets/collect_frames_for_context"), exist_ok=True) os.makedirs(os.path.join(os.getcwd(), "datasets/current"), exist_ok=True) # Copy the Crossbar config shutil.copy(os.path.join(os.path.dirname(__file__), "crossbar.json"), os.path.join(os.getcwd(), "crossbar.json"))
def _determine_fullscreen_resolution(self): if is_windows(): return [win32api.GetSystemMetrics(0), win32api.GetSystemMetrics(1)] else: monitors = mss.mss().monitors return monitors[0]["width"], monitors[0]["height"]