Python调用ADB读取手机截屏并识别文字信息

运行环境

Windows 11
Anaconda 3
python 11 虚拟环境

创建虚拟环境

conda create --name Cap2Ocr python=3.11
conda activate Cap2Ocr

安装 paddleocr

pip install paddleocr -i https://pypi.tuna.tsinghua.edu.cn/simple

安装完成结果:

Successfully installed Pillow-10.4.0 beautifulsoup4-4.12.3 certifi-2024.8.30 charset-normalizer-3.3.2 colorama-0.4.6 contourpy-1.3.0 cycler-0.12.1 cython-3.0.11 fire-0.6.0 fonttools-4.54.0 idna-3.10 imageio-2.35.1 imgaug-0.4.0 kiwisolver-1.4.7 lazy-loader-0.4 lmdb-1.5.1 lxml-5.3.0 matplotlib-3.9.2 networkx-3.3 numpy-1.26.4 opencv-contrib-python-4.10.0.84 opencv-python-4.10.0.84 packaging-24.1 paddleocr-2.8.1 pyclipper-1.3.0.post5 pyparsing-3.1.4 python-dateutil-2.9.0.post0 python-docx-1.1.2 pyyaml-6.0.2 rapidfuzz-3.10.0 requests-2.32.3 scikit-image-0.24.0 scipy-1.14.1 shapely-2.0.6 six-1.16.0 soupsieve-2.6 termcolor-2.4.0 tifffile-2024.9.20 tqdm-4.66.5 typing-extensions-4.12.2 urllib3-2.2.3

安装 paddle

pip install paddlepaddle -i https://pypi.tuna.tsinghua.edu.cn/simple

安装完成结果:

Successfully installed anyio-4.6.0 astor-0.8.1 decorator-5.1.1 h11-0.14.0 httpcore-1.0.5 httpx-0.27.2 opt-einsum-3.3.0 paddlepaddle-2.6.2 protobuf-3.20.2 sniffio-1.3.1

运行下列代码:

import subprocess
import numpy as np
import cv2
import os
import shutil
import time
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image

def ADB(cmd):
    result = subprocess.Popen(['adb'] + cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = result.communicate()
    return out, err

def list_devices():
    devices = []
    output, _ = ADB('devices')
    lines = output.decode().strip().split('\n')[1:]  # 跳过第一行的标题
    for line in lines:
        device_info = line.strip().split('\t')
        if len(device_info) >= 2 and device_info[1] == 'device':
            devices.append(device_info[0])
    return devices

def CAP():
    No_Error = True
    try:
        # 在设备上保存截图
        _, err = ADB('shell screencap -p /sdcard/screenshot.png')
        if err:
            raise ValueError(f"保存截图失败: {err.decode()}")

        # 检查设备上截图文件的存在
        out, err = ADB('shell ls /sdcard/screenshot.png')
        if b'No such file or directory' in err:
            raise FileNotFoundError("截图文件在设备上不存在")

        # 从设备拉取截图
        out, err = ADB('pull /sdcard/screenshot.png .')
        if b'Error:' in err:
            raise ValueError(f"拉取截图失败: {err.decode()}")

        # 检查截图文件是否存在
        if not os.path.exists('screenshot.png'):
            raise FileNotFoundError("截图文件不存在")

        # 读取截图文件
        with open('screenshot.png', 'rb') as f:
            img_data = f.read()
        
        if len(img_data) == 0:
            raise ValueError("截图数据为空")

        # 尝试直接解码二进制数据
        sc = cv2.imdecode(np.frombuffer(img_data, np.uint8), cv2.IMREAD_COLOR)
        if sc is None:
            raise ValueError("解码后的图像为空")


        
    except Exception as e:
        print(f"无法获取屏幕信息!错误: {e}")
        No_Error = False


# 设置环境变量
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'

def ocr_PaddleOCR(img_path):
    try:
        # 检查图像文件是否存在
        if not os.path.exists(img_path):
            raise FileNotFoundError(f"无法找到图像文件: {img_path}")

        # 初始化PaddleOCR
        ocr = PaddleOCR(use_angle_cls=True, lang="ch")  # lang参数可以根据需要更改

        # 加载图像
        img = cv2.imread(img_path)
        if img is None:
            raise FileNotFoundError(f"无法加载图像文件: {img_path}")

        # 进行OCR识别
        result = ocr.ocr(img, cls=True)

        # 输出结果
        for line in result:
            print(line)

    except Exception as e:
        print(f"发生错误: {e}")
    
    return result


start_time = time.time()
    
devices = list_devices()
if not devices:
    print("没有找到连接的设备,请确保设备已连接并开启USB调试模式。")
else:
    print(f"已找到设备: {', '.join(devices)}")
        
CAP()
        
img_path = 'screenshot.png'
result = ocr_PaddleOCR(img_path)
end_time = time.time()

# 清理临时文件
os.remove('screenshot.png')

print(f'\n ==== OCR cost time: {end_time - start_time} ====\n')

print('文字列表如下:')

# print(result[0][0][1][0])
for i in range(len(result)):
    for j in range(len(result[i])):
        print(result[i][j][1][0])
 

首次运行会去下载模型,出现以下结果:

download https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_det_infer.tar to C:\Users\Lenovo/.paddleocr/whl\det\ch\ch_PP-OCRv4_det_infer\ch_PP-OCRv4_det_infer.tar
100%|█████████████████████████████████████████████████████████████████████████████| 4.89M/4.89M [00:08<00:00, 577kiB/s]
download https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_rec_infer.tar to C:\Users\Lenovo/.paddleocr/whl\rec\ch\ch_PP-OCRv4_rec_infer\ch_PP-OCRv4_rec_infer.tar
100%|█████████████████████████████████████████████████████████████████████████████| 11.0M/11.0M [00:11<00:00, 981kiB/s]
download https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar to C:\Users\Lenovo/.paddleocr/whl\cls\ch_ppocr_mobile_v2.0_cls_infer\ch_ppocr_mobile_v2.0_cls_infer.tar
100%|█████████████████████████████████████████████████████████████████████████████| 2.19M/2.19M [00:05<00:00, 366kiB/s]

之后就成功啦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值