Python:OSError:[Errno 2] 没有这样的文件或目录。

9

我正在使用pytesseract库从图像中提取文本。当我在本地主机上运行代码时,它可以正常工作。但是,当我部署到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>

如何解决这个错误?谢谢!

应该使用 fpath 还是 filePath?你的代码片段中设置了 filePath 并使用它,而你的回溯显示在 fpath 上调用了 Image.open - user5219763
抱歉,我在测试所以在终端中写了fpath :( - Suraj Palwe
没有。错误没有解决。 - Suraj Palwe
本地主机意味着Windows吗?这可能与大小写有关吗? - wenzul
@wenzul localhost是我的电脑,上面运行着Ubuntu 15.04! - Suraj Palwe
显示剩余10条评论
6个回答

4

4

4
在我看来,如果我理解得正确的话,OpenShift可能类似于Heroku,其中文件系统是易失性的,并且路径必须略有不同或完全不同,因此,请首先检查以下内容:
  1. 路径与本地开发环境中的路径相同
  2. 路径存在
  3. 您有足够的权限访问路径中的文件
  4. 请查看OpenShift文档,特别是文件系统
希望对您有所帮助。

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

注意: 您可以使用以下方法打印模块的版本:

print Image.__version__

您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Suraj Palwe

3

我认为您可能没有输入正确的图像路径。您应该检查好您的路径。

此外,您是否确认已安装tesseract-ocr? 您应该使用导入函数调用模块并通过命令行工具检查它时看到不产生错误。

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


2
您需要从http://code.google.com/p/tesseract-ocr/安装google tesseract-ocr。

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

pytesseract在底层使用subprocesshttps://github.com/madmaze/pytesseract/blob/master/src/pytesseract.py#L93)调用tesseract命令:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接