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

温州网站优化搜索wordpress 比价

温州网站优化搜索,wordpress 比价,郑州网站优化外包顾问,郴州房产网目录 0 环境准备 1 安装tesseract-ocr引擎 1.1 下载安装包 1.2 安装tesseract-ocr 1.2.1 选择语言 1.2.2 开始安装 1.2.3 同意安装协议 1.2.4 选择安装用户 1.2.5 选择安装组件 1.2.6 选择安装路径 1.2.7 系统安装tesseract-ocr 1.2.8 完成安装 1.2.9 结束安装 1…

目录

0 环境准备

1 安装tesseract-ocr引擎

1.1 下载安装包

1.2 安装tesseract-ocr

1.2.1 选择语言

1.2.2 开始安装

1.2.3 同意安装协议

1.2.4 选择安装用户

1.2.5 选择安装组件

1.2.6 选择安装路径

1.2.7 系统安装tesseract-ocr

1.2.8 完成安装

1.2.9 结束安装

1.3 设置环境变量

1.4 验证是否安装成功

1.5 安装简体中文语言包

1.5.1 下载简体中文语言包

1.5.2 放置简体中文语言包

1.5.3 查看简体中文语言包是否安装成功

2 创建项目python环境

2.1 conda创建python环境

2.2 在pycharm中创建项目

2.3 激活python环境

2.4 安装项目依赖包

3 程序逻辑实现

3.1 导入依赖包

3.2 定义PDFOCR类

3.3 定义初始化方法

3.4 定义pdf转换成照片方法

3.5 定义OCR识别照片方法

3.6 定义清楚缓存资源方法

3.7 实现main方法

4 测试验证

5 完整代码

附录

附录一:项目结构

附录二: 可能存在的问题

问题一:找不到tesseract路径


0 环境准备

  • 已安装miniconda环境
  • 具备科学上网条件

1 安装tesseract-ocr引擎

        Python读取扫描版PDF文件的详细解决方案,结合OCR技术和PyMuPDF库实现,代码需兼容中英文识别。OCR技术需要安装tesseract-ocr引擎,本章节将介绍在windows环境下如何安装tesseract-ocr引擎。

1.1 下载安装包

        windows环境访问以下地址下载tesseract-ocr安装包,需要科学上网。

windows下载地址:Home · UB-Mannheim/tesseract Wiki · GitHub

1.2 安装tesseract-ocr

1.2.1 选择语言

        选择英语,点击ok。

1.2.2 开始安装

        点击next开始安装。

1.2.3 同意安装协议

        点击I Agree。

1.2.4 选择安装用户

        选择Install for anyone using this compoter。点击next。

1.2.5 选择安装组件

        这里直接点击next

1.2.6 选择安装路径

        注意选择不要带有空格、中文、特殊字符的安装路径。防止使用过程出现莫名其妙的问题。安装路径选择完成后点击next。

1.2.7 系统安装tesseract-ocr

        这里直接点击install。

1.2.8 完成安装

      这里直接点击next。

1.2.9 结束安装

        这里直接点击finish。

1.3 设置环境变量

        在系统环境变量Path中添加tesseract-ocr目录

1.4 验证是否安装成功

        在命令行中输入tesseract -v 查看是否能正常看到tesseract的版本信息,如下图

tesseract -v

1.5 安装简体中文语言包

        Tesseract 默认不支持中文,若要识别中文,需要下载相应的语言包。

1.5.1 下载简体中文语言包

可以从以下地址下载简体中文语言包:

        官网下载:Traineddata Files for Version 4.00 + | tessdoc ,找到 Chinese - Simplified 对应的语言包下载。需要科学上网。

1.5.2 放置简体中文语言包

        将下载的chi_sim.traineddata文件放置到Tesseract-OCR的数据目录,本机是D:\setup\Tesseract-OCR\tessdata。请修改成自己的安装地址。

1.5.3 查看简体中文语言包是否安装成功

        输入查询支持语言命令,可以看到安装的chi_sim语言,如下图:

tesseract --list-langs

2 创建项目python环境

2.1 conda创建python环境

conda create -n pdf_ocr_demo python=3.10

2.2 在pycharm中创建项目

  1. 解释器类型:选择自定义环境
  2. 环境:选择现有
  3. 类型:选择conda
  4. 环境:选择上一步创建的pdf_ocr_demo环境

2.3 激活python环境

conda activate pdf_ocr_demo

2.4 安装项目依赖包

pip install PyMuPDF pytesseract pillow

3 程序逻辑实现

3.1 导入依赖包

import osimport fitz
import pytesseractfrom PIL import Image

3.2 定义PDFOCR类

class PDFOCR:

3.3 定义初始化方法

    def __init__(self, file_path, output_txt):self.file_path = file_pathself.output_txt = output_txtself.temp_img_dir = 'temp_imgs'os.makedirs(self.temp_img_dir, exist_ok=True)

3.4 定义pdf转换成照片方法

    def _pdf_to_images(self, zoom=3):"""将PDF每页转换为高清图片"""doc = fitz.open(self.file_path)for page_num in range(len(doc)):page = doc.load_page(page_num)# 设置缩放参数提升分辨率mat = fitz.Matrix(zoom, zoom)pix = page.get_pixmap(matrix=mat, alpha=False)p_index = page_num + 1p_index = str(p_index).zfill(5)img_path = os.path.join(self.temp_img_dir, f'page_{p_index}.png')pix.save(img_path)print(f"已完成 {img_path} 存储")doc.close()

3.5 定义OCR识别照片方法

    def _ocr_images(self):"""对转换后的图片进行OCR识别"""with open(self.output_txt, 'w', encoding='utf-8') as f:for img_file in sorted(os.listdir(self.temp_img_dir)):img_path = os.path.join(self.temp_img_dir, img_file)# 图像预处理img = Image.open(img_path).convert('L') # 转为灰度图# OCR识别text = pytesseract.image_to_string(img, lang='chi_sim+eng') # 简体中文f.write(text + '\n')print(f"已完成 {img_file} 识别")

3.6 定义清楚缓存资源方法

    def _cleanup(self):"""清理临时文件"""for img_file in os.listdir(self.temp_img_dir):os.remove(os.path.join(self.temp_img_dir, img_file))os.rmdir(self.temp_img_dir)print("清理完成")

3.7 实现main方法

if __name__ == '__main__':processor = PDFOCR('./in_pdf/617336.pdf', './out/output.txt')try:processor._pdf_to_images(zoom=3)processor._ocr_images()finally:processor._cleanup()

4 测试验证

        找一个扫描版PDF文件,放入in_pdf文件夹,修改main方法中pdf文件名,运行pdf_ocr_reader.py,可以看到执行结果如下图:

5 完整代码

import osimport fitz
import pytesseractfrom PIL import Imageclass PDFOCR:def __init__(self, file_path, output_txt):self.file_path = file_pathself.output_txt = output_txtself.temp_img_dir = 'temp_imgs'os.makedirs(self.temp_img_dir, exist_ok=True)def _pdf_to_images(self, zoom=3):"""将PDF每页转换为高清图片"""doc = fitz.open(self.file_path)for page_num in range(len(doc)):page = doc.load_page(page_num)# 设置缩放参数提升分辨率mat = fitz.Matrix(zoom, zoom)pix = page.get_pixmap(matrix=mat, alpha=False)p_index = page_num + 1p_index = str(p_index).zfill(5)img_path = os.path.join(self.temp_img_dir, f'page_{p_index}.png')pix.save(img_path)print(f"已完成 {img_path} 存储")doc.close()def _ocr_images(self):"""对转换后的图片进行OCR识别"""with open(self.output_txt, 'w', encoding='utf-8') as f:for img_file in sorted(os.listdir(self.temp_img_dir)):img_path = os.path.join(self.temp_img_dir, img_file)# 图像预处理img = Image.open(img_path).convert('L') # 转为灰度图# OCR识别text = pytesseract.image_to_string(img, lang='chi_sim+eng') # 简体中文f.write(text + '\n')print(f"已完成 {img_file} 识别")def _cleanup(self):"""清理临时文件"""for img_file in os.listdir(self.temp_img_dir):os.remove(os.path.join(self.temp_img_dir, img_file))os.rmdir(self.temp_img_dir)print("清理完成")if __name__ == '__main__':processor = PDFOCR('./in_pdf/617336.pdf', './out/output.txt')try:processor._pdf_to_images(zoom=3)processor._ocr_images()finally:processor._cleanup()

附录

附录一:项目结构

附录二: 可能存在的问题

问题一:找不到tesseract路径

pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.

解决办法:

        请确认环境变量Path中是否配置tesseract的路径,参考章节1.3。如果确认已配置,则重启pycharm,重新运行程序。


文章转载自:

http://alr8zP0c.tpxgm.cn
http://6DeE9Kyv.tpxgm.cn
http://uce18RKs.tpxgm.cn
http://641Mx7ZQ.tpxgm.cn
http://irLAN3Op.tpxgm.cn
http://DHlKv9uw.tpxgm.cn
http://o57100YC.tpxgm.cn
http://uL0XqNYi.tpxgm.cn
http://jUSJVUCN.tpxgm.cn
http://ClRBazhb.tpxgm.cn
http://urbDJBIZ.tpxgm.cn
http://AX6M6ZMt.tpxgm.cn
http://UAy7RUbA.tpxgm.cn
http://gpbfcBhV.tpxgm.cn
http://RTwsB56p.tpxgm.cn
http://J5ZwI02T.tpxgm.cn
http://3Tf1o8ql.tpxgm.cn
http://J2IdxMab.tpxgm.cn
http://TeLN3qtL.tpxgm.cn
http://pVaMBIcr.tpxgm.cn
http://MvNwQkNd.tpxgm.cn
http://ZO3H6XwK.tpxgm.cn
http://qsJfIPuE.tpxgm.cn
http://4UvfOAYy.tpxgm.cn
http://fHSa5o7K.tpxgm.cn
http://5e45v62p.tpxgm.cn
http://saixLI3Q.tpxgm.cn
http://wN1weS0i.tpxgm.cn
http://CRR4JcWx.tpxgm.cn
http://0OSQGANa.tpxgm.cn
http://www.dtcms.com/wzjs/680616.html

相关文章:

  • 菏泽北京网站建设电子商务网站功能设计
  • vue做网站的实例中山医疗网站建设
  • 免费空间赞郑州网站制作专业乐云seo
  • 佛山市网站建设分站企业网页设计制作网站教程
  • 中国建设监理协会网站投稿百度对wordpress
  • 做游戏网站多钱湖北标书设计制作
  • 中国建设银行官网站企业年金做详情页比较好的网站
  • wordpress访问网站很慢做一个论坛网站要多少钱
  • 创建qq网站网站开发工程师任职资格
  • 网站开发英文翻译zencart 网站入侵
  • 购物网站开发的基本介绍开发公司空置房物业费会计科目
  • 专业的定制型网站建设更改网站伪静态
  • 建筑做地图分析的网站国外订房网站怎么和做
  • 做平面设计必知的网站网站设计有哪些公司
  • 做的网站要花钱吗广东新闻联播2011
  • 做网站维护工作难吗整站排名服务
  • 网站可以做的活动推广域名空间做网站
  • 网站如何做实名验证企业网站建设方案渠道
  • 阿里云做网站需要些什么上海营销网站建设
  • 搜索网站建设推广优化做行业分析的网站
  • 网站改版思路北京海淀的保险公司
  • 前端手机网站wordpress 函数教程
  • 网站域名证书软路由做网站
  • 做p2p网站的公司织梦dedecms网站简略标题shorttitle的使用方法
  • 长白山网站学做管理平台wordpress主题 评论
  • 中国旅游网站排名高清图片素材网站免费下载
  • 网站的请求服务做优先级网站中图片加水印
  • 一般网站字体大小高端模版网站
  • 网站通知发送邮件推广策略的概念
  • 东莞公司企业设计网站建设黄冈网站建设效果