使用百度飞桨PaddleOCR进行OCR识别

1、代码及文档

代码:https://github.com/PaddlePaddle/PaddleOCR?tab=readme-ov-file

介绍文档:https://paddlepaddle.github.io/PaddleOCR/ppocr/overview.html

2、依赖安装

在使用过程中需要安装库,可以依据代码运行过程中的提示安装。我使用的为python3.7,安装库为:

3、poppler for PDF OCR

我主要使用图片OCR以及PDF转DOCX文件,后者需要poppler,我使用的系统为windows .在此处进行下载:
https://github.com/oschwartz10612/poppler-windows/releases/tag/v24.07.0-0
解压后需要将poppler的bin路径加到系统环境变量path中

4、图像ocr代码样例:

bash 复制代码
from paddleocr import PaddleOCR, draw_ocr

# Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换
# 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`
ocr = PaddleOCR(use_angle_cls=True, lang="ch")  # need to run only once to download and load model into memory
img_path = './doc/imgs_en/254.jpg'
result = ocr.ocr(img_path, cls=True)
for idx in range(len(result)):
    res = result[idx]
    for line in res:
        print(line)

# 显示结果
from PIL import Image
result = result[0]
image = Image.open(img_path).convert('RGB')
boxes = [line[0] for line in result]
txts = [line[1][0] for line in result]
scores = [line[1][1] for line in result]
im_show = draw_ocr(image, boxes, txts, scores, font_path='./fonts/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')

5、pdf ocr代码样例:

bash 复制代码
import os

from pdf2image import convert_from_path
from paddleocr import PaddleOCR
import numpy as np
from docx import Document
from PIL import Image
current_path = os.path.abspath(__file__)
father_path = os.path.abspath(os.path.dirname(current_path))
input_path = os.path.join(father_path, 'mydata', 'input', '种植品种推荐1.pdf')
output_path = os.path.join(father_path, 'mydata', 'output', '种植品种推荐1.docx')
# 步骤 1: 将 PDF 转换为图片
pages = convert_from_path(input_path, 300)  # 转换为 PIL 图像对象

# 步骤 2: 初始化 OCR 模型
ocr = PaddleOCR(use_angle_cls=True, lang='ch')

# 步骤 3: 遍历每一页,进行 OCR 处理
results = []
for page in pages:
    # 将 PIL 图像转换为 numpy 数组
    page_np = np.array(page)

    # 使用 OCR 提取文本
    ocr_result = ocr.ocr(page_np, cls=True)
    page_text = []
    for line in ocr_result:
        if line:  # 检查 line 是否为 None
            for word_info in line:
                # word_info 包含了文本和置信度
                text, confidence = word_info[1]
                page_text.append(text)  # 提取文本部分

    results.append('\n'.join(page_text))

# 打印提取的文本
# for result in results:
#     print(result)

# 步骤 4: 将文本保存为 DOCX 文件
doc = Document()
for i, page_text in enumerate(results, 1):
    doc.add_heading(f'Page {i}', level=1)
    doc.add_paragraph(page_text)

doc.save(output_path)

注意修改为自己的文件路径

6、识别效果:

原文件:

识别效果:

相关推荐
开开心心就好7 小时前
高效全能PDF工具,支持OCR识别
java·前端·python·pdf·ocr·maven·jetty
林泽毅13 小时前
PaddleNLP框架训练模型:使用SwanLab教程
人工智能·深度学习·机器学习·大模型·paddlepaddle·模型训练·swanlab
一个人的博客@你1 天前
C# 通用OCR识别
图像处理·c#·ocr·图像识别·文字提取
Grassto2 天前
dockerfile: PaddleOCR hubserving api 服务
docker·ocr·paddleocr
TextIn智能文档云平台3 天前
PDF文档解析新突破:图表识别、公式还原、手写字体处理,让AI真正读懂复杂文档!
图像处理·人工智能·算法·自然语言处理·pdf·ocr
带鱼工作室3 天前
通义读光系列文字检测+识别模型端到端OCR应用
python·opencv·计算机视觉·ocr
白熊1884 天前
【计算机视觉】OpenCV实战项目:Text-Extraction-Table-Image:基于OpenCV与OCR的表格图像文本提取系统深度解析
opencv·计算机视觉·ocr
沉到海底去吧Go4 天前
【身份证识别表格】批量识别身份证扫描件或照片保存为Excel表格,怎么大批量将身份证图片转为excel表格?基于WPF和腾讯OCR的识别方案
ocr·wpf·excel·身份证识别表格·批量扫描件身份证转表格·图片识别表格·图片识别excel表格
伊织code5 天前
MixTeX - 支持CPU推理的多模态LaTeX OCR
python·ai·ocr·latex·mixtex
十步杀一人_千里不留行7 天前
【实战教程】React Native项目集成Google ML Kit实现离线水表OCR识别
react native·react.js·ocr