Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中使用tesseract ocr获取结构Format中的信息?_Python_Opencv_Ocr - Fatal编程技术网

如何在Python中使用tesseract ocr获取结构Format中的信息?

如何在Python中使用tesseract ocr获取结构Format中的信息?,python,opencv,ocr,Python,Opencv,Ocr,我正在使用Ubuntu 这是我从网上得到的照片 我关心的是获取图像中格式化的数据 并将其转储到文本文件中的位置必须保持95-97%的准确性 我和你一起工作 我的代码-: import cv2 import pytesseract from pytesseract import Output import numpy as np img = cv2.imread("/demo.jpg") d1 = pytesseract.image_to_data(img) print(

我正在使用Ubuntu

这是我从网上得到的照片

我关心的是获取图像中格式化的数据

并将其转储到文本文件中的位置必须保持95-97%的准确性

我和你一起工作

我的代码-:

import cv2
import pytesseract
from pytesseract import Output
import numpy as np

img = cv2.imread("/demo.jpg")

d1 = pytesseract.image_to_data(img)

print(d1)

它给了我一个完全错误的输出,从我的期望


简而言之,我想将此对齐图像转换为文本文件或CSV文件。

您可以使用HOCR中的tesseract输出来保留位置信息。将这些类型的文档直接转换为保留位置信息的文本是一个非常棘手和困难的问题。我可以给你一个中间解决方案,它可以给你一个包含每个单词及其坐标的数据框,这样你就可以解析它,用坐标来提取键值信息

### this will save the tesseract output as "demo.hocr" 
pytesseract.pytesseract.run_tesseract(
            "demo.jpg", "demo",
            extension='.html', lang='eng', config="hocr")
HOCR是一种类似HTML的表示,包含大量元数据,如行信息、单词信息、坐标等。 为了更好地处理,我有一个解析器,它将直接解析它,并为您提供一个包含单词及其坐标的数据帧。 我已经在pip中创建了一个名为此的包。 您可以使用pip install tesseract2dict轻松安装它 这就是你可以使用它的方式

import cv2
from tesseract2dict import TessToDict
td=TessToDict()
inputImage=cv2.imread('path/to/image.jpg')
### function 1
### this is for getting word level information as a dataframe
word_dict=td.tess2dict(inputImage,'outputName','outfolder')

### function 2
### this is for getting plain text for a given coordinates as (x,y,w,h)
text_plain=td.word2text(word_dict,(0,0,inputImage.shape[1],inputImage.shape[0]))

PS:此软件包仅与Tesseract 5.0.0兼容

您可以在HOCR中使用Tesseract输出来保留位置信息。将这些类型的文档直接转换为保留位置信息的文本是一个非常棘手和困难的问题。我可以给你一个中间解决方案,它可以给你一个包含每个单词及其坐标的数据框,这样你就可以解析它,用坐标来提取键值信息

### this will save the tesseract output as "demo.hocr" 
pytesseract.pytesseract.run_tesseract(
            "demo.jpg", "demo",
            extension='.html', lang='eng', config="hocr")
HOCR是一种类似HTML的表示,包含大量元数据,如行信息、单词信息、坐标等。 为了更好地处理,我有一个解析器,它将直接解析它,并为您提供一个包含单词及其坐标的数据帧。 我已经在pip中创建了一个名为此的包。 您可以使用pip install tesseract2dict轻松安装它 这就是你可以使用它的方式

import cv2
from tesseract2dict import TessToDict
td=TessToDict()
inputImage=cv2.imread('path/to/image.jpg')
### function 1
### this is for getting word level information as a dataframe
word_dict=td.tess2dict(inputImage,'outputName','outfolder')

### function 2
### this is for getting plain text for a given coordinates as (x,y,w,h)
text_plain=td.word2text(word_dict,(0,0,inputImage.shape[1],inputImage.shape[0]))

PS:此软件包仅与Tesseract 5.0.0兼容

您可以利用Pyteseract参数实现所需的功能。 更具体地说,您导入的输出类包含pytesseract支持的所有输出类型

进口cv2 导入pytesseract 从PyteSeract导入输出 将numpy作为np导入 img=cv2.imread/demo.jpg 我最喜欢的类型是Output.DICT,但因为您提到了CSV d1=pytesseract.image\u to\u dataimg,output\u type=output.DATAFRAME 打印类型1 d1.至_csv'ocr_dump.csv'
您可以利用PyteSeract参数来实现所需的功能。 更具体地说,您导入的输出类包含pytesseract支持的所有输出类型

进口cv2 导入pytesseract 从PyteSeract导入输出 将numpy作为np导入 img=cv2.imread/demo.jpg 我最喜欢的类型是Output.DICT,但因为您提到了CSV d1=pytesseract.image\u to\u dataimg,output\u type=output.DATAFRAME 打印类型1 d1.至_csv'ocr_dump.csv'