Merge pull request #80 from Marszzz1116/master

This commit is contained in:
mikigo 2024-06-05 18:52:14 +08:00 committed by GitHub
commit 922a894450
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 38 additions and 0 deletions

View File

@ -123,6 +123,44 @@ class OCRUtils:
def all_result(cls):
return cls.result
@classmethod
def ocr_find_by_range(cls, text, x1=None, x2=None, y1=None, y2=None):
"""
OCR在界面中识别到多个关键词时通过区域筛选出对应关键词并返回坐标
:param text: 页面查找关键词
:param x1: x坐标开始范围
:param x2: x坐标结束范围
:param y1: y坐标开始范围
:param y2: y坐标结束范围
:return: 坐标元组 (x, y)
注意需要特定区域内只有一组OCR关键词若任有多组请增加精度否则默认返回第一组符合条件的关键词坐标
以默认分辨率 1920*1080 为例多种示例情况如下
示例1识别左半屏幕关键字ocr_find_by_range(x1=960)
示例2识别下半屏幕关键字ocr_find_by_range(y1=540)
示例3识别左半屏幕-上半屏关键字ocr_find_by_range(x1=960, y1=540)
示例4识别特定区域 100*900-200*950 内关键字ocr_find_by_range(x1=100, x2=200, y1=900, y2=950)
"""
defaults = {
'x1': 0,
'x2': 1920,
'y1': 0,
'y2': 1080
}
x1 = x1 if x1 is not None else defaults['x1']
x2 = x2 if x2 is not None else defaults['x2']
y1 = y1 if y1 is not None else defaults['y1']
y2 = y2 if y2 is not None else defaults['y2']
ocr_return = cls.ocr(text)
if isinstance(ocr_return, dict):
for key, value in ocr_return.items():
if x1 <= value[0] <= x2 and y1 <= value[1] <= y2:
return value
return ocr_return
if __name__ == '__main__':
OCRUtils.ocrx().click()