当前位置: 首页 > news >正文

临海市住房和城乡建设规划局网站灰色行业seo

临海市住房和城乡建设规划局网站,灰色行业seo,采集微信公众号 做网站,网站建设书本信息文章用于学习记录 文章目录 前言一、PDF 文件转换为图片二、OCR 图片文字识别提取三、服务器端下载运行 PaddleOCR四、下载权重文件总结 前言 文字识别(Optical Character Recognition,简称OCR)是指将图片、扫描件或PDF、OFD文档中的打印字符…

文章用于学习记录

文章目录

  • 前言
  • 一、PDF 文件转换为图片
  • 二、OCR 图片文字识别提取
  • 三、服务器端下载运行 PaddleOCR
  • 四、下载权重文件
  • 总结


前言

文字识别(Optical Character Recognition,简称OCR)是指将图片、扫描件或PDF、OFD文档中的打印字符进行检测识别成可编辑的文本格式。


一、PDF 文件转换为图片

import datetime
import osimport fitz  #pip install PyMuPDFdef pyMuPDF_fitz(pdfPath, imagePath):startTime_pdf2img = datetime.datetime.now()  # 开始时间print("imagePath=" + imagePath)pdfDoc = fitz.open(pdfPath)for pg in range(pdfDoc.pageCount):page = pdfDoc[pg]rotate = int(0)# 每个尺寸的缩放系数为1.3,这将为我们生成分辨率提高2.6的图像。# 此处若是不做设置,默认图片大小为:792X612, dpi=96zoom_x = 1.33333333  # (1.33333333-->1056x816)   (2-->1584x1224)zoom_y = 1.33333333mat = fitz.Matrix(zoom_x, zoom_y).preRotate(rotate)pix = page.getPixmap(matrix=mat, alpha=False)if not os.path.exists(imagePath):  # 判断存放图片的文件夹是否存在os.makedirs(imagePath)  # 若图片文件夹不存在就创建pix.writePNG(imagePath + '/' + 'images_%s.png' % pg)  # 将图片写入指定的文件夹内endTime_pdf2img = datetime.datetime.now()  # 结束时间print('pdf2img时间=', (endTime_pdf2img - startTime_pdf2img).seconds)if __name__ == "__main__":# 1、PDF地址pdfPath = './pdf/note.pdf'# 2、需要储存图片的目录imagePath = 'pdf'pyMuPDF_fitz(pdfPath, imagePath)

在这里插入图片描述

  • AttributeError: ‘Document‘ object has no attribute ‘pageCount‘ PyMuPDF库
  • 由于 PyMuPDF 库更新导致的,里面的一些函数名发生了变化
  • 将 pageCount 改为 page_count

在这里插入图片描述

  • 将 preRotate 改为 prerotate

在这里插入图片描述

  • 将 getPixmap 改为 get_pixmap

在这里插入图片描述

  • 将 writePNG 改为 save
  • 这是要转换的 PDF 文件

在这里插入图片描述

  • 修改后
import datetime
import osimport fitz  # fitz就是pip install PyMuPDFdef pyMuPDF_fitz(pdfPath, imagePath):startTime_pdf2img = datetime.datetime.now()  # 开始时间print("imagePath=" + imagePath)pdfDoc = fitz.open(pdfPath)for pg in range(pdfDoc.page_count):page = pdfDoc[pg]rotate = int(0)# 每个尺寸的缩放系数为1.3,这将为我们生成分辨率提高2.6的图像。# 此处若是不做设置,默认图片大小为:792X612, dpi=96zoom_x = 1.33333333  # (1.33333333-->1056x816)   (2-->1584x1224)zoom_y = 1.33333333mat = fitz.Matrix(zoom_x, zoom_y).prerotate(rotate)pix = page.get_pixmap(matrix=mat, alpha=False)if not os.path.exists(imagePath):  # 判断存放图片的文件夹是否存在os.makedirs(imagePath)  # 若图片文件夹不存在就创建pix.save(imagePath + '/' + 'images_%s.png' % pg)  # 将图片写入指定的文件夹内endTime_pdf2img = datetime.datetime.now()  # 结束时间print('pdf2img时间=', (endTime_pdf2img - startTime_pdf2img).seconds)if __name__ == "__main__":# 1、PDF地址pdfPath = r'D:\BaiduNetdiskDownload\PaddleOCR-release-2.7\PaddleOCR-release-2.7\pdf\note.pdf'# 2、需要储存图片的目录imagePath = r'D:\BaiduNetdiskDownload\PaddleOCR-release-2.7\PaddleOCR-release-2.7\pdf'pyMuPDF_fitz(pdfPath, imagePath)
  • 这是转换后的两张图片

在这里插入图片描述

二、OCR 图片文字识别提取

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 = './pdf/images_0.png'
result = ocr.ocr(img_path, cls=True)
for idx in range(len(result)):res = result[idx]for line in res:print(line)# 显示结果
# 如果本地没有simfang.ttf,可以在doc/fonts目录下下载
from PIL import Imageresult = 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='doc/fonts/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

三、服务器端下载运行 PaddleOCR

git clone https://github.com/PaddlePaddle/PaddleOCR.git

在这里插入图片描述

# 进入 pytorch 虚拟环境
conda activate pytorch# 命令行进入 PaddleOCR 文件夹下
cd PaddleOCR# 识别单张图片
python tools/infer/predict_system.py --image_dir="./doc/imgs/11.jpg" --det_model_dir="./inference/ch_ppocr_mobile_v2.0_det_infer/"  --rec_model_dir="./inference/ch_ppocr_mobile_v2.0_rec_infer/" --cls_model_dir="./inference/ch_ppocr_mobile_v2.0_cls_infer/" --use_angle_cls=True --use_space_char=True --use_gpu=False

在这里插入图片描述

报错 not find model.pdmodel or inference.pdmodel in ./inference/ch_ppocr_mobile_v2.0_det_infer/

四、下载权重文件

  • 权重链接地址
# 检测权重
https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_det_infer.tar# 方向分类权重
https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar# 识别权重
https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_rec_infer.tar
  • 创建一个 inference 文件夹,把前面解压后的三个文件夹放入 inference 中,
  • 再把 inference 文件夹放入 PaddleOCR 中,最终树形目录结构效果如下:

在这里插入图片描述

  • 再次检测,报错问题解决

在这里插入图片描述
在这里插入图片描述


总结

以上就是 Python 实现 PDF 文件转换为图片以及快速使用 PaddleOCR 过程。
http://www.yidumall.com/news/45834.html

相关文章:

  • 宿迁做网站的公司seo技术网网
  • 有没有好的网站是JSP做的关键词歌词简谱
  • 如何在网站上做标注seo是指
  • 保险网站建设windows优化软件哪个好
  • 动态网站背景做多少合适最吸引人的营销广告文案
  • 什么网站可以做直播商城全网推广运营公司
  • 漯河做网站zrgu网站推广公司大家好
  • 网站首页被k多久恢复公司业务推广
  • 企业公司网站深圳谷歌seo推广
  • 青海做网站需要多少钱seo是什么工作内容
  • 李连杰做的功夫网站建设网站前的市场分析
  • 萍乡企业做网站seo推广优化培训
  • 手机微网站制作系统北京seo网站开发
  • 做非法网站怎样量刑排名优化外包公司
  • wordpress文章分类统计seo系统
  • vs做的网站源代码哈尔滨seo关键字优化
  • 企业网站建设及维护企业培训平台
  • 做网站的流程是什么全国互联网营销大赛官网
  • 域名指向其他网站贵阳seo网站推广
  • 西安专业做网站的的公司seo推广网站
  • wordpress标签拼音seo搜索引擎优化入门
  • 郑州彩票网站开发百度框架户一级代理商
  • 个人游戏开发者 死路一条seo外链发布
  • 建设个人网上银行登录入口官网跟我学seo
  • 泉州网站建设报价腾讯控股第三季度营收1401亿
  • 佛山网站建设联系电话电脑培训学校学费多少
  • 2k屏幕的网站怎么做谷歌seo搜索引擎下载
  • 建设安全协会网站潍坊seo计费
  • 立邦漆官方网站官网太原网站快速排名优化
  • 网站图怎么做四川seo推广方案