我的程序使用 Tesseract OCR,我将使用 pyinstaller 将其转换为单个 .exe 文件。问题是,为了让 Tesseract 工作,我需要引用我的计算机上安装的程序的路径,如下所示:pytesseract.pytesseract.tesseract_cmd = 'E:\\Tesseract-OCR\\tesseract'
由于这不仅仅是一个可以导入的单独库,而且是一个独立程序,因此我无法将其作为“--add_data”参数传递给 pyinstaller。那么如何制作一个单文件可执行文件呢?
我有一个简单的代码,其中有一个名为“try.png”的图像,我想使用 pytesseract 将其从图像转换为文本,但我的代码遇到了一些问题。
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd=r'tesseract-ocr-setup-4.00.00dev.exe'
img = cv2.imread('try.png')
img= cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
print(pytesseract.image_to_string(img))
Run Code Online (Sandbox Code Playgroud)
但这给了我一个错误。
Traceback (most recent call last):
File "C:/Users/user 1/PycharmProjects/JARVIS/try.py", line 6, in <module>
print(pytesseract.image_to_string(img))
File "C:\Users\user 1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pytesseract\pytesseract.py", line 356, in image_to_string
return {
File "C:\Users\user 1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pytesseract\pytesseract.py", line 359, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "C:\Users\user 1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pytesseract\pytesseract.py", line 270, in run_and_get_output
run_tesseract(**kwargs)
File "C:\Users\user 1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pytesseract\pytesseract.py", line 241, in run_tesseract
raise e
File "C:\Users\user 1\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pytesseract\pytesseract.py", line 238, in run_tesseract
proc = subprocess.Popen(cmd_args, **subprocess_args())
File "C:\Users\user 1\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Google Colab 中运行 Tesseract:
!sudo apt install tesseract-ocr
!pip install pytesseract
import pytesseract
import shutil
import os
import random
try:
from PIL import Image
except ImportError:
import Image
from google.colab import files
uploaded = files.upload()
extractedInformation = pytesseract.image_to_string(Image.open('aaa.png'))
print(extractedInformation)
Run Code Online (Sandbox Code Playgroud)
我尝试在我正在上传的图像('aaa.png')上运行,但它运行此错误:
TesseractError: (2, 'Usage: pytesseract [-l lang] input_file')
Run Code Online (Sandbox Code Playgroud)
从几乎相同(仍未解决)的帖子中搜索,我尝试了以下代码,但仍然无法正常工作:
pytesseract.pytesseract.tesseract_cmd = (
r'/usr/local/bin/tesseract'
)
Run Code Online (Sandbox Code Playgroud)
我尝试访问 pytesseract 文件夹,但运行此错误:
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.6/dist-packages/pytesseract/pytesseract.py'
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Tesseract-OCR 来读取下面图像的读数,但在获得与发现的背景一致的结果时遇到问题。我的 pytesseract 上有以下配置
\nCONFIG = f"\xe2\x80\x94psm 6 -c tessedit_char_whitelist=01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ\xc3\x85\xc3\x84abcdefghijklmnopqrstuvwxyz\xc3\xa5\xc3\xa4\xc3\xb6.,-"
我也尝试过下面的图像预处理,取得了一些不错的结果,但仍然不是完美的结果
\nblur = cv2.blur(img,(4,4))\n(T, threshInv) = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)\nRun Code Online (Sandbox Code Playgroud)\n我想要的是始终能够识别数字和小数点分隔符。什么样的图像预处理可以帮助获得一致的图像结果,如下所示?
\n\n数字示例

我正在使用标准 pytesseract img 来发送文本。我尝试过仅使用数字选项,90% 的情况下它都是完美的,但上面是一个出现严重错误的示例!这个例子根本没有产生任何字符
正如您所看到的,现在有字母,因此语言选项没有用,我确实尝试在抓取的图像中添加一些文本,但它仍然出错。
我使用 CV2 增加了对比度,捕获的上游文本已经模糊
关于提高准确性有什么想法吗?
经过多次测试后使用以下建议。我发现锐度滤镜给出的结果不可靠。你可以使用的另一个工具是contrast=cv2.convertScaleAbs(img2,alpha=2.5,beta=-200)我用它作为我的黑白文本最终在灰色背景上使用convertScaleAbs得到浅灰色文本我能够增加对比度以获得几乎黑白图像
OCR 的基本步骤
使用pytesseract我获得了非常好的结果,但是它不能保留双倍空格,它们对我来说真的很重要。而且,所以我决定检索hocr输出而不是纯文本。但是;似乎没有任何方法可以使用pytessearct指定配置文件。
因此,是否可以使用pytesseract指定配置文件,或者是否可以更改一些默认配置文件以获取hocr输出?
#run method from pytessearct.py
def run_tesseract(input_filename, output_filename_base, lang=None, boxes=False, config=None):
'''
runs the command:
`tesseract_cmd` `input_filename` `output_filename_base`
returns the exit status of tesseract, as well as tesseract's stderr output
'''
command = [tesseract_cmd, input_filename, output_filename_base]
if lang is not None:
command += ['-l', lang]
if boxes:
command += ['batch.nochop', 'makebox']
if config:
command += shlex.split(config)
#command+=['C:\\Program Files (x86)\\Tesseract-OCR\\tessdata\\configs\\hocr']
#print "command:",command
proc = subprocess.Popen(command,
stderr=subprocess.PIPE)
return (proc.wait(), proc.stderr.read())
Run Code Online (Sandbox Code Playgroud) 我正在尝试在Mac上运行以下代码。
import Image
enter code here`import pytesseract
im = Image.open('test.png')
print pytesseract.image_to_string(im)
Run Code Online (Sandbox Code Playgroud)
从这里提出以下问题:pytesseract-没有此类文件或目录错误, 我需要安装tesseract-ocr
但是,当我尝试点安装tesseract-ocr时,出现以下错误:
creating build/temp.macosx-10.5-x86_64-2.7
gcc -fno-strict-aliasing -I//anaconda/include -arch x86_64 -DNDEBUG -g
-fwrapv -O3 -Wall -Wstrict-prototypes -I//anaconda/include/python2.7 -c
tesseract_ocr.cpp -o build/temp.macosx-10.5-x86_64-2.7/tesseract_ocr.o
tesseract_ocr.cpp:264:10:
fatal error: 'leptonica/allheaders.h' file not found #include "leptonica/allheaders.h"
^
1 error generated.
error: command 'gcc' failed with exit status 1
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么办。
我想识别NumberPlate的字符。如何在Ubuntu 16.04中为相应的号码牌训练tesseract-ocr。由于我不熟悉培训。请帮助我创建一个“ traineddata”文件以识别车牌。


我有1000张车牌图像。
请调查一下。任何帮助将不胜感激。
所以我尝试了以下命令
tesseract [langname].[fontname].[expN].[file-extension] [langname].[fontname].[expN] batch.nochop makebox
tesseract eng.arial.plate3655.png eng.arial.plate3655 batch.nochop makebox
Run Code Online (Sandbox Code Playgroud)
但这会带来错误。
Tesseract Open Source OCR Engine v4.1.0-rc1-56-g7fbd with Leptonica
Error, cannot read input file eng.arial.plate3655.png: No such file or directory
Error during processing.
Run Code Online (Sandbox Code Playgroud)
之后,我尝试了
tesseract plate4.png eng.arial.plate4 batch.nochop makebox
Run Code Online (Sandbox Code Playgroud)
它有效,但在某些板块中。现在在步骤2中。
屏幕截图已随附。
板4图像进行训练

步骤1和终端上的Ste p2显示

步骤1和步骤2之后生成的文件

步骤1和步骤2之后生成的文件内容

ocr tesseract python-tesseract openalpr automatic-license-plate-recognition
我使用代码来定位文本框并在它们周围创建一个矩形。这使我可以围绕图像中的表格结构重建网格。
但是,即使文本框检测效果很好,如果我尝试定义每个矩形中存在的字符,pytesseract 也无法很好地识别它们并且无法找到原始文本。
这是我的 Python 代码:
import os
import cv2
import imutils
import argparse
import numpy as np
import pytesseract
# This only works if there's only one table on a page
# Important parameters:
# - morph_size
# - min_text_height_limit
# - max_text_height_limit
# - cell_threshold
# - min_columns
def pre_process_image(img, save_in_file, morph_size=(8, 8)):
# get rid of the color
pre = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
def img_estim(img, threshold=127):
is_dark = np.mean(img) < threshold
return True if is_dark else False
# …Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码在图像文本上绘制矩形以匹配日期模式及其工作正常。
import re
import cv2
import pytesseract
from PIL import Image
from pytesseract import Output
img = cv2.imread('invoice-sample.jpg')
d = pytesseract.image_to_data(img, output_type=Output.DICT)
keys = list(d.keys())
date_pattern = '^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/(19|20)\d\d$'
n_boxes = len(d['text'])
for i in range(n_boxes):
if int(d['conf'][i]) > 60:
if re.match(date_pattern, d['text'][i]):
(x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('img', img)
cv2.waitKey(0)
img.save("sample.pdf")
Run Code Online (Sandbox Code Playgroud)
现在,最后我得到了一个带有匹配日期模式的矩形的 PDF。
我想将此程序扫描的 PDF 作为输入而不是上面的图像。它应该首先将 PDF 转换为 opencv 可读的图像格式,以进行与上述相同的处理。请帮忙。(任何解决方法都可以。我需要一个解决方案,我可以在其中将 PDF …
python-tesseract ×10
python ×8
tesseract ×7
ocr ×4
opencv ×3
automatic-license-plate-recognition ×1
detection ×1
hocr ×1
image ×1
openalpr ×1
pyinstaller ×1
pytesser ×1
python-3.x ×1