# 包路径
from airscript.screen
图色检索
 图像 
Screen.
# 所属包
from airscript.screen import Screen
# 通过Screen 操作
Screen.*
图像缓存
Screen
# 所属包
from airscript.screen import Screen
# 打开缓存
Screen.cache(True) 
# 关闭缓存
Screen.cache(True) 
从屏幕获取图像
Screen
from airscript.screen import Screen 
from airscript.system import R
# 截取整个屏幕为Bitmap对象
bitmap = Screen.bitmap();
# 截取屏幕范围[361,309,718,659]为Bitmap对象
bitmap = Screen.bitmap(361,309,718,659);
# 打印截取的图片宽度, 关于Bitmap的更多属性请见:https://developer.android.google.cn/reference/kotlin/android/graphics/Bitmap?hl=en
print(bitmap.getWidth()) 
从文件获取图像
Screen
from airscript.screen import Screen 
from airscript.system import R
# 将sd卡中的文件/a/3.jpg 读取为bitmap对象
bitmap = Screen.file2Bitmap(R.sd('/a/3.jpg'))
print(bitmap.getWidth())
保存图像
Screen
# 导包
from airscript.screen import Screen 
from airscript.system import R
# 截取屏幕到 /sd/a/1.png 目录中
cpFile =  Screen.toFile(R.sd('/a/1.png'));
# 截取屏幕区域[10,10,500,500]到 /sd/a/2.png 目录中
cpFile =  Screen.toFile(R.sd('/a/2.jpg'),Screen.bitmap(10,10,500,500));
# 截取屏幕到 /sd/a/3.png 目录中,清晰度为 50%
cpFile =  Screen.toFile(R.sd('/a/3.jpg'),50);
# 截取屏幕区域[10,10,500,500]到 /sd/a/4.png 目录中,清晰度为30%
cpFile =  Screen.toFile(R.sd('/a/4.jpg'),Screen.bitmap(10,10,500,500),30);
图像转base64
Screen
from airscript.screen import Screen 
# 先获取Bitmap截图
bp = Screen.bitmap()
#再将bitmap 转换为 base64 字符串
b64str = Screen.base64(bp)
print(b64str)
图像缩放
Screen
from airscript.screen import Screen 
# 先获取Bitmap截图
bp = Screen.bitmap()
#将 bitmap 图像 最大边缩放至1000,图片等比缩放.
bp = Screen.maxside(bp,1000)
 找色 
# 导包
from airscript.screen import FindColors
构造
FindColors
# 导包
from airscript.screen import FindColors
# 构造一个FindColor对象
FindColors('426,346,#05D395|502,351,#05D015|676,569,#05D294')
范围
FindColors(colors)
缺省rect参数将默认全屏幕找色
间隔
FindColors(colors)
缺省space 默认为 5
方向
FindColors(colors)
缺省ori 默认为 2
偏色
FindColors(colors)
缺省ori 默认为 2
查找一个结果
FindColors(colors)
| 返回结果 | 备注 | 
|---|---|
| android.graphics.Point | 点位对象, Point有x和y属性.用来获取坐标位置. | 
# 导包
from airscript.screen import FindColors
# 构造一个FindColor对象,并执行查找
point = FindColors('931,549,#EFEFEF|932,496,#EEEEEE|964,523,#EFEFEF|862,475,#0B0B16').find()
if point:
    # 打印 x坐标 和 y坐标
    print(point.x,point.y)
查找全部结果
FindColors(colors)
| 返回结果 | 备注 | 
|---|---|
| android.graphics.Point[] | 点位对象数组,Point有x和y属性.可用来获取坐标位置. | 
# 导包
from airscript.screen import FindColors
# 构造一个FindColor对象,并执行查找
points = FindColors('931,549,#EFEFEF|932,496,#EEEEEE|964,523,#EFEFEF|862,475,#0B0B16').find_all()
if points:
    print('共查到%d个结果'%len(points))
    for point in points:
        print(point.x,point.y)
找图
# 导包
from airscript.screen import FindImages
通过局部图片,我们可以在屏幕中找到该图片的位置信息
- 找图支持全分辨率.(局部图片尺寸不变,屏幕如何缩放,均可以找到位置信息)
 
什么时候用找图合适
当我们想适配全分辨率时,可通过找图方法.
构造
FindImages
# 导包
from airscript.screen import FindImages
# 导入上下文环境包,方便导入图片地址
from airscript.system import R
# 构造一个FindColor对象,需要传入一个局部图片的路径
# 通过上下文R得到a.png的绝对位置
path = R(__file__).res("/img/a.png")
# 将路径传入FindImages 构建一个找图对象
FindImages(path)
范围
FindImages(part_img).
# 导包
from airscript.screen import FindImages
# 导入上下文环境包,方便导入图片地址
from airscript.system import R
# 构造一个FindColor对象,需要传入一个局部图片的路径
# 通过上下文R得到a.png的绝对位置
path = R(__file__).res("/img/a.png")
# 将路径传入FindImages 构建一个找图对象,并指定查找图的范围 是[0,0,300,300]
FindImages(path).rect(0,0,300,300)
信心
FindImages(part_img).
# 导包
from airscript.screen import FindImages
# 导入上下文环境包,方便导入图片地址
from airscript.system import R
# 构造一个FindColor对象,需要传入一个局部图片的路径
# 通过上下文R得到a.png的绝对位置
path = R(__file__).res("/img/a.png")
# 将路径传入FindImages 构建一个找图对象,并指定要获取的结果准确度必须0.8以上.
FindImages(path).confidence(0.8)
查找模式
- 返回一个结果
 
| 查找模式 | 备注 | 
|---|---|
| find | 先使用 find_template 查找,如果找不到在用 find_sift 查找 | 
| find_sift | 支持全分辨率的单图查找模式 优点:全分辨率 缺点:速度慢 | 
| find_template | 优点:速度快 缺点:不支持全分辨率 | 
- 查找多个结果
 
| 查找模式 | 备注 | 
|---|---|
| find_all | 先使用 find_all_template 查找,如果找不到在用 find_all_sift 查找 | 
| find_all_sift | 持全分辨率的查找模式 优点:全分辨率 缺点:速度慢 | 
| find_all_template | 优点:速度快 缺点:不支持全分辨率 | 
查找一个结果
FindImages(part_img).
FindImages(part_img).
FindImages(part_img).
# 导包
from airscript.screen import FindImages
from airscript.system import R
# 构造找图对象,并执行查找
res = FindImages(R(__file__).res("/img/a.png")).find() # 先使用 find_template找不到结果,在用find_sift查询
# res = FindImages(R(__file__).res("/img/a.png")).find_sift() #  全分辨率形式查询
# res = FindImages(R(__file__).res("/img/a.png")).find_template() #  模版查询,速度快,但不支持全分辨率
#判断结果不为空
if res:
    #输入找图结果的属性
    print("中心坐标为:x=%d,y=%d" % (res['result'][0],res['result'][1]))
    print("范围:",res["rectangle"])
    print("准确度:",res["confidence"])
查找所有结果
FindImages(part_img).
FindImages(part_img).
FindImages(part_img).
# 导包
from airscript.screen import FindImages
from airscript.system import R
# 构造找图对象,过滤掉,可信度小于0.6的 并查找全部结果
ress = FindImages(R(__file__).res("/img/a.png")).confidence(0.6).find_all()
# ress = FindImages(R(__file__).res("/img/a.png")).confidence(0.6).find_all() # 全分辨率查找模式,速度慢
# ress = FindImages(R(__file__).res("/img/a.png")).confidence(0.6).find_all_template() # 模版查找模式,速度快,但不支持全分辨率
#判断结果不为空
if ress:
    print('共查到%d个结果'%len(ress))
    #输入找图结果的属性
    for res in ress:
        print("中心坐标为:x=%d,y=%d" % (res['result'][0],res['result'][1]))
        print("范围:",res["rectangle"])
        print("准确度:",res["confidence"])
区域颜色量
# 导包
from airscript.screen import GetColorNum
获取全屏 或指定区域的颜色点数量
构造
GetColorNum()
GetColorNum(colors)
# 导包
from airscript.screen import GetColorNum
# 创建一个获取区域颜色数量的对象
GetColorNum()
# 创建一个获取区域颜色数量的对象 ,并指定颜色值
GetColorNum("#FFFFFF|#000000-#FFFFFF")
颜色值
设置要查找的颜色值
GetColorNum().
| 参数 | 类型 | 必须 | 备注 | 
|---|---|---|---|
| strcolr | string | 必填 | 格式1: "#FFFFFF" 检索单一颜色值数量 格式2: "#FFFFFF|#000000" 多种颜色满足其一则算数 格式3:"#000000-#111111" 给定颜色值 在 两个颜色值之间则算数  | 
# 导包
from airscript.screen import GetColorNum
# 格式1:
num = GetColorNum().colors("#FFFFFF").find()
print(num)
# 格式2:
num = GetColorNum().colors("#FFFFFF|#000000").find()
print(num)
# 格式3:
num = GetColorNum().colors("#000000-#111111").find()
print(num)
# 混合模式:
num = GetColorNum().colors("#FFFFFF|#000000-#111111").find()
print(num)
范围
指定 区域范围
GetColorNum().
| 参数 | 类型 | 必须 | 备注 | 
|---|---|---|---|
| left,top,right,bottom | int | 必填 | 屏幕范围坐标,可用图色检索工具获取 | 
# 导包
from airscript.screen import GetColorNum
num = GetColorNum("#FFFFFF").rect(0,0,200,200).find()
print(num)
相似度
设置相似度
GetColorNum().
| 参数 | 类型 | 必须 | 备注 | 
|---|---|---|---|
| arg | int | 必填 | 0-1之间,默认0.9,表示90%相似度颜色都算在内 | 
# 导包
from airscript.screen import GetColorNum
num = GetColorNum("#FFFFFF").sim(0.85).find()
print(num)
查找
执行查找
GetColorNum().
- 无参数
 
# 导包
from airscript.screen import GetColorNum
num = GetColorNum("#FFFFFF").sim(0.85).find()
print(num)
目标检测
# 导包
from airscript.screen import yolo_v5
经过数据集训练,加载模型.检测出屏幕中的目标
模型加载
yolo_v5
创建一个yolov5目标检测对象,并初始化模型
| 参数 | 类型 | 必须 | 备注 | 
|---|---|---|---|
| model | string | 必填 | 从模型库 中加载:("模型名称:模型版本号") 也可加载模型文件,填写文件路径即可.  | 
from airscript.screen import yolo_v5
# 从模型库中加载
yolo = yolo_v5("微信跳一跳:1.5")
from airscript.screen import yolo_v5
# 从文件中加载模型()
yolo = yolo_v5(R(__file__).root("as.ai"))
检测目标
yolo_v5(model).
检测屏幕中的所有目标
| 返回结果 | 备注 | 
|---|---|
| Obj[] | 结果对象数组 | 
| Obj属性 | 备注 | 
|---|---|
| x | 目标的x坐标 | 
| y | 目标的y坐标 | 
| w | 目标的宽度 | 
| w | 目标的高度 | 
| label | 目标名称 | 
| prob | 准确度,(0-1之间,1为100%准确) | 
from airscript.screen import yolo_v5
# 从模型库中加载
yolo = yolo_v5("微信跳一跳:1.5")
res = yolo.find_all()
if res:
    #打印检测结果
    print(res)
模型训练
模型训练 有很多种方式
文字识别
# 导包
from airscript.screen import Ocr
将屏幕中的文字识别出来
构造
Ocr
创建一个文字识别对象
# 导包
from airscript.screen import Ocr
# 创建一个中英文识别对象
Ocr()
模式
Ocr()
#导包
from airscript.screen import Ocr # 文字识别
#将识别模式切换为paddlev3 ,如果不调用mode方法,默认paddlev2识别
Ocr().mode(3)
范围
Ocr()
#导包
from airscript.screen import Ocr # 文字识别
#创建一个文字识别对象,指定屏幕识别范围
Ocr().rect(182,630,853,1261)
匹配
Ocr()
#导包
from airscript.screen import Ocr # 文字识别
#,指定要匹配的结果中包含 “小程序”
Ocr().pattern(".*小程序.*")
最大边
Ocr()
#导包
from airscript.screen import Ocr # 文字识别
#指定所有输入资源 最大边缩放至1200px 后再识别
Ocr().max_side_len(1200)
精度
Ocr()
#导包
from airscript.screen import Ocr # 文字识别
# 设置识别精度为16
Ocr().precision(16)
从Bitmap识别
Ocr()
#导包
from airscript.screen import Ocr, Screen
Ocr().bitmap(Screen.bitmap()).find_all()
从文件识别
Ocr()
#导包
from airscript.screen import Ocr # 文字识别
from airscript.system import R # 文字识别
#,指定要识别的文件,为当前工程下的
Ocr().file(R.sd("/airscript/temp.png"))
识别一个结果
Ocr()
| 返回结果 | 备注 | 
|---|---|
| OcrText | 属性对象 具体属性 见下方代码块  | 
#find 返回的结果对象
OcrText {
	confidence = 0.8111246228218079, # 可信度
    text = '*才',                    # 识别到文本
    text_box_position = [            # 识别到的文本在屏幕上的区域坐标
		[746, 32],
		[908, 32],
		[908, 82],
		[746, 82]
	]
}
#导包
from airscript.screen import Ocr # 文字识别
# 识别一个结果
ot =  Ocr().find()
print(ot)
if ot:
    print(ot.confidence) #可信度
    print(ot.text) #识别到文本
    otRect = ot.text_box_position
    print(otRect)
    # 这里的范围是4个坐标,因为文本在屏幕上可能不是规则的矩形. 因此需要4个点位来确定区域
    print(otRect[0][0],otRect[0][1]) # 左上角顶点x,y 坐标
    print(otRect[1][0],otRect[1][1]) # 右上角顶点x,y 坐标
    print(otRect[2][0],otRect[2][1]) # 右下角顶点x,y 坐标
    print(otRect[3][0],otRect[3][1]) # 左下角顶点x,y 坐标
识别全部结果
Ocr()
| 返回结果 | 备注 | 
|---|---|
| OcrText[] | 属性对象数组 具体属性 见下方代码块  | 
#find 返回的结果对象
[
    OcrText {
        confidence = 0.8111246228218079, # 可信度
        text = '小程序',                    # 识别到文本
        text_box_position = [            # 识别到的文本在屏幕上的区域坐标
            [746, 32],
            [908, 32],
            [908, 82],
            [746, 82]
        ]
    },
    OcrText {
        confidence = 0.8111246228218079, # 可信度
        text = '应用',                    # 识别到文本
        text_box_position = [            # 识别到的文本在屏幕上的区域坐标
            [746, 32],
            [908, 32],
            [908, 82],
            [746, 82]
        ]
    }...
]
#导包
from airscript.screen import Ocr # 文字识别
#识别全部
ots =  Ocr().find_all()
print(ots)
if ots:
    # 循环打印出所有识别结果
    for ot in ots:
        print(ot.text) #识别到文本
        print(ot.confidence) #可信度
        otRect = ot.text_box_position
        print(otRect)
        # 这里的范围是4个坐标,因为文本在屏幕上可能不是规则的矩形. 因此需要4个点位来确定区域
        print(otRect[0][0],otRect[0][1]) # 左上角顶点x,y 坐标
        print(otRect[1][0],otRect[1][1]) # 右上角顶点x,y 坐标
        print(otRect[2][0],otRect[2][1]) # 右下角顶点x,y 坐标
        print(otRect[3][0],otRect[3][1]) # 左下角顶点x,y 坐标
比色
# 导包
from airscript.screen import CompareColors
对比多个坐标点的颜色 是否与当前屏幕想符
比色 和 找色
比色要比找色快很多
构造
CompareColors
# 导包
from airscript.screen import CompareColors
# 构造一个CompareColors对象
CompareColors('426,346,#05D395|502,351,#05D015|676,569,#05D294')
偏色
CompareColors(colors)
# 导包
from airscript.screen import CompareColors
# 构造一个CompareColors对象
CompareColors('426,346,#05D395|502,351,#05D015|676,569,#05D294').diff("#050505")
重复直到成功
CompareColors(colors)
# 导包
from airscript.screen import CompareColors
# 构造一个CompareColors对象,并执行比色,直到返回true为止
CompareColors('426,346,#05D395|502,351,#05D015|676,569,#05D294').until()
异步执行
CompareColors(colors)
| 参数 | 类型 | 必须 | 备注 | 
|---|---|---|---|
| method | function | 必填 | 一个python的 回掉函数,函数必须存在一个形式参数,接收比色结果 | 
# 导包
#导包
from airscript.screen import CompareColors 
#比色异步回调函数
def compare_result(res):
    print(res)
#创建一个比色,并使用异步线程执行
CompareColors('321,1515,#4CDAAF|512,1539,#51DBB1').asy(compare_result)
执行
CompareColors(colors)
| 返回结果 | 备注 | 
|---|---|
| boolean | 比色结果,全部正确返回True,否则返回False | 
-案例1: 执行比色返回结果
#导包
from airscript.screen import CompareColors 
#创建一个比色,同步执行比色,直到比对成功
res = CompareColors('321,1515,#4CDAAF|512,1539,#51DBB1').compare()
print(res)
-案例2: 异步执行比色返回结果
#导包
from airscript.screen import CompareColors 
#比色异步回调函数
def compare_result(res):
    print(res)
#创建一个比色,并使用异步线程执行
CompareColors('321,1515,#4CDAAF|512,1539,#51DBB1').asy(compare_result).compare()
-案例3: 异步执行比色,直到比对成功后,返回结果
#导包
from airscript.screen import CompareColors 
#比色异步回调函数
def compare_result(res):
    print(res)
#创建一个比色,并使用异步线程执行,直到比对成功才返回结果
CompareColors('321,1515,#4CDAAF|512,1539,#51DBB1').asy(compare_result).until().compare()
-案例4: 阻塞执行比色,直到比对成功后,返回结果
#导包
from airscript.screen import CompareColors 
#创建一个比色,同步执行比色,直到比对成功
res = CompareColors('321,1515,#4CDAAF|512,1539,#51DBB1').until().compare()
print(res)
二维码识别
# 导包
from airscript.screen import QRcode
从屏幕或图片中识别二维码
构造
QRcode
# 导包
from airscript.screen import QRcode
# 构造一个QRcode对象
QRcode()
范围
QRcode()
# 导包
from airscript.screen import QRcode
# 构造一个二维码识别对象,并制定要识别的屏幕范围
QRcode().rect(100,200,400,600)
从文件识别
QRcode()
# 导包
from airscript.screen import QRcode
from airscript.system import R
# 构造一个QRcode对象,并指定要识别的 图片文件地址
QRcode().file(R.sd("1.png"))
从Bitmap识别
QRcode()
# 导包
from airscript.screen import QRcode
from airscript.screen import Screen 
# 构造一个QRcode对象,并指定要识别的内存图片
QRcode().bitmap(Screen.bitmap())
识别
QRcode()
#案例1:从屏幕识别二维码
# 导包
from airscript.screen import QRcode
# 构造一个QRcode对象,并指定要识别的内存图片
s = QRcode().find()
# 打印识别的结果
print(s)
#案例2:从屏幕指定范围识别二维码
# 导包
from airscript.screen import QRcode
# 构造一个QRcode对象,并指定要识别的内存图片
s = QRcode().rect(204,739,915,1443).find()
# 打印识别的结果
print(s)
#案例3:从屏幕指定文件识别二维码
# 导包
from airscript.screen import QRcode
from airscript.system import R
# 构造一个QRcode对象,并指定要识别的内存图片
s = QRcode().file(R.sd("2.jpg")).find()
# 打印识别的结果
print(s)
#案例4:从Bitmap 识别二维码
# 导包
from airscript.screen import QRcode
from airscript.screen import Screen
# 构造一个QRcode对象,并指定要识别的内存图片
s = QRcode().bitmap(Screen.bitmap()).find()
# 打印识别的结果
print(s)