当前位置:网站首页>Using OCR to reverse recognize text content in airtest
Using OCR to reverse recognize text content in airtest
2022-07-28 17:34:00 【itest_ two thousand and sixteen】
airtest Netease is a development based on image recognition ui Automation framework , The process of element operation is to take a screenshot of the element first , Then compare with the screenshot of the current interface of the client , Find the location , Perform the click operation .
This operation eliminates the complexity of traditional search controls , But the assertion of elements is not precise , Especially in not only judging whether elements exist , And also check the content of the text , It can be said that there is nothing I can do .
I want to solve this problem , You can use the traditional element search method (airtest Built in the way of element search at each end api), Found element , Then judge his text attribute , Get the content ; But in this way, first, we need to add traditional methods such as xpath The relationship between , Second, many times due to non-standard development and other reasons , The positioning attribute of an element is often not easy to determine , Spending time searching has actually lost the convenience of image recognition controls .
In practice ( My project is pc End + The scene of double end interaction of Andrade ), I found it possible to take advantage of airtest Of itself api, Plus ocr Of python library , Identify the content of elements , To solve this problem .
The general idea is , First use airtest Of itself api, Take the screenshot of the content to be identified , And then use it ocr Library to identify its content .
Theoretically , If your device resolution does not change , In fact, there is no need to use airtest Of api, Directly use fixed position , adopt opencv Wait for the image processing library to save the matting , However, the resolution of the production equipment cannot be guaranteed , therefore , Need a relative position , For this position ,airtest There are direct methods available in , This method is also its own resolution compatible method .
airtest Through the coordinates recorded during your screenshot , Device resolution , To generate a recordpos, This pos In fact, it is used to calculate the coordinate offset when the resolution is different .
How to pass recordpos Calculate the coordinates , I won't repeat it here , Mainly through recordpos How to find elements , The method is get_predict_area, There are four parameters , Namely record_pos( The offset when intercepting the control image )、image_wh( Width and height of space )、image_resolution( The device resolution recorded when capturing pictures )、screen_resolution( The actual resolution of the device where the control is found ).
This method will return a quadrangular coordinate , We use the built-in image processing method for this quadrangular coordinate , Save screenshots on the equipment and drawings , You can get the picture of the control to be recognized , And then use ocr The library recognizes this picture , You can get the text content of the control .
The sample code is as follows ( Only applicable to my hardware device , Used as a reference ):
dev = device()
stu_answer_num = r'./valid_pic/stu_answer_num.png' # Screenshot path of the answer area
interact_pic_path = r'./valid_pic/snap_interact.png' # Screenshot path of interactive interface
interact_image = dev.snapshot(interact_pic_path) # Save the screenshot of the interactive interface
from PIL import Image
import pytesseract
from airtest import aircv
from airtest.core.cv import Predictor
screen_resolution = aircv.get_resolution(interact_image) # Get the actual resolution of the screenshot of the interactive interface
# Dynamically create classes that inherit from Predictor, Modify class parameters , Offset value DEVIATION by 0, To obtain accurate screenshot area
predictor = type('Pos', (Predictor,), {'DEVIATION': 0})
xmin, ymin, xmax, ymax = predictor.get_predict_area(record_pos=(0.404, -0.213), image_wh=(265, 45),
image_resolution=(1919, 1040),
screen_resolution=screen_resolution) # Get the screenshot coordinates of the answer area
predict_area = aircv.crop_image(interact_image, (xmin, ymin, xmax, ymax))
aircv.imwrite(stu_answer_num, predict_area) # Save the screenshot of the area to be identified
answer_str = pytesseract.image_to_string(Image.open(stu_answer_num), 'chi_sim') # The recognized words are similar ’ whole class common ginseng And : 1/1‘
print(answer_str)
if answer_str.find(' ginseng And '):
answer_str = answer_str.split(' ')[-1] # take 1/1
answer_num = int(answer_str.split('/')[0]) # Take the number of answers
return answer_num
return
( Please forward if you like , thank you !)
Join beta future qq Group , Get more professional technical knowledge sharing :
274166295 ( Love to test the future two groups )
610934609 ( Love to test the future three groups )
195730410 ( Love to test the future four groups )
More wonderful articles :
Mobile H5 Debugging and Automation
Android What should we do to improve the compatibility test ?
Performance analysis OS Resource saturation
Front end performance monitoring
come from 520 The welfare of the ---- Performance test of live video platform
Front end performance test platform and its application
A classic case that shocked the performance testing Circle !!
——————————————————
Love future official account
WX:itest_forever

The way to test | Testing Technology
Long press to identify the qr code , Pay attention to the official account of Aitai future , Learn more about
边栏推荐
- Mysql database development specification
- Verilog 每日一题(VL8 使用generate…for语句简化代码)
- [sqoop] sqoop 1.4.7 installation integration cdh5.13
- The practice of beego framework developed by goweb: Section 4 database configuration and connection
- 深度分享阿里(蚂蚁金服)技术面试流程,附前期准备,学习方向
- LNMP source code compilation and installation
- Esp-mqtt-at instruction connects Alibaba cloud Internet of things platform
- AMQP协议详解
- Verilog 每日一题 (VL24 多bit MUX同步器 跨时域输出)
- 谈谈你知道的发布上线(一)
猜你喜欢

一文掌握 JVM 面试要点

【CDH】通过 ClouderaManager 配置CDH组件用 prometheus 监控采集JMX信息

Awk of shell script

Verilog 每日一题 (VL5 信号发生器)

Wechat applet cash red packet returns the error "the IP address is not the available IP address you set on the merchant platform". The ultimate solution

Verilog daily question (vl27 settable counter)

ionic 中遇到的一些东西

Encountered.Sqlite file processing during Android Development

高速电路中电感的选型和应用

Introduction to vscade interface
随机推荐
Encrypt the video and upload it to OSS to achieve high concurrent access
一篇带你走近Kubernetes概貌与原理
AMQP protocol details
SNAT、DNAT 防火墙规则的备份和还原
@RequestParam使用
漫谈测试平台—平台建设思路(上)
【presto】presto 常用的命令
Verilog daily question (vl27 settable counter)
Iris framework practice of goweb development: project summary and review
线性代数及矩阵论(七)
Verilog 每日一题(VL14 自动贩售机1--FSM常见题型)
[Presto] details of the new version upgrade of Presto
The actual combat of the beego framework of goweb development: Section III program execution process analysis
Connection design and test platform -- Summary of SystemVerilog interface knowledge points
部署LAMP平台---Linux,Apache,MySQL,PHP的编译安装
Use of influxdb2
一文掌握 JVM 面试要点
Verilog 每日一题(VL2 异步复位的串联T触发器--牛客网)
Esp-mqtt-at instruction connects Alibaba cloud Internet of things platform
Uparse rich text style of uni app