解决“tesseract&tesserocr安装”问题

问题描述

学习Python爬虫首先要配置环境安装tesseract和tesserocr两个库,但是在Mac上按照书中的指导会出现无法解决的安装错误,经过一天时间的试错,总算把这两个库安装完毕可以使用。

 

解决方法

tesseract&tesserocr安装指南

1.安装tesseract

终端输入指令brew install tesseract

在安装全语言支持版本的时候,指令为brew install tesseract-lang

验证所支持的语言,指令为tesseract --list-langs

 

2.安装tesserocr

终端输入指令

采用指令:

pip3 install tesserocr pilow

 

安装过程及报错:

Collecting tesserocr

  Using cached https://files.pythonhosted.org/packages/92/2d/05a7f8387e93c192919b508e4f4936f232bd3d2ca388b9130ae538a9f9ad/tesserocr-2.4.0.tar.gz

Requirement already satisfied: pillow in /anaconda3/lib/python3.7/site-packages (5.3.0)

Building wheels for collected packages: tesserocr

  Building wheel for tesserocr (setup.py) ... error

  Complete output from command /anaconda3/bin/python3 -u -c "import setuptools, tokenize;__file__='/private/var/folders/tg/g57hkq3s3g163k6zstsmjfj00000gn/T/pip-install-m92cxqko/tesserocr/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /private/var/folders/tg/g57hkq3s3g163k6zstsmjfj00000gn/T/pip-wheel-zpv8cubn --python-tag cp37:

  Supporting tesseract v4.0.0

  Building with configs: {'libraries': ['tesseract', 'lept'], 'cython_compile_time_env': {'TESSERACT_VERSION': 67108864}}

  running bdist_wheel

  running build

  running build_ext

  building 'tesserocr' extension

  creating build

  creating build/temp.macosx-10.7-x86_64-3.7

  gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/anaconda3/include -arch x86_64 -I/anaconda3/include -arch x86_64 -I/anaconda3/include/python3.7m -c tesserocr.cpp -o build/temp.macosx-10.7-x86_64-3.7/tesserocr.o -std=c++11 -DUSE_STD_NAMESPACE

  In file included from tesserocr.cpp:656:

  In file included from /usr/local/include/tesseract/genericvector.h:28:

  In file included from /usr/local/include/tesseract/tesscallback.h:22:

  /usr/local/include/tesseract/host.h:30:10: fatal error: 'cinttypes' file not found

  #include   // PRId32, ...

           ^~~~~~~~~~~

  1 error generated.

  error: command 'gcc' failed with exit status 1

  

  ----------------------------------------

  Failed building wheel for tesserocr

  Running setup.py clean for tesserocr

Failed to build tesserocr

Installing collected packages: tesserocr

  Running setup.py install for tesserocr ... error

    Complete output from command /anaconda3/bin/python3 -u -c "import setuptools, tokenize;__file__='/private/var/folders/tg/g57hkq3s3g163k6zstsmjfj00000gn/T/pip-install-m92cxqko/tesserocr/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/var/folders/tg/g57hkq3s3g163k6zstsmjfj00000gn/T/pip-record-7m0n40sm/install-record.txt --single-version-externally-managed --compile:

    Supporting tesseract v4.0.0

    Building with configs: {'libraries': ['tesseract', 'lept'], 'cython_compile_time_env': {'TESSERACT_VERSION': 67108864}}

    running install

    running build

    running build_ext

    building 'tesserocr' extension

    creating build

    creating build/temp.macosx-10.7-x86_64-3.7

    gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/anaconda3/include -arch x86_64 -I/anaconda3/include -arch x86_64 -I/anaconda3/include/python3.7m -c tesserocr.cpp -o build/temp.macosx-10.7-x86_64-3.7/tesserocr.o -std=c++11 -DUSE_STD_NAMESPACE

    In file included from tesserocr.cpp:656:

    In file included from /usr/local/include/tesseract/genericvector.h:28:

    In file included from /usr/local/include/tesseract/tesscallback.h:22:

    /usr/local/include/tesseract/host.h:30:10: fatal error: 'cinttypes' file not found

    #include   // PRId32, ...

             ^~~~~~~~~~~

    1 error generated.

    error: command 'gcc' failed with exit status 1

    

    ----------------------------------------

Command "/anaconda3/bin/python3 -u -c "import setuptools, tokenize;__file__='/private/var/folders/tg/g57hkq3s3g163k6zstsmjfj00000gn/T/pip-install-m92cxqko/tesserocr/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/var/folders/tg/g57hkq3s3g163k6zstsmjfj00000gn/T/pip-record-7m0n40sm/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/tg/g57hkq3s3g163k6zstsmjfj00000gn/T/pip-install-m92cxqko/tesserocr/

 

尝试更换指令:

pip install tesserocr

 

报错同上

 

尝试更换指令:

pip install tesseract-ocr

 

安装过程及报错:

Collecting tesseract-ocr

  Downloading https://files.pythonhosted.org/packages/e2/0d/dcee3dd0fc4c7bcd18125a98f8ba6d9db7aecaa40770595203e312649587/tesseract-ocr-0.0.1.tar.gz

Requirement already satisfied: cython in /anaconda3/lib/python3.7/site-packages (from tesseract-ocr) (0.29.2)

Building wheels for collected packages: tesseract-ocr

  Building wheel for tesseract-ocr (setup.py) ... error

  Complete output from command /anaconda3/bin/python3 -u -c "import setuptools, tokenize;__file__='/private/var/folders/tg/g57hkq3s3g163k6zstsmjfj00000gn/T/pip-install-mrfmt2dz/tesseract-ocr/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /private/var/folders/tg/g57hkq3s3g163k6zstsmjfj00000gn/T/pip-wheel-cy55vq_r --python-tag cp37:

  running bdist_wheel

  running build

  running build_py

  file tesseract_ocr.py (for module tesseract_ocr) not found

  file tesseract_ocr.py (for module tesseract_ocr) not found

  running build_ext

  building 'tesseract_ocr' extension

  creating build

  creating build/temp.macosx-10.7-x86_64-3.7

  gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/anaconda3/include -arch x86_64 -I/anaconda3/include -arch x86_64 -I/anaconda3/include/python3.7m -c tesseract_ocr.cpp -o build/temp.macosx-10.7-x86_64-3.7/tesseract_ocr.o

  In file included from tesseract_ocr.cpp:651:

  In file included from /usr/local/include/tesseract/baseapi.h:27:

  In file included from /usr/local/include/tesseract/apitypes.h:23:

  /usr/local/include/tesseract/publictypes.h:33:1: error: unknown type name 'constexpr'

  constexpr int kPointsPerInch = 72;

  ^

  /usr/local/include/tesseract/publictypes.h:33:11: error: expected unqualified-id

  constexpr int kPointsPerInch = 72;

            ^

  /usr/local/include/tesseract/publictypes.h:38:1: error: unknown type name 'constexpr'

  constexpr int kMinCredibleResolution = 70;

  ^

  /usr/local/include/tesseract/publictypes.h:38:11: error: expected unqualified-id

  constexpr int kMinCredibleResolution = 70;

            ^

  /usr/local/include/tesseract/publictypes.h:40:1: error: unknown type name 'constexpr'

  constexpr int kMaxCredibleResolution = 2400;

  ^

  /usr/local/include/tesseract/publictypes.h:40:11: error: expected unqualified-id

  constexpr int kMaxCredibleResolution = 2400;

            ^

  /usr/local/include/tesseract/publictypes.h:45:1: error: unknown type name 'constexpr'

  constexpr int kResolutionEstimationFactor = 10;

  ^

  /usr/local/include/tesseract/publictypes.h:45:11: error: expected unqualified-id

  constexpr int kResolutionEstimationFactor = 10;

            ^

  In file included from tesseract_ocr.cpp:651:

  In file included from /usr/local/include/tesseract/baseapi.h:31:

  /usr/local/include/tesseract/resultiterator.h:103:63: error: a space is required between consecutive right angle brackets (use '> >')

    virtual std::vector>>* GetBestLSTMSymbolChoices() const;

                                                                ^

  In file included from tesseract_ocr.cpp:651:

  In file included from /usr/local/include/tesseract/baseapi.h:32:

  In file included from /usr/local/include/tesseract/serialis.h:26:

  /usr/local/include/tesseract/host.h:30:10: fatal error: 'cinttypes' file not found

  #include   // PRId32, ...

           ^~~~~~~~~~~

  10 errors generated.

  error: command 'gcc' failed with exit status 1

  

  ----------------------------------------

  Failed building wheel for tesseract-ocr

  Running setup.py clean for tesseract-ocr

Failed to build tesseract-ocr

Installing collected packages: tesseract-ocr

  Running setup.py install for tesseract-ocr ... error

    Complete output from command /anaconda3/bin/python3 -u -c "import setuptools, tokenize;__file__='/private/var/folders/tg/g57hkq3s3g163k6zstsmjfj00000gn/T/pip-install-mrfmt2dz/tesseract-ocr/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/var/folders/tg/g57hkq3s3g163k6zstsmjfj00000gn/T/pip-record-ad5y3pxn/install-record.txt --single-version-externally-managed --compile:

    running install

    running build

    running build_py

    file tesseract_ocr.py (for module tesseract_ocr) not found

    file tesseract_ocr.py (for module tesseract_ocr) not found

    running build_ext

    building 'tesseract_ocr' extension

    creating build

    creating build/temp.macosx-10.7-x86_64-3.7

    gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/anaconda3/include -arch x86_64 -I/anaconda3/include -arch x86_64 -I/anaconda3/include/python3.7m -c tesseract_ocr.cpp -o build/temp.macosx-10.7-x86_64-3.7/tesseract_ocr.o

    In file included from tesseract_ocr.cpp:651:

    In file included from /usr/local/include/tesseract/baseapi.h:27:

    In file included from /usr/local/include/tesseract/apitypes.h:23:

    /usr/local/include/tesseract/publictypes.h:33:1: error: unknown type name 'constexpr'

    constexpr int kPointsPerInch = 72;

    ^

    /usr/local/include/tesseract/publictypes.h:33:11: error: expected unqualified-id

    constexpr int kPointsPerInch = 72;

              ^

    /usr/local/include/tesseract/publictypes.h:38:1: error: unknown type name 'constexpr'

    constexpr int kMinCredibleResolution = 70;

    ^

    /usr/local/include/tesseract/publictypes.h:38:11: error: expected unqualified-id

    constexpr int kMinCredibleResolution = 70;

              ^

    /usr/local/include/tesseract/publictypes.h:40:1: error: unknown type name 'constexpr'

    constexpr int kMaxCredibleResolution = 2400;

    ^

    /usr/local/include/tesseract/publictypes.h:40:11: error: expected unqualified-id

    constexpr int kMaxCredibleResolution = 2400;

              ^

    /usr/local/include/tesseract/publictypes.h:45:1: error: unknown type name 'constexpr'

    constexpr int kResolutionEstimationFactor = 10;

    ^

    /usr/local/include/tesseract/publictypes.h:45:11: error: expected unqualified-id

    constexpr int kResolutionEstimationFactor = 10;

              ^

    In file included from tesseract_ocr.cpp:651:

    In file included from /usr/local/include/tesseract/baseapi.h:31:

    /usr/local/include/tesseract/resultiterator.h:103:63: error: a space is required between consecutive right angle brackets (use '> >')

      virtual std::vector>>* GetBestLSTMSymbolChoices() const;

                                                                  ^

    In file included from tesseract_ocr.cpp:651:

    In file included from /usr/local/include/tesseract/baseapi.h:32:

    In file included from /usr/local/include/tesseract/serialis.h:26:

    /usr/local/include/tesseract/host.h:30:10: fatal error: 'cinttypes' file not found

    #include   // PRId32, ...

             ^~~~~~~~~~~

    10 errors generated.

    error: command 'gcc' failed with exit status 1

    

    ----------------------------------------

Command "/anaconda3/bin/python3 -u -c "import setuptools, tokenize;__file__='/private/var/folders/tg/g57hkq3s3g163k6zstsmjfj00000gn/T/pip-install-mrfmt2dz/tesseract-ocr/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/var/folders/tg/g57hkq3s3g163k6zstsmjfj00000gn/T/pip-record-ad5y3pxn/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/tg/g57hkq3s3g163k6zstsmjfj00000gn/T/pip-install-mrfmt2dz/tesseract-ocr/

 

如果通过interpreter site-package中“+”在PyCharm中安装这个包的话

IDE会安装错误,查看Details的时候

报错:

fatal error: 'cinttypes' file not found

 

经过在Google中反复寻找解决办法,终于在Github上查到了解决方法:

输入指令:

CC=clang XCC=clang++ CPPFLAGS="-stdlib=libc++ -DUSE_STD_NAMESPACE -mmacosx-version-min=10.8" pip install tesserocr

 

安装过程:

Collecting tesserocr

  Using cached https://files.pythonhosted.org/packages/92/2d/05a7f8387e93c192919b508e4f4936f232bd3d2ca388b9130ae538a9f9ad/tesserocr-2.4.0.tar.gz

Building wheels for collected packages: tesserocr

  Building wheel for tesserocr (setup.py) ... done

  Stored in directory: /Users/zhangpeiji/Library/Caches/pip/wheels/f4/f8/13/9e30c62e12a7ec922c4cd3e8d936b63679f845543c2f66d172

Successfully built tesserocr

Installing collected packages: tesserocr

Successfully installed tesserocr-2.4.0

 

但是还有个问题,就是输入python3进入Python命令行之后

输入import tesserocr会报错

python3

Python 3.7.1 (default, Dec 14 2018, 13:28:58) 

[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> import tesserocr

!strcmp(locale, "C"):Error:Assert failed:in file baseapi.cpp, line 209

Illegal instruction: 4

同时在PyCharm中尽管interpreter中package里面已经看到tesserocr安装成功

但是IDE在引用这个包的时候还是会提醒错误“No module named tesserocr”

如果运行的话会报错:

!strcmp(locale, "C"):Error:Assert failed:in file baseapi.cpp, line 209

 

还是在Github上找到了解决办法,完整代码如下:

import locale

locale.setlocale(locale.LC_ALL,'C')

import tesserocr

from PIL import Image

image=Image.open('image.png')

print(tesserocr.image_to_text(image))

尽管IDE还是会在tesserocr处下划线提醒错误,但是已经可以正确运行了

你可能感兴趣的:(Python,debug,Python,爬虫,安装库)