<h1 align="center">đšī¸ GameDriver đī¸đ¨</h1>
<h3 align="center">Lightweight cross-platform image matching tools focused on automation</h3>
<p align="center">
<a href="https://gamedriver.readthedocs.io/en/latest/?badge=latest">
<img alt="Documentation status" src="https://readthedocs.org/projects/gamedriver/badge/?version=latest">
</a>
<a href="https://codecov.io/github/aentwist/gamedriver">
<img alt="codecov" src="https://codecov.io/github/aentwist/gamedriver/graph/badge.svg?token=KJJ8Q1HRMV">
</a>
<a href="https://dl.circleci.com/status-badge/redirect/gh/aentwist/gamedriver/tree/main">
<img alt="CircleCI pipeline" src="https://dl.circleci.com/status-badge/img/gh/aentwist/gamedriver/tree/main.svg?style=shield">
</a>
<a href="https://pypi.org/project/gamedriver/">
<img alt="PyPI latest version" src="https://img.shields.io/pypi/v/gamedriver?color=006dad">
</a>
<a href="https://www.conventionalcommits.org">
<img alt="semantic-release: conventionalcommits" src="https://img.shields.io/badge/semantic--release-conventionalcommits-fa6673?logo=semantic-release">
</a>
</p>
## Install
This project uses OpenCV. If your project also uses it and [needs opencv-python](https://github.com/opencv/opencv-python?tab=readme-ov-file#installation-and-usage), take that. Otherwise, take opencv-python-headless. Taking exactly one of the two is required.
```sh
pip install gamedriver[opencv-python-headless]
```
```sh
pip install gamedriver[opencv-python]
```
## Setup
```py
import os
import time
import gamedriver as gd
from gamedriver.settings import set_settings as set_gd_settings
# For example (Android), we will use an adb client to provide the tap_xy and swipe
# functionality, and a scrcpy client to provide the image of the device screen.
import adbutils # https://github.com/openatx/adbutils
import scrcpy # https://github.com/leng-yue/py-scrcpy-client
# Recommend setting a SRC_DIR var in the project's top level __init__ file
# SRC_DIR = os.path.dirname(os.path.abspath(__file__))
from my_project import SRC_DIR
# Boilerplate for however you are providing tap/swipe, and get device
# screen functionality
# For example, the adb device is available on the standard port, 5555
serial = "127.0.0.1:5555"
# Initialization for this adb client
# Note there must be an adb server* (see the lib for specific behavior)
adb_client = adbutils.AdbClient()
adb_client.connect(serial)
adb_device = adb_client.device(serial)
# Initialization for this scrcpy client
scrcpy_client = scrcpy.Client(serial)
scrcpy_client.start(daemon_threaded=True)
# Wait for the server to spin up...
while scrcpy_client.last_frame is None:
time.sleep(1)
# Set settings
# I'd recommend setting these settings at minimum
set_gd_settings(
{
# For example, next to our top level __init__ file we have a folder
# "img" that contains all of our template images
"img_path": os.path.join(SRC_DIR, "img"),
"img_ext": ".png",
"get_screen": lambda: scrcpy_client.last_frame,
# Since we are streaming the screen using scrcpy, we can choose a "high"
# polling frequency. Screenshotting should use closer to 1+ seconds.
# Increasing this fast enough simply becomes 'as fast as the CPU goes'.
"refresh_rate_ms": 100,
"tap_xy": lambda x, y: adb_device.click(x, y),
"swipe": lambda x1, y1, x2, y2, duration_ms: adb_device.swipe(
x1, y1, x2, y2, duration_ms / 1_000
),
}
)
```
For the full settings reference, see the [API documentation](#api-documentation).
## Usage
### Cookbook
```py
# Recommend using this function by default
#
# Opens image <SRC_DIR>/img/buttons/confirm.png, searches the device screen for
# it, and taps it when it becomes visible.
gd.tap_img_when_visible("buttons/confirm")
# Wait until text/success is visible, then tap a different image
gd.wait_until_img_visible("text/success")
gd.tap_img("my-other-image")
# Keep tapping the image until it goes away or the timeout is reached
gd.tap_img_while_visible("buttons/back")
# Find all instances of an image, and do something with them
add_btns = list(gd.locate_all("buttons/add"))
if len(add_btns) > 3:
gd.tap_box(add_btns[3])
# Matching is in grayscale by default, however sometimes there is a need for
# color matching. For example, if the same button has different colors. While
# that is the core use case, color matching also improves accuracy - a
# reasonable alternative to fine-tuning the threshold strictness (see below).
if not gd.tap_img_when_visible(
# Wait until the button changes from disabled (gray) to enabled (blue).
# Use a lower timeout in case this might not occur; then if it doesn't
# we won't be waiting too long.
"buttons/submit-blue", timeout_s=5, convert_to_grayscale=False
):
raise Exception("Failed to submit")
# Perhaps you are missing matches or getting too many - try adjusting the
# threshold. For our default OpenCV match method, the threshold is a value in
# [0, 1], and lowering it makes matching more strict.
#
# The cancel button is not matching even though it is present. Loosen (raise)
# the threshold value since the match is of lower quality.
gd.tap_img_when_visible("buttons/cancel", threshold=0.1)
# Not recommended, but you can alternatively use a more implicit style.
#
# Maybe you need to tap whatever is at a location no matter what image it may be.
gd.tap_xy(500, 1000)
# Using an implicit wait avoids needing to have an image file to wait for.
# Hopefully you have a very good idea of about how long it should be.
gd.wait(0.1)
gd.tap_xy(750, 250)
gd.wait_until_img_visible("text/done")
gd.tap_xy(100, 100)
```
## API Documentation
https://gamedriver.readthedocs.io
## FAQ
### Why only games, and why not Unity?
This can be used to automate other things, but using computer vision for automation (this) should be an absolute last resort. For other use cases see [Appium](https://github.com/appium/appium), and in the Unity case the [Appium AltUnity plugin](https://github.com/headspinio/appium-altunity-plugin).
### Other PLs/platforms/_I need more_?
While I'm open to PRs, for the long term, effort is probably better directed at [integrating this project into the Appium images plugin](https://discuss.appium.io/t/images-plugin-support-and-design-limitations/43831). While it comes with challenges, especially of streaming the screens of various devices, integration with the WebDriver API would instantly provide support for many PLs and devices.
Raw data
{
"_id": null,
"home_page": null,
"name": "gamedriver",
"maintainer": null,
"docs_url": null,
"requires_python": "~=3.9",
"maintainer_email": null,
"keywords": "automation, computer vision, game, image detection, image matching, opencv, simulation, template matching, testing",
"author": "Anderson Entwistle",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/7c/a7/c5498e7a099aa948e8503c25dee5a4caa407552285ea0c06e5483118f057/gamedriver-1.1.0.tar.gz",
"platform": null,
"description": "<h1 align=\"center\">\ud83d\udd79\ufe0f GameDriver \ud83c\udfce\ufe0f\ud83d\udca8</h1>\n<h3 align=\"center\">Lightweight cross-platform image matching tools focused on automation</h3>\n<p align=\"center\">\n <a href=\"https://gamedriver.readthedocs.io/en/latest/?badge=latest\">\n <img alt=\"Documentation status\" src=\"https://readthedocs.org/projects/gamedriver/badge/?version=latest\">\n </a>\n <a href=\"https://codecov.io/github/aentwist/gamedriver\">\n <img alt=\"codecov\" src=\"https://codecov.io/github/aentwist/gamedriver/graph/badge.svg?token=KJJ8Q1HRMV\">\n </a>\n <a href=\"https://dl.circleci.com/status-badge/redirect/gh/aentwist/gamedriver/tree/main\">\n <img alt=\"CircleCI pipeline\" src=\"https://dl.circleci.com/status-badge/img/gh/aentwist/gamedriver/tree/main.svg?style=shield\">\n </a>\n <a href=\"https://pypi.org/project/gamedriver/\">\n <img alt=\"PyPI latest version\" src=\"https://img.shields.io/pypi/v/gamedriver?color=006dad\">\n </a>\n <a href=\"https://www.conventionalcommits.org\">\n <img alt=\"semantic-release: conventionalcommits\" src=\"https://img.shields.io/badge/semantic--release-conventionalcommits-fa6673?logo=semantic-release\">\n </a>\n</p>\n\n## Install\n\nThis project uses OpenCV. If your project also uses it and [needs opencv-python](https://github.com/opencv/opencv-python?tab=readme-ov-file#installation-and-usage), take that. Otherwise, take opencv-python-headless. Taking exactly one of the two is required.\n\n```sh\npip install gamedriver[opencv-python-headless]\n```\n\n```sh\npip install gamedriver[opencv-python]\n```\n\n## Setup\n\n```py\nimport os\nimport time\n\nimport gamedriver as gd\nfrom gamedriver.settings import set_settings as set_gd_settings\n\n# For example (Android), we will use an adb client to provide the tap_xy and swipe\n# functionality, and a scrcpy client to provide the image of the device screen.\nimport adbutils # https://github.com/openatx/adbutils\nimport scrcpy # https://github.com/leng-yue/py-scrcpy-client\n\n# Recommend setting a SRC_DIR var in the project's top level __init__ file\n# SRC_DIR = os.path.dirname(os.path.abspath(__file__))\nfrom my_project import SRC_DIR\n\n\n# Boilerplate for however you are providing tap/swipe, and get device\n# screen functionality\n\n# For example, the adb device is available on the standard port, 5555\nserial = \"127.0.0.1:5555\"\n\n# Initialization for this adb client\n# Note there must be an adb server* (see the lib for specific behavior)\nadb_client = adbutils.AdbClient()\nadb_client.connect(serial)\nadb_device = adb_client.device(serial)\n\n# Initialization for this scrcpy client\nscrcpy_client = scrcpy.Client(serial)\nscrcpy_client.start(daemon_threaded=True)\n# Wait for the server to spin up...\nwhile scrcpy_client.last_frame is None:\n time.sleep(1)\n\n\n# Set settings\n\n# I'd recommend setting these settings at minimum\nset_gd_settings(\n {\n # For example, next to our top level __init__ file we have a folder\n # \"img\" that contains all of our template images\n \"img_path\": os.path.join(SRC_DIR, \"img\"),\n \"img_ext\": \".png\",\n \"get_screen\": lambda: scrcpy_client.last_frame,\n # Since we are streaming the screen using scrcpy, we can choose a \"high\"\n # polling frequency. Screenshotting should use closer to 1+ seconds.\n # Increasing this fast enough simply becomes 'as fast as the CPU goes'.\n \"refresh_rate_ms\": 100,\n \"tap_xy\": lambda x, y: adb_device.click(x, y),\n \"swipe\": lambda x1, y1, x2, y2, duration_ms: adb_device.swipe(\n x1, y1, x2, y2, duration_ms / 1_000\n ),\n }\n)\n```\n\nFor the full settings reference, see the [API documentation](#api-documentation).\n\n## Usage\n\n### Cookbook\n\n```py\n# Recommend using this function by default\n#\n# Opens image <SRC_DIR>/img/buttons/confirm.png, searches the device screen for\n# it, and taps it when it becomes visible.\ngd.tap_img_when_visible(\"buttons/confirm\")\n\n# Wait until text/success is visible, then tap a different image\ngd.wait_until_img_visible(\"text/success\")\ngd.tap_img(\"my-other-image\")\n\n# Keep tapping the image until it goes away or the timeout is reached\ngd.tap_img_while_visible(\"buttons/back\")\n\n# Find all instances of an image, and do something with them\nadd_btns = list(gd.locate_all(\"buttons/add\"))\nif len(add_btns) > 3:\n gd.tap_box(add_btns[3])\n\n# Matching is in grayscale by default, however sometimes there is a need for\n# color matching. For example, if the same button has different colors. While\n# that is the core use case, color matching also improves accuracy - a\n# reasonable alternative to fine-tuning the threshold strictness (see below).\nif not gd.tap_img_when_visible(\n # Wait until the button changes from disabled (gray) to enabled (blue).\n # Use a lower timeout in case this might not occur; then if it doesn't\n # we won't be waiting too long.\n \"buttons/submit-blue\", timeout_s=5, convert_to_grayscale=False\n):\n raise Exception(\"Failed to submit\")\n\n# Perhaps you are missing matches or getting too many - try adjusting the\n# threshold. For our default OpenCV match method, the threshold is a value in\n# [0, 1], and lowering it makes matching more strict.\n#\n# The cancel button is not matching even though it is present. Loosen (raise)\n# the threshold value since the match is of lower quality.\ngd.tap_img_when_visible(\"buttons/cancel\", threshold=0.1)\n\n# Not recommended, but you can alternatively use a more implicit style.\n#\n# Maybe you need to tap whatever is at a location no matter what image it may be.\ngd.tap_xy(500, 1000)\n# Using an implicit wait avoids needing to have an image file to wait for.\n# Hopefully you have a very good idea of about how long it should be.\ngd.wait(0.1)\ngd.tap_xy(750, 250)\ngd.wait_until_img_visible(\"text/done\")\ngd.tap_xy(100, 100)\n```\n\n## API Documentation\n\nhttps://gamedriver.readthedocs.io\n\n## FAQ\n\n### Why only games, and why not Unity?\n\nThis can be used to automate other things, but using computer vision for automation (this) should be an absolute last resort. For other use cases see [Appium](https://github.com/appium/appium), and in the Unity case the [Appium AltUnity plugin](https://github.com/headspinio/appium-altunity-plugin).\n\n### Other PLs/platforms/_I need more_?\n\nWhile I'm open to PRs, for the long term, effort is probably better directed at [integrating this project into the Appium images plugin](https://discuss.appium.io/t/images-plugin-support-and-design-limitations/43831). While it comes with challenges, especially of streaming the screens of various devices, integration with the WebDriver API would instantly provide support for many PLs and devices.\n",
"bugtrack_url": null,
"license": null,
"summary": "Lightweight cross-platform image matching tools focused on automation",
"version": "1.1.0",
"project_urls": {
"Homepage": "https://github.com/aentwist/gamedriver",
"Issues": "https://github.com/aentwist/gamedriver/issues"
},
"split_keywords": [
"automation",
" computer vision",
" game",
" image detection",
" image matching",
" opencv",
" simulation",
" template matching",
" testing"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d2f060502acf3c3296c13ef06195b0711571f5e36c82db11cb3c95cccaef85b1",
"md5": "e546fba35bdac5103160defb12bb8161",
"sha256": "f4c6d5153632f5db4241fab4fe5ef7c0418a819f4cff909ae93d989f5de6a26b"
},
"downloads": -1,
"filename": "gamedriver-1.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e546fba35bdac5103160defb12bb8161",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "~=3.9",
"size": 25816,
"upload_time": "2024-09-09T23:13:11",
"upload_time_iso_8601": "2024-09-09T23:13:11.355628Z",
"url": "https://files.pythonhosted.org/packages/d2/f0/60502acf3c3296c13ef06195b0711571f5e36c82db11cb3c95cccaef85b1/gamedriver-1.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7ca7c5498e7a099aa948e8503c25dee5a4caa407552285ea0c06e5483118f057",
"md5": "9106c2a9d5a9ccda603f37d6fc0becef",
"sha256": "f0eee82f674acf9539cf11ae42d8489dde752d30e95754cf199892c23e98e8a4"
},
"downloads": -1,
"filename": "gamedriver-1.1.0.tar.gz",
"has_sig": false,
"md5_digest": "9106c2a9d5a9ccda603f37d6fc0becef",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "~=3.9",
"size": 24118,
"upload_time": "2024-09-09T23:13:12",
"upload_time_iso_8601": "2024-09-09T23:13:12.662445Z",
"url": "https://files.pythonhosted.org/packages/7c/a7/c5498e7a099aa948e8503c25dee5a4caa407552285ea0c06e5483118f057/gamedriver-1.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-09 23:13:12",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "aentwist",
"github_project": "gamedriver",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"circle": true,
"requirements": [],
"lcname": "gamedriver"
}