电脑mac本地,楼主本来想用下面这段代码识别一个图片中的数字:
import pytesseract
from PIL import Image
img = Image.open('/Users/liangmimi/Desktop/11.png')
a = pytesseract.image_to_string(img, config='--psm 6')
print(a)
1. pip install pytesseract成功后运行上面的代码,报错没有tesseract
Traceback (most recent call last):
File "/Users/User/Environments/testEnv/testTess.py", line 26, in
i = pytesseract.image_to_string(img)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pytesseract/pytesseract.py", line 122, in image_to_string
config=config)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pytesseract/pytesseract.py", line 46, in run_tesseract
proc = subprocess.Popen(command, stderr=subprocess.PIPE)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'tesseract': 'tesseract'
这说明需要安装 tesseract-ocr
2. 用pip install tesseract-ocr未成功,报错没有allheaders.h
creating build/temp.macosx-10.5-x86_64-2.7
gcc -fno-strict-aliasing -I//anaconda/include -arch x86_64 -DNDEBUG -g
-fwrapv -O3 -Wall -Wstrict-prototypes -I//anaconda/include/python2.7 -c
tesseract_ocr.cpp -o build/temp.macosx-10.5-x86_64-2.7/tesseract_ocr.o
tesseract_ocr.cpp:264:10:
fatal error: 'leptonica/allheaders.h' file not found #include "leptonica/allheaders.h"
^
1 error generated.
error: command 'gcc' failed with exit status 1
说明需要安装leptonica,那么需要先安装brew。
3. 安装brew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
参考网址:https://blog.csdn.net/boyqicheng/article/details/71481213?utm_source=itdadao&utm_medium=referral
4. brew install leptonica成功
5. 用pip install tesseract-ocr未成功,报错没有cstdint
/usr/local/include/tesseract/host.h:28:10: fatal error: 'cstdint' file not found
#include // int32_t, ...
^~~~~~~~~
1 error generated.
error: command 'gcc' failed with exit status 1
按你的路径 将host.h文件中的include
参考网址:https://blog.csdn.net/qq_37566910/article/details/82946276
6. pip install tesseract-ocr成功,运行第一段代码结果正确。