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博客

相关推荐
Είναι η κοπέλα14 分钟前
显存计算与模型选择:你的显卡能跑多大的模型
人工智能·pytorch·python·开源·conda
ROSF686819 分钟前
2026 仿石漆乳液供应商:市场布局与行业发展态势梳理
大数据·人工智能·python
青少儿编程课堂27 分钟前
AC自动机多模式串匹配精讲:校园关键词巡检实战解析
c++·python·算法·bfs·信息学竞赛
hhzz1 小时前
【YOLO 入门到精通 04】理解任务与模型家族:7 大 CV 任务与 YOLO26 选型指南
人工智能·python·深度学习·yolo·计算机视觉
Escalating_xu1 小时前
【Python】使用库:标准库、第三方库、pip、项目实战与程序打包
java·python·pip
水獭比特1 小时前
Agent 工具审批不止一个按钮:用 v0.22.1 复盘入口、恢复和副作用
人工智能·python
苦瓜打怪兽1 小时前
leetcode 热题100(11. 盛最多水的容器)
python·算法·leetcode
跨境数据猎手1 小时前
ddddocr 与多模态大模型 OCR 对比和选择建议
ocr
砚底藏山河2 小时前
量化实战:回测数据底座快照与版本管理进阶
java·大数据·python·金融·maven
“AI国潮设计-小江”2 小时前
《Python+SDXL实战:用ControlNet批量生成“英歌舞麻将糕”IP,附自动化脚本与商用思路》
开发语言·人工智能·python·prompt·aigc