目录
前言
在之前的篇章出现了,不支持ocr,或者模型还不支持最新版的Langchain框架,所以我找了个新的开源ocr识别模型,仅支持图片识别,结合Markitdown基本能完成对基本文件的识别操作。
然后本篇章主要是对ocr做统筹优化,进行文件的区分,实现对文件上传的识别和操作。然后实现上下文对话。
pytesseract-OCR
这个专门用于图片识别的轻巧ocr工具,目前我个人测试的,准确率应该是有95%左右的
Tesseract OCR 引擎(底层识别工具)和 pytesseract(Python 调用接口)
ocr引擎网址:https://github.com/UB-Mannheim/tesseract/wiki
关于新版的缺少add to path和中文语言环境包
找到系统变量新建添加文件夹路径就好了
win+r然后输入该指令
sysdm.cpl
然后找到高级,点击环境变量
在环境变量添加就ok了
安装依赖
# 安装 pytesseract(Python调用接口)和 pillow(图片处理)
pip install pytesseract pillow
参考代码
import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = r'D:\workfile\Tesseract-ocr\tesseract.exe'
#这里指的是应用程序所在的文件路径
image_path = r'D:\pychram\test_ocr.png'
#这里是图片所在的路径
img = Image.open(image_path)
text = pytesseract.image_to_string(img, lang='chi_sim+eng')
print(text)
需要识别的原图

控制台输出

整合ocr创建工具函数模块
之前的参考函数
def chat_with_markdown():
md = MarkItDown()
file_path = "C:\\Users\\a\Desktop\\AgentTestworddemo.txt"
res = md.convert(file_path)
text = res.text_content
# 修改此处:对系统提示中的大括号进行转义
prompt = ChatPromptTemplate.from_messages(
[
("system",
f"用户上传文件的内容{text}\n\n你是一个问答助手,用户和你进行对话,你先基于用户上传文件的内容查询回答,如果没有需要根据用户问题给出一个回答。".replace(
"{", "{{").replace("}", "}}")),
("human", "{question}"),
]
)
chat = prompt | llm
question = input("请输入问题:")
response = chat.invoke({"question": question})
print(response.content)
现在要统一拆开,分类和识别,为之后的接口调用做好准备
关于文件识别还是统一使用markitdown模块,因为除了不支持文件,基本上都完成了识别
然后就是利用上述的ocr进行一个识别图片,关于pdf文件里面含有图片再讨论,得要实现基本的文件流程,后续专精的事后续再考虑,因为到时候可能要采用多模态的模型来使用,方便快捷识别,而不是采用basex64来写,虽然这个也行,但是实现有点麻烦,我这里还没找到相关的内容。
可以的话自行降低langchain版本,使用paddle-ocr实现图片,pdf多文件内容识别,我们这里就采用小巧型的方便写法。
普通文件识别函数
def text_file_ocr(file_path):
try:
md = MarkItDown()
res = md.convert(file_path)
text = res.text_content
return text
except Exception as e:
print(f"Error processing file: {e}")
return None
图片文件识别函数
def image_file_ocr(file_path):
try:
pytesseract.pytesseract.tesseract_cmd = os.getenv("TESSERACT_CMD")
img = Image.open(file_path)
text = pytesseract.image_to_string(img, lang='chi_sim+eng')
return text
except Exception as e:
print(f"Error processing image: {e}")
return None
对于文件后缀名作为识别来进行函数分类调用函数
这里使用
file_path.endswith
来进行后缀名识别
代码
def ocr_file(file_path):
"""
此函数是用来做文件分类,ocr识别操作的
:param file_path:
:return:
"""
if file_path.endswith((".txt", ".text", ".docx", ".doc", ".pdf", ".md")):
return text_file_ocr(file_path)
elif file_path.endswith((".png", ".jpg", ".jpeg")):
return image_file_ocr(file_path)
else:
return None
示例代码参考
import pytesseract
from PIL import Image
import warnings
from dotenv import load_dotenv
from markitdown import MarkItDown
import os
load_dotenv()
warnings.filterwarnings("ignore", message="Couldn't find ffmpeg or avconv*")
def text_file_ocr(file_path):
try:
md = MarkItDown()
res = md.convert(file_path)
text = res.text_content
return text
except Exception as e:
print(f"Error processing file: {e}")
return None
def image_file_ocr(file_path):
try:
pytesseract.pytesseract.tesseract_cmd = os.getenv("TESSERACT_CMD")
img = Image.open(file_path)
text = pytesseract.image_to_string(img, lang='chi_sim+eng')
return text
except Exception as e:
print(f"Error processing image: {e}")
return None
def ocr_file(file_path):
"""
此函数是用来做文件分类,ocr识别操作的
:param file_path:
:return:
"""
if file_path.endswith((".txt", ".text", ".docx", ".doc", ".pdf", ".md")):
return text_file_ocr(file_path)
elif file_path.endswith((".png", ".jpg", ".jpeg")):
return image_file_ocr(file_path)
else:
return None
结束语
关于这里就没什么特别的代码演示,接下来的篇章将会学习使用fastapi创建对话接口,关于这里,我想最后说明,我的代码仅供参考,不一定正确,可以的话如果有错和问题可以私信我,咱们一起讨论和学习。
2771

被折叠的 条评论
为什么被折叠?



