python识别ocr 图片和pdf文件

复制代码
#识别图片
pip3 install paddleocr
pip3 install paddlepaddle


#识别pdf
pip3 install PyMuPDF

重点:路径不能有中文,不然pdf文件访问不了

复制代码
from paddleocr import PaddleOCR
from rest_framework.response import Response
from rest_framework.views import APIView


# 识别单张图片
class GetOneImage(APIView):
    def get(self, request, *args, **kwargs):
        ocr = PaddleOCR(use_angle_cls=True, lang="ch")  # need to run only once to download and load model into memory
        img_path = 'F:/OCR/data/4.png'
        result = ocr.ocr(img_path, cls=True)
        for idx in range(len(result)):
            res = result[idx]
            for line in res:
                print(line)

        # 显示结果
        for idx in range(len(result)):
            res = result[idx]
            txts = [line[1][0] for line in res]
        return Response({'code': 200, "data": txts})


import datetime
import fitz  # fitz就是pip install PyMuPDF
import os
import cv2
from paddleocr import PPStructure
from paddleocr.ppstructure.recovery.recovery_to_doc import sorted_layout_boxes

# 中文测试图
table_engine = PPStructure(recovery=True, lang='ch')


#识别pdf
class GetPDF(APIView):
    def get(self, request, *args, **kwargs):
        pdfPath = "F:/OCR/image/13.pdf";
        imagePath = "F:/OCR/image/13"
        startTime_pdf2img = datetime.datetime.now()  # 开始时间
        print("imagePath=" + imagePath)
        if not os.path.exists(imagePath):
            os.makedirs(imagePath)
        pdfDoc = fitz.open(pdfPath)
        totalPage = pdfDoc.page_count
        for pg in range(totalPage):
            page = pdfDoc[pg]
            rotate = int(0)
            zoom_x = 2
            zoom_y = 2
            mat = fitz.Matrix(zoom_x, zoom_y).prerotate(rotate)
            pix = page.get_pixmap(matrix=mat, alpha=False)
            print(f'正在保存{pdfPath}的第{pg + 1}页,共{totalPage}页')
            pix.save(imagePath + '/' + f'images_{pg + 1}.png')
        endTime_pdf2img = datetime.datetime.now()
        print(f'{pdfDoc}-pdf2img-花费时间={(endTime_pdf2img - startTime_pdf2img).seconds}秒')
        img_path = imagePath;
        text = []
        imgs = os.listdir(img_path)
        for img_name in imgs:
            img = cv2.imread(os.path.join(img_path, img_name))
            result = table_engine(img)
            h, w, _ = img.shape
            res = sorted_layout_boxes(result, w)
            for line in res:
                line.pop('img')
                print(line)
                for pra in line['res']:
                    text.append(pra['text'])
                text.append('\n')
        return Response({'code': 200, "data": text})

参考:使用paddleOCR批量识别pdf_paddleocr pdf-CSDN博客

相关推荐
正经教主5 分钟前
【FDE系列】阶段2:Day 28:FastAPI 入门 — 把你的函数变成 API 服务
人工智能·python·fde
李航198310 分钟前
用 DeepDraw 几何引擎开发建筑设计软件(十):推挤工具
python·3d
VXbishe1 小时前
【课程设计】基于SpringBoot的乡村政务服务系统的设计与实现-计算机毕设77011
javascript·vue.js·spring boot·python·php·课程设计·政务
名字还没想好☜1 小时前
Python sqlite3 实战:事务提交、参数化防注入、WAL 并发与 row_factory 取字典
后端·python·编程语言
楚识科技1 小时前
云端 API 与私有化 OCR 的架构取舍:数据边界、并发模型与成本测算
ocr
论文复现现场2 小时前
ComfyUI 怎么同时调用 4 张/8 张 RTX 4090?AI 视频批量生成的多实例队列与 Python 调度方案
人工智能·python·comfyui·rtx4090
泡海椒2 小时前
评分系统最佳实践:JQuick-Java实现权重、阈值动态配置评分
java·人工智能·python
weixin199701080162 小时前
《二手ERP对接的对账机制:按日巡检 + 自动补偿,消灭“幽灵订单“》(附Python源码)
python
m0_547486662 小时前
《Python数据分析与实践》全套PPT课件(杭州电子科技大学)
python·数据分析
OKkankan2 小时前
Python 基础进阶(三):从函数、类到 asyncio 与 FastAPI 后端开发实战
开发语言·python