0

我正在尝试设置 OCR Web 服务,以便可以从多个位置发送图像进行处理。

我从来没有对 cgi 做过任何事情,所以我说是时候尝试 mod_wsgi 了。我花了 2 天时间来安装所有的库,以及 opencv 和 pytesseract。如果我以“正常方式”(启动新的 python 窗口解释器)进行操作,我的 OCR 工作得很好。我在让一些库与 mod_wsgi 一起工作时遇到了很多麻烦,即使它们工作正常。

我被困在 pytessearct 上。如果我运行它:

tesseract -l myl image.jpe out

一切正常文件。

即使我这样做:

import pytessearct
from PIL import Image

pytesseract.image_to_string(Image.open('/var/www/path/image.jpe'), lang='myl')

这也有效。

如果我使用 mod_wsgi 执行此操作,我的httpd 日志文件中会出现此错误:

mod_wsgi (pid=1836): Exception occurred processing WSGI script '/var/www/path/app.wsgi'.
[Mon May 18 06:28:31 2015] [error] [client IP] Traceback (most recent call last):
[Mon May 18 06:28:31 2015] [error] [client IP]   File "/var/www/path/app.wsgi", line 28, in wsgi_app
[Mon May 18 06:28:31 2015] [error] [client IP]     output = check_text('a.jpe')
[Mon May 18 06:28:31 2015] [error] [client IP]   File "/var/www/path/app.wsgi", line 20, in check_text
[Mon May 18 06:28:31 2015] [error] [client IP]     return pytesseract.image_to_string(Image.open('/var/www/path/a.jpe'), lang='myl')
[Mon May 18 06:28:31 2015] [error] [client IP]   File "/usr/local/lib/python2.7/site-packages/pytesseract/pytesseract.py", line 161, in image_to_string
[Mon May 18 06:28:31 2015] [error] [client IP]     boxes=boxes,
[Mon May 18 06:28:31 2015] [error] [client IP]   File "/usr/local/lib/python2.7/site-packages/pytesseract/pytesseract.py", line 94, in run_tesseract
[Mon May 18 06:28:31 2015] [error] [client IP]     stderr=subprocess.PIPE)
[Mon May 18 06:28:31 2015] [error] [client IP]   File "/usr/local/lib/python2.7/subprocess.py", line 710, in __init__
[Mon May 18 06:28:31 2015] [error] [client IP]     errread, errwrite)
[Mon May 18 06:28:31 2015] [error] [client IP]   File "/usr/local/lib/python2.7/subprocess.py", line 1335, in _execute_child
[Mon May 18 06:28:31 2015] [error] [client IP]     raise child_exception
[Mon May 18 06:28:31 2015] [error] [client IP] OSError: [Errno 2] No such file or directory

这是我的app.wsgi文件:

#!/usr/local/bin python2.7
#-*- coding: utf-8 -*-

import os
import sys
from subprocess import check_output



sys.path.append('/var/www/path')

import pytesseract
from PIL import Image

def check_text(image_path):
#   return check_output(['pytesseract', '-l', 'myl', '/var/www/path/a.jpe'])
        return pytesseract.image_to_string(Image.open('/var/www/path/a.jpe'), lang='myl')


def wsgi_app(environ, start_response):
        output = sys.version.encode('utf-8')
        status = '200 OK'
        headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))]
        output = check_text('a.jpe')
        start_response(status, headers)
        return os.getcwd()
        return output

# mod_wsgi need the *application* variable to serve our small app
application = wsgi_app

正如您在源代码中看到的那样,我也尝试使用 subprocess 的 check_output 来自己启动一个新的 pytesseract 进程,但我得到了同样的错误。

我已经从源代码构建了 tesseract 和 mod_wsgi。但同样,我确信它与 mod_wsgi 有关,因为如果我在 python 中正常执行它,它就可以工作。

更新:我对 mod_wsgi 和 opencv 有类似的“奇怪”问题。问题和答案可以在这里找到:Occasional ctypes error importing numpy from mod_wsgi django app

任何建议将不胜感激。

4

1 回答 1

1

为了解决它,我已更改/usr/local/lib/python2.7/site-packages/pytesseract/pytesseract.pytesseract_cmd = 'tesseract'tesseract_cmd = '/usr/local/bin/tesseract'.

于 2015-05-18T17:50:56.950 回答