天天看点

Python 图片识别 OCRPython 图片识别 OCR

文章目录

  • Python 图片识别 OCR
    • #1 需求
    • #2 环境
    • #3 安装
      • #3.1 macOS
      • #3.2 Linux(CentOS)
    • #4 使用
      • #4.1 python安装pytesseract库
      • #4.2 Python代码
    • #5 在线案例

Python 图片识别 OCR

#1 需求

  • 识别图片中的信息,如二维码

#2 环境

macOS / Linux
Python3.7.6           

复制

#3 安装

#3.1 macOS

  1. 安装 tesseract
//只安装tesseract,不安装训练工具
brew install tesseract
 
//安装tesseract的同时安装训练工具
brew install --with-training-tools tesseract
 
//安装tesseract的同时安装所有语言,语言包比较大,如果安装的话时间较长,建议不安装,按需选择
brew install  --all-languages tesseract
 
//安装tesseract,并安装训练工具和语言
brew install --all-languages --with-training-tools tesseract            

复制

2. 下载语言包

地址 : https://github.com/tesseract-ocr/tessdata

我这里安装的是中文语言包

中文语言包 : https://github.com/tesseract-ocr/tessdata/blob/master/chi_sim.traineddata

然后将下载的中文语言包拷贝到如下路径 :

/usr/local/Cellar/tesseract/4.0.0_1/share/tessdata

3. 查看本地语言包

tesseract --list-langs           

复制

Python 图片识别 OCRPython 图片识别 OCR

#3.2 Linux(CentOS)

  1. 安装依赖
yum install autoconf automake libtool libjpeg-devel libpng-devel libtiff-devel zlib-devel           

复制

2. 安装 leptonica

下载 : wget https://github.com/tesseract-ocr/tesseract/archive/4.1.0.tar.gz

解压安装

tar -xzvf leptonica-1.74.4.tar.gz
cd leptonica-1.74.4.tar.gz
./configure --profix=/usr/local/leptonica
make
sudo make install           

复制

3. 安装 tesseract-ocr

wget https://github.com/tesseract-ocr/tesseract/archive/3.04.zip
unzip 3.04.zip
cd tesseract-3.04/
./configure
make && make install
sudo ldconfig           

复制

我这里安装的是中文语言包

中文语言包 : https://github.com/tesseract-ocr/tessdata/blob/master/chi_sim.traineddata

然后将下载的中文语言包拷贝到如下路径 :

/usr/local/share/tessdata

#4 使用

#4.1 python安装pytesseract库

pip install pytesseract
pip install Pillow           

复制

#4.2 Python代码

from PIL import Image
import pytesseract
 
# 指定图片路径和识别的语言
data = pytesseract.image_to_string(Image.open('/Users/Documents/1.png'), lang='chi_sim')
print(data)           

复制

#5 在线案例

地址 :

http://admin.minhung.me:20420/#/

Python 图片识别 OCRPython 图片识别 OCR