ubuntu+python+百度AI识别图片中的文字

  1. 获取百度AI调用的 ID 和 key
    打开百度AI官网:https://ai.baidu.com/
    点击控制台-登录账号-产品服务-图像识别-创建应用,勾选需要的服务(图像服务已默认全都选上, 此时我们还有勾选上文字识别中的功能)-立即创建
    在应用列表中,可以看到自己的AppID,API Key,Secret Key

  2. 安装百度AI库
    sudo pip install baidu-aip (在上节中已安装python的pip包管理工具)

  3. 编写python代码

     # encoding=utf-8
     """
     Created on Tue Jun 12 09:37:38 2018
     利用百度api实现图片文本识别
     @author: XnCSD
     """
     
     import glob
     from os import path
     import os
     from aip import AipOcr
     from PIL import Image
     import sys
     
     reload(sys)
     sys.setdefaultencoding('utf8')  # 解决UnicodeEncodeError: 'ascii' 不支持utf8的问题
     
     
     def convertimg(picfile, outdir):
         '''调整图片大小,对于过大的图片进行压缩
         picfile:    图片路径
         outdir:    图片输出路径
         '''
         img = Image.open(picfile)
         width, height = img.size
         while (width * height > 4000000):  # 该数值压缩后的图片大约 两百多k
             width = width // 2
             height = height // 2
         new_img = img.resize((width, height), Image.BILINEAR)
         new_img.save(path.join(outdir, os.path.basename(picfile)))
     
     
     def baiduOCR(picfile, outfile):
         """利用百度api识别文本,并保存提取的文字
         picfile:    图片文件名
         outfile:    输出文件
         """
         filename = path.basename(picfile)
         APP_ID = 'xxxxxxx'  # 刚才获取的 ID,下同
         API_KEY = 'xxxxxxxx'
         SECRECT_KEY = 'xxxxxxxxx'
         client = AipOcr(APP_ID, API_KEY, SECRECT_KEY)
    
         i = open(picfile, 'rb')
         img = i.read()
         print("正在识别图片:\t" + filename)
         message = client.basicGeneral(img)  # 通用文字识别,每天 50 000 次免费
         # message = client.basicAccurate(img)   # 通用文字高精度识别,每天 800 次免费
         print("识别成功!")
         i.close()
    
         with open(outfile, 'a+') as fo:
             # 输出文本内容
             for text in message.get('words_result'):
                 fo.writelines(str(text.get('words')) + '\n')
    
         print("文本导出成功!")
    
    
     if __name__ == "__main__":
         outfile = './export.txt'
         outdir = './tmp'
         if path.exists(outfile):
             os.remove(outfile)
         if not path.exists(outdir):
             os.mkdir(outdir)
         print("压缩过大的图片...")
         # 首先对过大的图片进行压缩,以提高识别速度,将压缩的图片保存与临时文件夹中
         for picfile in glob.glob(" ./picture/*"):  
             print 'picfile',picfile
             convertimg(picfile, outdir)
    
         print("图片识别...")
         for picfile in glob.glob("tmp/*"):
             baiduOCR(picfile, outfile)
             os.remove(picfile)
         print('图片文本提取结束!文本输出结果位于 %s 文件中。' % outfile)
         os.removedirs(outdir)
    

    要识别的图片需放在picture文件夹中
    最后运行代码,识别结果保存在./export.txt中

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值