From db689f0d8163b3706ac4fdb68725c99323e774c8 Mon Sep 17 00:00:00 2001 From: ut001652 Date: Mon, 23 Sep 2024 15:20:53 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9find=5Fimage=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E8=BF=9C=E7=A8=8B=E8=B0=83=E7=94=A8=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=A4=9A=E5=8F=82=E6=95=B0=EF=BC=8C=E4=BF=AE=E6=94=B9=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E9=83=A8=E7=BD=B2=E6=97=B6=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E4=B8=AD=E5=BF=83=E7=99=BD=E5=90=8D=E5=8D=95?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=8D=E7=94=9F=E6=95=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/remotectl/remote.py | 8 +++++--- src/utils/env_vir.sh | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/remotectl/remote.py b/src/remotectl/remote.py index 7dac969a..e7f5368b 100644 --- a/src/remotectl/remote.py +++ b/src/remotectl/remote.py @@ -94,9 +94,11 @@ def rctl_plus(self) -> Src: restart_service=self.restart_service, ) - def find_image(self, image_path): - _image_path = image_path.replace(conf.HOME, "~", 1) - return self.rctl_plus.find_image(_image_path) + def find_image(self, image_path, rate=None, multiple=False, picture_abspath=None, screen_bbox=None, + log_level='info', network_retry=None, pause=None, timeout=None, max_match_number=None): + _image_path = tuple([_path.replace(conf.HOME, "~", 1) for _path in image_path]) + return self.rctl_plus.find_image_remote(_image_path, rate, multiple, picture_abspath, screen_bbox, log_level, + network_retry, pause, timeout, max_match_number) if __name__ == '__main__': diff --git a/src/utils/env_vir.sh b/src/utils/env_vir.sh index be21dcf5..cb8852cd 100644 --- a/src/utils/env_vir.sh +++ b/src/utils/env_vir.sh @@ -81,7 +81,7 @@ if [ $? != 0 ]; then exit 121 fi python_virtualenv_path=$(pipenv --venv) -whitelist_path=`echo "${python_virtualenv_path}" | sed "s/\/home\/$USER\//\//"` +whitelist_path=`echo "${python_virtualenv_path}" | sed "s/$HOME\//\//"` if [ -f "${whitelist}" ]; then result=`sudo cat ${whitelist} | grep ${whitelist_path}` if [ -z "$result" ]; then From 5e0f8708d436555be78bc7dad0ab8cb4a836925c Mon Sep 17 00:00:00 2001 From: ut001652 Date: Wed, 25 Sep 2024 10:30:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9remote.py=20->=20find=5Fi?= =?UTF-8?q?mage=E3=80=81ocr=E6=94=AF=E6=8C=81=E8=BF=9C=E7=A8=8B=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E4=B8=8E=E6=9C=AC=E5=9C=B0=E8=B0=83=E7=94=A8=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E4=BF=9D=E6=8C=81=E4=B8=80=E8=87=B4=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E7=8E=AF=E5=A2=83=E9=83=A8=E7=BD=B2=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AE=89=E5=85=A8=E4=B8=AD=E5=BF=83=E7=99=BD?= =?UTF-8?q?=E5=90=8D=E5=8D=95=E6=B7=BB=E5=8A=A0=E4=B8=8D=E7=94=9F=E6=95=88?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/remotectl/remote.py | 76 +++++++++++++++++++++++++++++++++++++++-- src/utils/env_vir.sh | 2 +- 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/remotectl/remote.py b/src/remotectl/remote.py index 7dac969a..9d76335b 100644 --- a/src/remotectl/remote.py +++ b/src/remotectl/remote.py @@ -2,6 +2,8 @@ # _*_ coding:utf-8 _*_ # SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. # SPDX-License-Identifier: GPL-2.0-only +from typing import Union, List + from funnylog import logger from setting import conf @@ -94,10 +96,78 @@ def rctl_plus(self) -> Src: restart_service=self.restart_service, ) - def find_image(self, image_path): - _image_path = image_path.replace(conf.HOME, "~", 1) - return self.rctl_plus.find_image(_image_path) + def find_image( + self, + *image, + rate: Union[float, int] = None, + multiple: bool = False, + picture_abspath: str = None, + screen_bbox: List[int] = None, + log_level: str = "info", + network_retry: int = None, + pause: [int, float] = None, + timeout: [int, float] = None, + max_match_number: int = None + ): + """ + 在屏幕中区寻找小图,返回坐标, + 如果找不到,根据配置重试次数,每次间隔1秒 + :param picture_abspath: 原始图片绝对路径,不指定时截全屏 + :param image: 模板图片路径 + :param rate: 相似度 + :param multiple: 是否返回匹配到的多个目标 + :param screen_bbox: 截取屏幕上指定区域图片(仅支持X11下使用); + [x, y, w, h] + x: 左上角横坐标;y: 左上角纵坐标;w: 宽度;h: 高度;根据匹配度返回坐标 + :param log_level: 日志级别 + :param network_retry: 连接服务器重试次数 + :param pause: 图像识别重试的间隔时间 + :param timeout: 最大匹配超时,单位秒 + :param max_match_number: 最大匹配次数 + :return: 坐标元组 + """ + _image_path = tuple([_path.replace(conf.HOME, "~", 1) for _path in image]) + if picture_abspath: + picture_abspath = picture_abspath.replace(conf.HOME, "~", 1) + return self.rctl_plus.find_image_remote( + _image_path, rate, multiple, picture_abspath, screen_bbox, log_level, + network_retry, pause, timeout, max_match_number + ) + def ocr( + self, + *target, + picture_abspath: str = None, + similarity: [int, float] = 0.6, + return_default: bool = False, + return_first: bool = False, + lang: str = "ch", + network_retry: int = None, + pause: [int, float] = None, + timeout: [int, float] = None, + max_match_number: int = None + ): + """ + 通过 OCR 进行识别。 + :param target: + 目标字符,识别一个字符串或多个字符串,并返回其在图片中的坐标; + 如果不传参,返回图片中识别到的所有字符串。 + :param picture_abspath: 要识别的图片路径,如果不传默认截取全屏识别。 + :param similarity: 匹配度。 + :param return_default: 返回识别的原生数据。 + :param return_first: 只返回第一个,默认为 False,返回识别到的所有数据。 + :param lang: `ch`, `en`, `fr`, `german`, `korean`, `japan` + :param network_retry: 连接服务器重试次数 + :param pause: 重试间隔时间,单位秒 + :param timeout: 最大匹配超时,单位秒 + :param max_match_number: 最大匹配次数 + :return: 返回的坐标是目标字符串所在行的中心坐标。 + """ + if picture_abspath: + picture_abspath = picture_abspath.replace(conf.HOME, "~", 1) + return self.rctl_plus.ocr_remote(target, picture_abspath, similarity, return_default, return_first, lang, + network_retry, + pause, timeout, max_match_number) if __name__ == '__main__': a = Remote( diff --git a/src/utils/env_vir.sh b/src/utils/env_vir.sh index be21dcf5..cb8852cd 100644 --- a/src/utils/env_vir.sh +++ b/src/utils/env_vir.sh @@ -81,7 +81,7 @@ if [ $? != 0 ]; then exit 121 fi python_virtualenv_path=$(pipenv --venv) -whitelist_path=`echo "${python_virtualenv_path}" | sed "s/\/home\/$USER\//\//"` +whitelist_path=`echo "${python_virtualenv_path}" | sed "s/$HOME\//\//"` if [ -f "${whitelist}" ]; then result=`sudo cat ${whitelist} | grep ${whitelist_path}` if [ -z "$result" ]; then