Python识别中的文字

1 利用百度OCR功能(需电脑联网等百度账号)

参照https://www.cnblogs.com/chunfang/p/13234289.html

pip install baidu-aip

2. 本地安装Tesseract

pip install pytesseract 文字识别

pip install pillow 图片读取
 
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)
posted @ 2021-05-20 17:51  Sunnyday21  阅读(167)  评论(0)    收藏  举报