0
点赞
收藏
分享

微信扫一扫

【NLP开发】Python识别图片文字、中文分词


文章目录

  • ​​1、Tesseract​​
  • ​​1.1 下载安装​​
  • ​​1.2 命令行​​
  • ​​1.3 python接口​​
  • ​​2、EasyOCR​​
  • ​​2.1 简介​​
  • ​​2.2 安装​​
  • ​​2.3 测试代码​​
  • ​​3、PaddleOCR​​
  • ​​4、Jieba(分词)​​
  • ​​4.1 简介​​
  • ​​4.2 安装​​
  • ​​4.3 测试代码​​
  • ​​结语​​

OCR 的前身是光学字符识别,它对当今的数字世界具有革命性意义。OCR 实际上是一个完整的过程,在此过程中,数字世界中存在的图像/文档被处理,文本被处理成普通的可编辑文本。

1、Tesseract

Tesseract最初由惠普实验室支持,用于电子版文字识别,1996年被移植到Windows上,1998年进行了C++化,在2005年Tesseract由惠普公司宣布开源。2006年到现在,由Google公司维护开发。
最初Tesseract是用C语言写的,在1998年改用C++。

1.1 下载安装

​​https://digi.bib.uni-mannheim.de/tesseract/​​​​https://tesseract-ocr.github.io/tessdoc/Data-Files​​

下载Tesseract的安装程序如下:

【NLP开发】Python识别图片文字、中文分词_中文分词


安装Tesseract后,文件夹如下:

【NLP开发】Python识别图片文字、中文分词_Tesseract_02


下载中英文语言包:

​​https://github.com/tesseract-ocr/tessdata​​

【NLP开发】Python识别图片文字、中文分词_人工智能_03

1.2 命令行

  • (1)安装测试

tesseract -v

【NLP开发】Python识别图片文字、中文分词_人工智能_04

  • (2)识别图片中文字
    tesseract imagename outputbase [-l lang] [–psm pagesegmode] [configfile…]
    解释:tesseract 图片名 输出文件名 -l 字库文件 -psm pagesegmode 配置文件

tesseract d:\20190219162542304.png result

【NLP开发】Python识别图片文字、中文分词_Tesseract_05

  • 测试的图片d:\20190219162542304.png:
  • 测试的结果文件如下:

    使用中文语言包再次进行识别:

tesseract d:\20190219162542304.png result -l chi_sim

【NLP开发】Python识别图片文字、中文分词_中文分词_06

  • 测试的结果文件如下:

此外还有一个参数psm:比如tesseract test.jpg result -l eng --psm 7 nobatch
psm 参数说明

0 = Orientation and script detection (OSD) only.
1 = Automatic page segmentation with OSD.
2 = Automatic page segmentation, but no OSD, or OCR
3 = Fully automatic page segmentation, but no OSD. (Default)
4 = Assume a single column of text of variable sizes.
5 = Assume a single uniform block of vertically aligned text.
6 = Assume a single uniform block of text.
7 = Treat the image as a single text line.
8 = Treat the image as a single word.
9 = Treat the image as a single word in a circle.
10 = Treat the image as a single character.

1.3 python接口

  • 安装库:

pip install pytesseract
pip install pillow

  • 测试代码:

import pytesseract
from PIL import Image
# 读取图片
im = Image.open('sentence.jpg')
# 识别文字
string = pytesseract.image_to_string(im)
print(string)

import pytesseract
from PIL import Image
# 读取图片
im = Image.open('sentence.png')
# 识别文字,并指定语言
string = pytesseract.image_to_string(im, lang='chi_sim')
print(string)

import os
import pytesseract
# 文字图片的路径
path = 'text_img/'
# 获取图片路径列表
imgs = [path + i for i in os.listdir(path)]
# 打开文件
f = open('text.txt', 'w+', encoding='utf-8')
# 将各个图片的路径写入text.txt文件当中
for img in imgs:
f.write(img + '\n')
# 关闭文件
f.close()
# 文字识别
string = pytesseract.image_to_string('text.txt', lang='chi_sim')
print(string)

2、EasyOCR

2.1 简介

EasyOCR 实际上是一个 Python 包,它将 PyTorch 作为后端处理程序。

EasyOCR 像任何其他 OCR(Google 的 tesseract 或任何其他)一样检测图像中的文本,但我在使用它时,我发现它是从图像中检测文本的最直接的方法,而且它将 PyTorch 作为后端处理程序,准确性更可靠。

EasyOCR 支持 42 多种语言进行检测。EasyOCR 是由 Jaided AI 公司创建的。

2.2 安装

Ready-to-use OCR with 80+ supported languages and all popular writing scripts including: Latin, Chinese, Arabic, Devanagari, Cyrillic, etc.
​​​https://pypi.org/project/easyocr/​​

pip install easyocr

在线测试如下:
​​​https://www.jaided.ai/easyocr/​​

  • 测试图片如下:
  • 识别结果如下:

2.3 测试代码

import os
import easyocr
import cv2
from matplotlib import pyplot as plt
import numpy as np

IMAGE_PATH = 'test.jpg'
reader = easyocr.Reader(['en'])
result = reader.readtext(IMAGE_PATH,paragraph="False")
print(result)

import os
import easyocr
import cv2

reader = easyocr.Reader(['ch_sim'])
result = reader.readtext(r'd:\test_chs.png', detail = 0, paragraph=True)
print(result)

3、PaddleOCR

​​https://github.com/PaddlePaddle/PaddleOCR​​

支持多语言识别,目前能够支持 80 多种语言;
除了能对中文、英语、数字识别之外,还能应对字体倾斜、文本中含有小数点字符等复杂情况
提供有丰富的 OCR 领域相关工具供我们使用,方便我们制作自己的数据集、用于训练。

PaddleOCR 需在 PaddlePaddle2.0 下才可以正常运行,开始之前请确保 PaddlePaddle2.0 已经安装。

python3 -m pip install paddlepaddle==2.0.0 -i https://mirror.baidu.com/pypi/simple
git clone https://github.com/PaddlePaddle/PaddleOCR
cd PaddleOCR
pip3 install -r requirements.txt

  • 使用 gpu,识别单张图片

python3 tools/infer/predict_system.py --image_dir="./doc/imgs/11.jpg" --det_model_dir="./inference/ch_ppocr_mobile_v2.0_det_infer/"  --rec_model_dir="./inference/ch_ppocr_mobile_v2.0_rec_infer/" --cls_model_dir="./inference/ch_ppocr_mobile_v2.0_cls_infer/" --use_angle_cls=True --use_space_char=True

  • 使用 gpu ,识别多张图片

python3 tools/infer/predict_system.py --image_dir="./doc/imgs/" --det_model_dir="./inference/ch_ppocr_mobile_v2.0_det_infer/"  --rec_model_dir="./inference/ch_ppocr_mobile_v2.0_rec_infer/" --cls_model_dir="./inference/ch_ppocr_mobile_v2.0_cls_infer/" --use_angle_cls=True --use_space_char=True

  • 不使用gpu,识别单张图片

python3 tools/infer/predict_system.py --image_dir="./doc/imgs/11.jpg" --det_model_dir="./inference/ch_ppocr_mobile_v2.0_det_infer/"  --rec_model_dir="./inference/ch_ppocr_mobile_v2.0_rec_infer/" --cls_model_dir="./inference/ch_ppocr_mobile_v2.0_cls_infer/" --use_angle_cls=True --use_space_char=True --use_gpu=False

4、Jieba(分词)

​​https://pypi.org/project/jieba/​​​​https://github.com/fxsjy/jieba​​

“结巴”中文分词:做最好的 Python 中文分词组件

“Jieba” (Chinese for “to stutter”) Chinese text segmentation: built to
be the best Python Chinese word segmentation module.

4.1 简介

自然语言处理( Natural Language Processing, NLP)是计算机科学领域与人工智能领域中的一个重要方向。jieba的主要功能是做中文分词,可以进行简单分词、并行分词、命令行分词,当然它的功能不限于此,目前还支持关键词提取、词性标注、词位置查询等。

  • Jieba其实并不是只有分词这一个功能,其是一个开源框架,提供了很多在分词之上的算法,如关键词提取、词性标注等。
  • Jieba官方提供了Python、C++、Go、R、iOS等多平台多语言支持,不仅如此,还提供了很多热门社区项目的扩展插件,如ElasticSearch、solr、lucene等。在实际项目中,进行扩展十分容易。

Jieba提供了三种分词模式:

  • 精确模式:试图将句子最精确地切开,适合文本分析。
  • 全模式:把句子中所有可以成词的词语都扫描出来,速度非常快,但是不能解决歧义。
  • 搜索引擎模式:在精确模式的基础上,对长词再次切分,提高召回率,适合用于搜索引擎分词。

4.2 安装

pip install jieba -i https://mirror.baidu.com/pypi/simple

【NLP开发】Python识别图片文字、中文分词_人工智能_07

4.3 测试代码

  • 例子1:官网入门例子

# encoding=utf-8
import jieba

#jieba.enable_paddle()# 启动paddle模式。 0.40版之后开始支持,早期版本不支持
strs=["我来到北京清华大学","乒乓球拍卖完了","中国科学技术大学"]
for str in strs:
seg_list = jieba.cut(str,use_paddle=True) # 使用paddle模式
print("Paddle Mode: " + '/'.join(list(seg_list)))

seg_list = jieba.cut("我来到北京清华大学", cut_all=True)
print("Full Mode: " + "/ ".join(seg_list)) # 全模式

seg_list = jieba.cut("我来到北京清华大学", cut_all=False)
print("Default Mode: " + "/ ".join(seg_list)) # 精确模式

seg_list = jieba.cut("他来到了网易杭研大厦") # 默认是精确模式
print(", ".join(seg_list))

seg_list = jieba.cut_for_search("小明硕士毕业于中国科学院计算所,后在日本京都大学深造") # 搜索引擎模式
print(", ".join(seg_list))

【NLP开发】Python识别图片文字、中文分词_python_08

  • 例子2:四种搜索模式

import jieba

sent = '中文分词是文本处理不可或缺的一步!'

seg_list = jieba.cut(sent, cut_all=True)
print('全模式:', '/ '.join(seg_list))

seg_list = jieba.cut(sent, cut_all=False)
print('精确模式:', '/ '.join(seg_list))

seg_list = jieba.cut(sent)
print('默认精确模式:', '/ '.join(seg_list))

seg_list = jieba.cut_for_search(sent)
print('搜索引擎模式', '/ '.join(seg_list))

【NLP开发】Python识别图片文字、中文分词_人工智能_09

  • 例子3:带词性的分词

import jieba.posseg as jp
print(jp.lcut('我爱西樵山'))

【NLP开发】Python识别图片文字、中文分词_人工智能_10

  • 例子4:词与词性间映射

from jieba.posseg import dt
print(dt.word_tag_tab)

【NLP开发】Python识别图片文字、中文分词_人工智能_11

结语

​如果您觉得该方法或代码有一点点用处,可以给作者点个赞​

​​​如果您感觉方法或代码不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进;​​​o_O???
​​​如果您需要相关功能的代码定制化开发,可以留言私信作者;​​​(✿◡‿◡)
​​​感谢各位大佬童鞋们的支持!​​( ´ ▽´ )ノ ( ´ ▽´)っ!!!




举报

相关推荐

0 条评论