python - cv2 直接进行 tesseract 不保存
问题描述
import pytesseract
from pdf2image import convert_from_path, convert_from_bytes
import cv2,numpy
def pil_to_cv2(image):
open_cv_image = numpy.array(image)
return open_cv_image[:, :, ::-1].copy()
path='OriginalsFile.pdf'
images = convert_from_path(path)
cv_h=[pil_to_cv2(i) for i in images]
img_header = cv_h[0][:160,:]
#print(pytesseract.image_to_string(Image.open('test.png'))) I only found this in tesseract docs
你好,有没有办法img_header
直接使用pytesseract读取而不保存,
解决方案
pytesseract.image_to_string() 输入格式
正如文档所解释pytesseract.image_to_string()
的,需要一个 PIL 图像作为输入。因此,您可以轻松地将您的 CV 图像转换为 PIL 图像,如下所示:
from PIL import Image
... (your code)
print(pytesseract.image_to_string(Image.fromarray(img_header)))
如果你真的不想使用 PIL!
见: https ://github.com/madmaze/pytesseract/blob/master/src/pytesseract.py
pytesseract 是运行 tesseract 命令def run_and_get_output()
行的简单包装器,您会看到它将图像保存到临时文件中,然后将地址提供给 tesseract 以运行。
因此,您可以对 opencv 执行相同的操作,只需重写 pytesseract only.py
文件以使用 opencv 执行此操作;我没有看到任何性能改进。
推荐阅读
- azure - Azure Batch REST API,授权问题
- llvm - LLVM:以前未定义的“无效重新定义函数”
- modelica - Dymola 在非线性系统初始化过程中如何选择迭代变量?
- oauth-2.0 - 是否可以拥有多个具有相同客户端凭据的有效访问令牌?
- ruby-on-rails - RSpec:关联对象的工厂定义
- android - 如何在语义上比较两个句子?
- java - Java:如何将两个变量设置为相同的值,然后更改一个而不更改另一个?
- vbscript - 以最大长度和特定字符剪切字符串
- r - 如果在两个变量之间,跨R中的数据框分配一个值
- java - 图上的 Floyd Warshall 算法