当前位置: 首页 > article >正文

小猿口算脚本

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

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)


http://www.kler.cn/news/342396.html

相关文章:

  • 前端 NPM
  • MedMamba代码解释及用于糖尿病视网膜病变分类
  • 【Flutter】如何生成和修改 Flutter 应用图标
  • 第二阶段:mysql(学完就隐藏版)
  • Linux 查看当前正在使用的 Bash 版本
  • uniapp打包安卓apk步骤
  • MySQL(B站CodeWithMosh)——2024.10.8(11)
  • 【element-tiptap】如何引进系统中的字体?
  • FreeRTOS和Systemview联合调试——M0内核
  • 使用Pytorch+Numpy+Matplotlib实现手写字体分类和图像显示
  • 【分布式微服务云原生】战胜Redis脑裂:深入解析与解决方案
  • Python中的Socket魔法:如何利用socket模块构建强大的网络通信
  • “国货户外TOP1”凯乐石签约实在智能,RPA助力全域电商运营自动化提效
  • 企业如何借力AI,提升人力资源管理的效率完成组织提效变革
  • processing像素画教程
  • 高防服务器为何有时难以防御CC攻击及其对策
  • 手机控车系统是一种高科技的汽车智能控制系统?
  • VR科技云展如何以沉浸式体验引领科技成果新展示
  • Linux使用Docker部署Paperless-ngx结合内网穿透打造无纸化远程办公
  • tcpdump深入浅出