核心思路:
截屏包含toast的图片->裁剪toast区域->ocr识别
核心代码片段:
from datetime import datetime
import ddddocr
from PIL import Image
def ocr_toast(driver, toast_text):
flag = False
img_file = 'full.png'
driver.get_screenshot_as_file(img_file)
# print(f"--log-{datetime.now()}-全屏截图成功路径:{img_file}")
# 截取toast区域
im = Image.open(img_file)
# toast位置 x1,y1,x2,y2(对应左上x1,y1值,右下x2,y2值)
box = (65, 1163, 1020, 1265)
region = im.crop(box)
cropped_path = 'cropped.png'
region.save(cropped_path)
print(f"--log-{datetime.now()}-label区域截图成功:{cropped_path}")
# 识别toast文字
ocr = ddddocr.DdddOcr(beta=True, show_ad=False)
with open(cropped_path, "rb") as f:
img = f.read()
result = ocr.classification(img, png_fix=True)
print(f"ocr识别出的内容为:{result}")
# 判断toast是否包含指定文字 (对汉字数字支持较好,避免使用,~等符号)
if toast_text in result:
flag = True
print(f"--log-{datetime.now()}-ocr识别成功,图片是否包含toast:{flag}")
return flag
案例调用(布丁扫描app):
import time
import unittest
from datetime import datetime
from appium import webdriver
from appium.options.android import UiAutomator2Options
from tools.device_tools import device_name
from tools.ocr_tools_1 import ocr_toast
capabilities = {
"platformName": "Android",
"deviceName": device_name,
"appPackage": "com.wibo.bigbang.ocr",
"appActivity": "com.wibo.bigbang.ocr.MainActivity",
"noReset": True,
}
appium_server_url = "http://localhost:4723/wd/hub"
class TestAppium(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Remote(appium_server_url, options=UiAutomator2Options().load_capabilities(capabilities))
# 设置全局等待时间
self.driver.implicitly_wait(5)
def tearDown(self):
if self.driver:
self.driver.quit()
def test_buding_ocr(self):
time.sleep(5)
print(f"--log-{datetime.now()}已进入app首页")
# 使用tap 避免截屏延迟问题
self.driver.tap([(540, 1330)])
toast_text = "必须勾选同意服务协议和隐私政策才能登录哦"
# 可根据实际情况自行调整延迟时间保证能够截图包含toast
time.sleep(1)
if ocr_toast(self.driver, toast_text):
print("toast抓取成功")
else:
print("toast抓取失败")