0

我正在尝试应用以下代码:

import cv2
import numpy as np
import pytesseract
from PIL import Image

# Path of working folder on Disk
src_path = "C:/TEST/"

def get_string(img_path):
    # Read image with opencv
    img = cv2.imread(img_path)

    # Convert to gray
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Apply dilation and erosion to remove some noise
    kernel = np.ones((1, 1), np.uint8)
    img = cv2.dilate(img, kernel, iterations=1)
    img = cv2.erode(img, kernel, iterations=1)

    # Write image after removed noise
    cv2.imwrite(src_path + "removed_noise.png", img)

    #  Apply threshold to get image with only black and white
    #img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)

    # Write the image after apply opencv to do some ...
    cv2.imwrite(src_path + "thres.png", img)

    # Recognize text with tesseract for python
    result = pytesseract.image_to_string(Image.open(src_path + "thres.png"))

    # Remove template file
    #os.remove(temp)

    return result


print '--- Start recognize text from image ---'
print get_string(src_path + "textArea01.png")

但作为回报我得到

Traceback (most recent call last):
  File "C:/Python27/erw.py", line 40, in <module>
    print get_string(src_path + "textArea01.png")
  File "C:/Python27/erw.py", line 31, in get_string
    result = pytesseract.image_to_string(Image.open(src_path + "thres.png"))
  File "C:\Python27\lib\site-packages\pytesseract\pytesseract.py", line 122, in image_to_string
    config=config)
  File "C:\Python27\lib\site-packages\pytesseract\pytesseract.py", line 46, in run_tesseract
    proc = subprocess.Popen(command, stderr=subprocess.PIPE)
  File "C:\Python27\lib\subprocess.py", line 390, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
    startupinfo)

* 我已经尝试安装 tesseract-ocr

但它最终对我来说:

Command "c:\python27\python.exe -u -c "import setuptools, tokenize;__file__='c:\
\users\\xyz~1\\appdata\\local\\temp\\pip-build-bvk9mm\\tesseract-ocr\\setup.p
y';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n
');f.close();exec(compile(code, __file__, 'exec'))" install --record c:\users\df
asto~1\appdata\local\temp\pip-vcr3xw-record\install-record.txt --single-version-
externally-managed --compile" failed with error code 1 in c:\users\xyz~1\appd
ata\local\temp\pip-build-bvk9mm\tesseract-ocr\
#

当我尝试不同的代码时:

from PIL import Image
from pytesseract import image_to_string

im = image_to_string(Image.open("c:/Python36/Projekty/textArea01.png"))
print(im)

与上述相同的故事:

Traceback (most recent call last):
  File "C:\Python36\Projekty\OCR_v1.py", line 6, in <module>
    im = image_to_string(Image.open("c:/Python36/Projekty/textArea01.png"))
  File "C:\Python36\lib\site-packages\pytesseract\pytesseract.py", line 122, in image_to_string
    config=config)
  File "C:\Python36\lib\site-packages\pytesseract\pytesseract.py", line 46, in run_tesseract
    proc = subprocess.Popen(command, stderr=subprocess.PIPE)
  File "C:\Python36\lib\subprocess.py", line 707, in __init__
    restore_signals, start_new_session)
  File "C:\Python36\lib\subprocess.py", line 990, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2]

问题是——如何从上述问题中走出来 | 以及为什么我第一次看到这些问题

4

1 回答 1

0

我在安装 tesseract 时没有任何问题,但我在 UB Mannheim 安装程序中使用了 Tesseract:

https://github.com/UB-Mannheim/tesseract/wiki

您还需要安装 pytesseract:

pip3.6 安装 pytesseract

我使用以下代码没有问题:

import numpy as np
from numpy import *
from PIL import Image
from PIL import *
import pytesseract
import cv2


src_path = "C:\\Users\\USERNAME\\Documents\\OCR\\"


def get_string(img_path):
    # Read image with opencv
    img = cv2.imread(img_path)
    # Convert to gray
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Apply dilation and erosion to remove some noise
    kernel = np.ones((1, 1), np.uint8)
    img = cv2.dilate(img, kernel, iterations=1)
    img = cv2.erode(img, kernel, iterations=1)
    # Write image after removed noise
    cv2.imwrite(src_path + "removed_noise.png", img)
    #  Apply threshold to get image with only black and white
    #img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)
    # Write the image after apply opencv to do some ...
    cv2.imwrite(src_path + "thres.png", img)
    # Recognize text with tesseract for python

    result = pytesseract.image_to_string(Image.open(src_path + "thres.png"))

    return result

def main():
    #Output results
    print ("OCR Output: ")
    print (get_string(src_path + "test.png"))

创建一个扩展名为 .png 的图像文件,并确保修改代码以具有正确的路径和图像名称。

为确保 OCR 正常工作,我建议将文本(或图像中的文本)设为 16 号字体。我遇到了较小字体准确或根本不被阅读的问题。

于 2017-06-23T04:11:16.540 回答