從一個touch方法說起
這個分析我們從IDE的touch方法說起柒桑,拷貝IDE的腳本到pycharm里面伏钠,逐行跟蹤這個touch方法是怎樣執(zhí)行的
# -*- encoding=utf8 -*-
__author__ = "cloudhuan"
from airtest.core.api import *
auto_setup(__file__)
touch(Template(r"tpl1591349971573.png", record_pos=(0.116, -0.266), resolution=(1080, 2340)))
touch方法定義在core/api里面,提供通用的核心api定義
@logwrap
def touch(v, times=1, **kwargs):
"""
Perform the touch action on the device screen
:param v: target to touch, either a Template instance or absolute coordinates (x, y)
:param times: how many touches to be performed
:param kwargs: platform specific `kwargs`, please refer to corresponding docs
:return: finial position to be clicked
:platforms: Android, Windows, iOS
"""
print(">>>>>>>from local source!!!<<<<<<<<<");
if isinstance(v, Template):
pos = loop_find(v, timeout=ST.FIND_TIMEOUT)
else:
try_log_screen()
pos = v
for _ in range(times):
G.DEVICE.touch(pos, **kwargs)
time.sleep(0.05)
delay_after_operation()
return pos
click = touch # click is alias of touch
所以閱讀代碼可知airtest的touch方法都是最終轉(zhuǎn)為坐標點谒府,然后調(diào)用G.DEVICE.touch(pos, **kwargs)方法進行操作屏幕的号阿,所以要分析圖像識別写妥,繼續(xù)跟蹤pos = loop_find(v, timeout=ST.FIND_TIMEOUT)這行語句
查找圖像
loop_find方法在core/cv.py里面,Airtest圖像識別專用
@logwrap
def loop_find(query, timeout=ST.FIND_TIMEOUT, threshold=None, interval=0.5, intervalfunc=None):
"""
Search for image template in the screen until timeout
Args:
query: image template to be found in screenshot
timeout: time interval how long to look for the image template
threshold: default is None
interval: sleep interval before next attempt to find the image template
intervalfunc: function that is executed after unsuccessful attempt to find the image template
Raises:
TargetNotFoundError: when image template is not found in screenshot
Returns:
TargetNotFoundError if image template not found, otherwise returns the position where the image template has
been found in screenshot
"""
G.LOGGING.info("Try finding:\n%s", query)
start_time = time.time()
while True:
screen = G.DEVICE.snapshot(filename=None, quality=ST.SNAPSHOT_QUALITY)
if screen is None:
G.LOGGING.warning("Screen is None, may be locked")
else:
if threshold:
query.threshold = threshold
match_pos = query.match_in(screen)
if match_pos:
try_log_screen(screen)
return match_pos
if intervalfunc is not None:
intervalfunc()
# 超時則raise,未超時則進行下次循環(huán):
if (time.time() - start_time) > timeout:
try_log_screen(screen)
raise TargetNotFoundError('Picture %s not found in screen' % query)
else:
time.sleep(interval)
這個方法大概做了如下兩部操作
- 先調(diào)用設備方法截圖
- 調(diào)用Template對象的match_in方法囤踩,傳入截圖旨椒,返回坐標點
繼續(xù)深入
要知道具體怎么查找的,我們需要繼續(xù)研究cv.py里面的Template對象堵漱,這個對象作為airtest的核心到底是怎么實現(xiàn)圖像查找封裝的综慎,源碼定義同樣在cv.py里面
這個class暴露的只有兩個公開方法,前者是查找單個圖片勤庐,后者支持查找多個圖片示惊,返回值都是坐標點
def match_in(self, screen):
pass
def match_all_in(self, screen):
pass
match_in
方法調(diào)用如下_cv_match-->_try_match
_cv_match方法是讀取core/setting.py里面默認定義的圖像識別算法,列表是有優(yōu)先級的愉镰。匹配到之后就會直接返回結(jié)果
CVSTRATEGY = ["surf", "tpl", "brisk"] # keypoint matching: kaze/brisk/akaze/orb, contrib: sift/surf/brief
這三個默認識別算法的默認映射到cv.py的頂部字典定義
MATCHING_METHODS = {
"tpl": TemplateMatching,
"kaze": KAZEMatching,
"brisk": BRISKMatching,
"akaze": AKAZEMatching,
"orb": ORBMatching,
"sift": SIFTMatching,
"surf": SURFMatching,
"brief": BRIEFMatching,
}
那么也就是說這個字典對應的文件才是圖像算法的真正目錄米罚,對應目錄是aircv目錄,具體實現(xiàn)文件是aircv.keypoint_matching.py和aircv.keypoint_matching_contrib.py
_try_match方法就是執(zhí)行這些方法對象的find_best_result方法丈探,返回結(jié)果
match_all_in
這個方法匹配的是多個录择,調(diào)用_find_all_template-->TemplateMatching.py
和匹配單個的不同,多個匹配直接用模板匹配算法碗降,單個匹配的分析模板匹配默認優(yōu)先級是第二的隘竭。
最終調(diào)用的都是find_all_results方法,進行算法匹配讼渊,返回結(jié)果
總結(jié)
那么圖像識別的基本流程就分析完了动看,從core/api.py的touch方法,跟蹤到cv.py精偿,提供單個和多個的圖像識別方法弧圆,以及默認的setting.py配置項,最終到aircv里面封裝的keypoint_xxxx具體圖像算法笔咽,那么有空再繼續(xù)分析下具體的識別和參數(shù)吧