9

我正在使用pytesseractlib 从图像中提取文本。当我在本地主机上运行代码时,这工作正常。但是当我在 openshift 上部署时给了我以上错误。

下面是我到目前为止写的代码。

try:
  import Image
except ImportError:
  from PIL import Image
import pytesseract
filePath = PATH_WHERE_FILE_IS_LOCATED # '/var/lib/openshift/555.../app-root/data/data/y.jpg'
text = pytesseract.image_to_string(Image.open(filePath))  # this line produces error

上述错误的追溯是

>>> pytesseract.image_to_string(Image.open(filePath))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/var/lib/openshift/56faaee42d527151d5000089/app-  root/runtime/repo/pytesseract/pytesseract.py", line 132, in  image_to_string
boxes=boxes)
File "/var/lib/openshift/56faaee42d527151d5000089/app-root/runtime/repo/pytesseract/pytesseract.py", line 73, in run_tesseract
stderr=subprocess.PIPE)
File "/opt/rh/python27/root/usr/lib64/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/opt/rh/python27/root/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

Image.open(filePath)返回对象引用

 <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1366x768 at 0x7FC5A9F719D0>

如何消除此错误?提前致谢!!

4

6 回答 6

4

试试这段代码,检查错误在哪里:

try:
  import Image
  print("image not from PIL")
except ImportError:
  print("image from PIL")
  from PIL import Image
import pytesseract
import os
filePath = PATH_WHERE_FILE_IS_LOCATED # '/var/lib/openshift/555.../app-root/data/data/y.jpg'
if not os.path.exist(filePath):
    print("no image file")
I=None
try:
    I=Image.open(filePath)
except Exception as e:
    raise RuntimeError(" Can't open image because %s"% e)
text = pytesseract.image_to_string(I)  # this line produces error

PS:您可以像这样打印模块版本:

print Image.__version__
于 2016-04-22T04:40:00.673 回答
4

如此处所述安装tesseract-ocr

您可以通过rhc ssh运行命令。可以在此处找到更多特定于 Windows 的详细信息。

于 2016-04-20T08:36:53.677 回答
4

要么您没有在“openshift”上安装 tesseract-ocr,要么它不在您的 PATH 中。请参阅https://pypi.python.org/pypi/pytesseract/0.1 检查您是否可以从命令行执行 tesseract 命令。

于 2016-04-19T17:09:21.397 回答
4

恕我直言,如果我理解 openshift,它可能像 Heroku,其中文件系统是易失性的,路径必须来自略有不同或完全不同,所以,首先检查:

  1. 路径与本地开发环境中的路径相同
  2. 路径存在
  3. 您有足够的权限访问路径中的文件
  4. 请特别检查 openshift 文档,文件系统

我希望我有帮助

于 2016-04-21T13:13:43.210 回答
3

我想你可能没有输入正确的图像路径。你应该检查你的路径。

您是否还验证了 tesseract-ocr 的安装?您应该看到,当您使用 import 函数调用模块并从命令行实用程序检查它时,不会产生任何错误。

正如 Wuelfhis Asuaje 所说,您应该确保您有足够的权限来访问路径中的文件。

于 2016-04-23T07:05:38.413 回答
2

您应该从http://code.google.com/p/tesseract-ocr/安装 google tesseract-ocr 。

确保该tesseract命令在服务器上可用。

在后台,使用(https://github.com/madmaze/pytesseract/blob/master/src/pytesseract.py#L93pytesseract调用tesseract命令:subprocess

proc = subprocess.Popen(command,
            stderr=subprocess.PIPE)

现在猜猜如果命令不可用会发生什么?

In [45]: subprocess.Popen(['tesseract'])
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-45-f4e9dd5a7f0b> in <module>()
----> 1 subprocess.Popen(['tesseract'])

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
    708                                 p2cread, p2cwrite,
    709                                 c2pread, c2pwrite,
--> 710                                 errread, errwrite)
    711         except Exception:
    712             # Preserve original exception in case os.close raises.

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, to_close, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
   1333                         raise
   1334                 child_exception = pickle.loads(data)
-> 1335                 raise child_exception
   1336
   1337

OSError: [Errno 2] No such file or directory
于 2016-04-24T04:16:23.333 回答