小猿口算脚本

实现原理:安卓adb截图传到电脑,然后用python裁剪获得两张数字图片,使用ddddocr识别数字,比较大小,再用adb命令模拟安卓手势实现><

python 复制代码
import os
import ddddocr
from time import sleep
from PIL import Image

def take_screenshot(path):
    """从设备截取屏幕并保存到指定路径。"""
    os.system(f'adb shell screencap -p > {path}')

    # 读取截取的屏幕截图并替换行结束符
    with open(path, 'rb') as f:
        return f.read().replace(b'\r\n', b'\n')

def process_image(image_path, crop_area):
    """打开图片,裁剪并返回裁剪后的图片。"""
    with Image.open(image_path) as img:
        return img.crop(crop_area)

def extract_text(img):
    """提取图片中的文本。"""
    with open(img, 'rb') as f:
        img_bytes = f.read()
    res = ocr.classification(img_bytes)
    return res.replace(' ', '').replace('\n', '')

def compare_numbers(x, y):
    """比较两个数字并相应地执行滑动操作。"""
    try:
        x_int, y_int = int(x), int(y)
        if x_int > y_int:
            print(f"{x} > {y}")
            os.system("adb shell input swipe 450 1800 850 1900 1")
            os.system("adb shell input swipe 850 1900 450 2000 1")
        else:
            print(f"{x} < {y}")
            os.system("adb shell input swipe 850 1800 450 1900 1")
            os.system("adb shell input swipe 450 1900 850 2000 1")
    except ValueError:
        print("数字格式无效。")

def main():
    """主程序逻辑。"""
    screenshot_path = 'screenshot.png'

    # 截取屏幕并保存
    screenshot = take_screenshot(screenshot_path)
    with open(screenshot_path, 'wb') as f:
        f.write(screenshot)

    # 定义裁剪区域(左,上,右,下)分别是两个数字在图片中的区域坐标
    crop_areas = [
        (330, 720, 530, 880),
        (730, 720, 930, 880)
    ]

    cropped_images = []
    for i, crop_area in enumerate(crop_areas, start=1):
        cropped_image = process_image(screenshot_path, crop_area)
        cropped_image_path = f"screenshot{i}.png"
        cropped_image.save(cropped_image_path)
        cropped_images.append(cropped_image_path)

    # 从裁剪后的图片中提取文本
    texts = [extract_text(image) for image in cropped_images]

    # 比较提取的数字
    compare_numbers(texts[0], texts[1])


if __name__ == '__main__':
    ocr = ddddocr.DdddOcr(show_ad=False)
    while True:
        main()
        sleep(0.2)
相关推荐
2601_957780842 分钟前
Claude 4.6 对阵 GPT-5.4:2026 开发者大模型 API 选型深度解析
人工智能·python·gpt·ai·claude
2601_957780842 分钟前
GPT-5.5 深度解析:2026年4月OpenAI旗舰模型的技术跨越与商业决策指南
大数据·人工智能·python·gpt·openai
zhaoyong22230 分钟前
SQL如何统计每个用户的首次行为时间_MIN聚合与分组
jvm·数据库·python
2501_9010064741 分钟前
C#怎么实现配置热更新 C#如何在运行时动态刷新配置文件不需要重启程序【技巧】
jvm·数据库·python
m0_4708576443 分钟前
HTML怎么创建响应式图片备选方案_HTML srcset与sizes结构【详解】
jvm·数据库·python
2301_795099742 小时前
如何优化SQL中大批量数据的物理删除_分批次与间隔控制
jvm·数据库·python
阿kun要赚马内2 小时前
后端数据操作组合:Pydantic与ORM
后端·python·orm·sqlalchemy
2301_812539672 小时前
CSS如何引入CSS形状生成器_通过自定义属性实现图形化样式
jvm·数据库·python
m0_609160493 小时前
Golang怎么实现数据库连接重试_Golang如何在启动时重试连接直到数据库就绪【技巧】
jvm·数据库·python
花米徐3 小时前
技术洞察精选 | 2026年4月28日 — 5月4日
后端·python·flask