快递面单识别
一.基于tesseract的初次尝试
- 跟着本文参考教程走,下载了训练好的语言包,然后直接上代码做预测
输出结果:from PIL import Image from pytesseract import pytesseract import time # paddleocr # paddlepaddle img_path = ('C:/Users/Kaidi/Pictures/Saved Pictures/快递面单/2.jpg','C:/Users/Kaidi/Pictures/Saved Pictures/快递面单/3.png') i=1 total_time = 0 for img_path in img_path: stat_time = time.time() a = pytesseract.image_to_string((Image.open((img_path))), lang='chi_sim') print("—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————") print(f"运行结果{i}: {a}") i+=1 end_time = time.time() stamp = end_time - stat_time total_time += stamp #时间戳 aver_time = total_time / len(img_path) print(f"平均运行时间: {aver_time} 秒")
发现对于部分复杂字体的识别准确度不高,于是找寻解决方法,本文使用paddleocr
二.paddleocr图片高精度识别
3.1配置
- 下载准备
- 下载whl文件
在以下网址找到对应python版本的whl文件(最新只到python3.10)
whl下载链接 - 下载paddleocr
在Terminal输入
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple paddleocr
如果发现以下报错–无法构建 PyMuPDF
- 报错解决
发现是缺失whl文件,查询网址后发现只有到python3.10的版本(我用的3.11),于是用Anaconda重新构建了一个虚拟环境,报错消失 - 下载paddlepaddle
Terminal输入
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple paddlepaddle
- 下载whl文件
- 代码测试
- 代码示例
from PIL import Image from paddleocr import PaddleOCR, draw_ocr import time # import pytesseract i = 1 total_time = 0 ocr = PaddleOCR(use_angle_cls=True, lang="ch", det=False) # 创建ocr实例,修改det -- 只做文本识别不做检测 img_paths = [ 'C:/Users/Kaidi/Pictures/Saved Pictures/快递面单/3.jpg', 'C:/Users/Kaidi/Pictures/Saved Pictures/快递面单/2.jpg', 'C:/Users/Kaidi/Pictures/Saved Pictures/快递面单/3.png', 'C:/Users/Kaidi/Pictures/Saved Pictures/快递面单/mulan.jpg' ] for img_path in img_paths: start_time = time.time() result = ocr.ocr(img_path, cls=True) # 注意每次只传单个img_path print("———————————————————————————————————————————————————————————————————————————————————————————————————————————") print(f"运行结果{i}") for line in result: coordinate = line[0] text = line[1][0] confidence = line[1][1] print(f"text: {text} | confidence: {confidence} | coordinate: {coordinate} ") i += 1 end_time = time.time() elapsed_time = end_time - start_time total_time += elapsed_time average_time = total_time / len(img_paths) print(len(img_paths)) print(f"平均运行时间: {average_time} 秒")
- 目标图片(部分)
- 代码示例
-
识别结果
- 将识别结果保存为图片
- 以下代码放置到for循环内
# 用于可视化结果 --- 保存图片 img = Image.open(img_path).convert("RGB") boxes = [line[0] for line in result[0]] txts = [line[1][0] for line in result[0]] # 报错TypeError: '<' not supported between instances of 'tuple' and 'float', 此处改为从result[0]文本行中提取score # AssertionError: The number of txts and corresponding scores must match # 处理特殊情况(元组结构不同) len>1 保证该行包含文本和置信度两个元素 且第二个元素(即confi)是整数或浮点 else默认1.0 score = [line[1][1] if len(line[1]) > 1 and isinstance(line[1][1], (float, int)) else 1.0 for line in result[0]] img_show = draw_ocr(img, boxes, txts, score) # 1.AssertionError: The number of txts and corresponding scores must match img_show = Image.fromarray(img_show) img_show.save(f'{i}.jpg')
- 图片结果
- 将识别结果保存为图片
3.2关于paddle模型
-
PaddleOCR(和其他文本识别系统)中的坐标和置信度是通过神经网络模型训练得到的。在训练阶段,使用了大量的带有标注的文本图像来训练模型,标注包括了文本区域的坐标和文本内容。模型根据这些标注数据学习如何从图像中检测文本区域和识别文本内容。
-
具体来说,训练阶段包括两个主要任务:
- 文本检测(Text Detection): 在这个任务中,模型学习了如何检测图像中的文本区域,即文本的位置和形状。坐标信息是通过文本检测网络生成的,这些坐标用于描述文本区域的位置。
- 文本识别(Text Recognition): 在这个任务中,模型学习了如何从文本区域中提取和识别文本内容。置信度得分是通过文本识别网络生成的,它表示系统对识别结果的置信程度。
-
模型的训练过程会最小化损失函数,使其能够尽可能地匹配标注数据(即监督学习)。因此,模型在训练过程中通过与标注数据的比对来学习生成坐标和置信度信息,以最大程度地接近正确输出。
三. 附
3.1 虚拟环境构建参考
-
python 3.10 虚拟环境构建
# 在项目目录下创建虚拟环境 virtualenv -p python3.10 venv
source venv/bin/activate # 在Linux/macOS上 # 或 venv\Scripts\activate # 在Windows上 pip install package_name # 安装所需的包
- 报错PEP-514错误
#换用conda创建虚拟环境 conda create --name venv python=3.10
四.说说问题
实测发现虽然paddle识别准确度极高,但运行速度比tesseract慢得多(tesseract用时0.1秒,paddle需要4秒),解决方案请听下回分解