本文主要是各类ocr的api对比问题,至于app推荐几款:合合信息(扫面全能王),TextGrabber,白描等等等等

工作需要,搞文字识别技术,对比了几家

百度的OCR:

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Tue Jun 12 09:37:38 2018
  5. 利用百度api实现图片文本识别
  6. @author: XnCSD
  7. """
  8.  
  9. import glob
  10. from os import path
  11. import os
  12. from aip import AipOcr
  13. from PIL import Image
  14.  
  15. def convertimg(picfile, outdir):
  16. '''
  17. 调整图片大小,对于过大的图片进行压缩
  18. picfile: 图片路径
  19. outdir: 图片输出路径
  20. '''
  21. img = Image.open(picfile)
  22. width, height = img.size
  23. while (width * height > 4000000): # 该数值压缩后的图片大约 两百多k
  24. width = width // 2
  25. height = height // 2
  26. new_img = img.resize((width, height), Image.BILINEAR)
  27. new_img.save(path.join(outdir, os.path.basename(picfile)))
  28.  
  29. def baiduOCR(picfile, outfile):
  30. """利用百度api识别文本,并保存提取的文字
  31. picfile: 图片文件名
  32. outfile: 输出文件
  33. """
  34. filename = path.basename(picfile)
  35.  
  36. APP_ID = '你自己的appid' # 刚才获取的 ID,下同
  37. API_KEY = '创建完实例人家给'
  38. SECRECT_KEY = '创建完实例人家给'
  39. client = AipOcr(APP_ID, API_KEY, SECRECT_KEY)
  40.  
  41. i = open(picfile, 'rb')
  42. img = i.read()
  43. print("正在识别图片:\t" + filename)
  44. # message = client.basicGeneral(img) # 通用文字识别,每天 50 000 次免费
  45. message = client.basicAccurate(img) # 通用文字高精度识别,每天 800 次免费
  46. print("识别成功!")
  47. i.close()
  48.  
  49. with open(outfile, 'a+', encoding='utf8') as fo:
  50. fo.writelines("+" * 60 + '\n')
  51. fo.writelines("识别图片:\t" + filename + "\n" * 2)
  52. fo.writelines("文本内容:\n")
  53. # 输出文本内容
  54. for text in message.get('words_result'):
  55. fo.writelines(text.get('words') + '\n')
  56. fo.writelines('\n' * 2)
  57. print("文本导出成功!")
  58. print()
  59.  
  60. if __name__ == "__main__":
  61.  
  62. outfile = '最后写入的文件名'
  63. outdir ='图片压缩后存储的路径'
  64. if path.exists(outfile):
  65. os.remove(outfile)
  66. if not path.exists(outdir):
  67. os.mkdir(outdir)
  68. print("压缩过大的图片...")
  69. # 首先对过大的图片进行压缩,以提高识别速度,将压缩的图片保存与临时文件夹中
  70. for picfile in glob.glob("picture\*"): # 在picture文件夹里放图片
  71. convertimg(picfile, outdir)
  72. print("图片识别...")
  73. for picfile in glob.glob("outdir\*"):
  74. baiduOCR(picfile, outfile)
  75. os.remove(picfile)
  76. print('图片文本提取结束!文本输出结果位于 %s 文件中。' % outfile)
  77. os.removedirs(outdir)

有道的OCR(加入了自己写的图片压缩以及循环的逻辑,如果有不对的地方请指出):

  1. #/usr/bin/env python
  2. #coding=utf8
  3.  
  4. import hashlib
  5. from PIL import Image
  6. import os
  7. import requests
  8. import random
  9. import glob
  10. import json
  11. from os import path
  12. import base64
  13. import time
  14.  
  15. def convertimg(picfile, outdir):
  16. '''
  17. 调整图片大小,对于过大的图片进行压缩
  18. picfile: 图片路径
  19. outdir: 图片输出路径
  20. '''
  21. img = Image.open(picfile)
  22. width, height = img.size
  23. while (width * height > 4000000): # 该数值压缩后的图片大约 两百多k
  24. width = width // 2
  25. height = height // 2
  26. new_img = img.resize((width, height), Image.BILINEAR)
  27. new_img.save(path.join(outdir, os.path.basename(picfile)))
  28.  
  29. def youdaoOCR(picfile, outfile):
  30. """利用有道api识别文本,并保存提取的文字
  31. picfile: 图片文件名
  32. outfile: 输出文件
  33. """
  34. appKey = '自己的appkey' # 需要到有道云官网去注册实例
  35. secretKey = '自己的secretKey'
  36. httpClient = None
  37. try:
  38. filename = path.basename(picfile)
  39. print('正在识别%s' % filename)
  40. # print(picfile)
  41. f = open(picfile, 'rb') # 二进制方式打开图文件
  42. img = base64.b64encode(f.read()) # 读取文件内容,转换为base64编码
  43. f.close()
  44. img = str(img, 'utf-8')
  45. detectType = ''
  46. imageType = ''
  47. langType = 'auto'
  48. salt = random.randint(1, 65536)
  49. sign = appKey + img + str(salt) + secretKey
  50. m1 = hashlib.md5()
  51. m1.update(sign.encode('utf-8'))
  52. sign = m1.hexdigest()
  53. data = {'appKey': appKey, 'img': img, 'detectType': detectType, 'imageType': imageType,
  54. 'langType': langType, 'salt': str(salt), 'sign': sign}
  55. req = requests.post('http://openapi.youdao.com/ocrapi', data)
  56.  
  57. content = req.text
  58. j = json.loads(content)
  59. # print(j)
  60. # print(j['Result']['regions'])
  61. lst = []
  62. for regionstr in j['Result']['regions']:
  63. for lineStr in regionstr['lines']:
  64. # print(lineStr['text'])
  65. lst.append(lineStr['text'])
  66. result = ','.join(lst)
  67. print(result)
  68. except ValueError:
  69. print("error")
  70.  
  71. if __name__ == '__main__':
  72. outfile = 'youdao.txt' # 最后写入的文件
  73. outdir = '压缩的图片存储位置'
  74. if path.exists(outfile):
  75. os.remove(outfile) # 如果有要写入的文件,删除
  76. if not path.exists(outdir):
  77. os.mkdir(outdir) # 如果存储压缩后的图片的文件夹,创建
  78. print("正在压缩过大的图片....")
  79. for picfile in glob.glob(r"picture\*"): # 图片存储路径
  80. convertimg(picfile, outdir)
  81. print('进行图片识别....')
  82. for picfile in glob.glob('压缩的图片存储位置\*'):
  83. youdaoOCR(picfile, outfile)
  84. os.remove(picfile)
  85. # print('图片提取结束!文本输出结果位于 %s 文件中。' % outfile)
  86. os.removedirs(outdir)

阿里的OCR(也加入了图片压缩以及循环功能(官网没有)):

  1. import urllib.request
  2. import urllib.parse
  3. import json
  4. import time
  5. import base64
  6. import glob
  7. import re
  8. from PIL import Image
  9. from os import path
  10. import os
  11. import time
  12.  
  13. def convertimg(picfile, outdir):
  14. '''
  15. 调整图片大小,对于过大的图片进行压缩
  16. picfile: 图片路径
  17. outdir: 图片输出路径
  18. '''
  19. img = Image.open(picfile)
  20. width, height = img.size
  21. while (width * height > 4000000): # 该数值压缩后的图片大约 两百多k
  22. width = width // 2
  23. height = height // 2
  24. new_img = img.resize((width, height), Image.BILINEAR)
  25. new_img.save(path.join(outdir, os.path.basename(picfile)))
  26.  
  27. #请求头
  28. headers = {
  29. 'Authorization': 'APPCODE 自己的appcode', # 阿里云官网上创建实例
  30. 'Content-Type': 'application/json; charset=UTF-8'
  31. }
  32. def posturl(url,data={}): # data为第一张的默认参数, data1为第二张的默认参数
  33. try:
  34. params = json.dumps(dict).encode(encoding='UTF8')
  35. req = urllib.request.Request(url, params, headers)
  36. r = urllib.request.urlopen(req)
  37. html = r.read()
  38. r.close()
  39. return html.decode("utf8")
  40. except urllib.error.HTTPError as e:
  41. print(e.code)
  42. print(e.read().decode("utf8"))
  43. time.sleep(1)
  44. if __name__=="__main__":
  45. url_request = "https://ocrapi-document.taobao.com/ocrservice/document"
  46. outfile = 'ali.txt'
  47. outdir = '压缩图片的存储位置'
  48. if path.exists(outfile):
  49. os.remove(outfile)
  50. if not path.exists(outdir):
  51. os.mkdir(outdir)
  52. print('开始进行图片压缩....')
  53. for picfile in glob.glob(r"picture\*"): # 图片的存储位置
  54. convertimg(picfile, outdir)
  55. print('开始进行图片识别....')
  56. for picfile in glob.glob("outdir\*"):
  57. filename = path.basename(picfile)
  58. print('正在识别%s' % filename)
  59. with open(picfile, 'rb') as f: # 以二进制读取本地图片
  60. data = f.read()
  61. encodestr = str(base64.b64encode(data), 'utf-8')
  62.  
  63. dict = {'img': encodestr}
  64. html = posturl(url_request, data=dict)
  65. os.remove(picfile)
  66. html1 = json.loads(html)
  67. # print(html)
  68. # print(type(html))
  69. lst = []
  70. # print(html1['prism_wordsInfo'])
  71. for i in html1['prism_wordsInfo']:
  72. if i.get('pos'):
  73. i.pop('pos')
  74. for k, v in i.items():
  75. lst.append(v)
  76. # print(lst)
  77. str1 = ','.join(lst)
  78. print(str1)
  79. os.removedirs(outdir)

以下仅为个人意见:

我主要考虑的是有道和阿里:在准确度上差别不是很大,阿里稍微准确一丢丢

             价格方面阿里是有道的两倍左右(阿里的为高精度文字识别)

             速度上阿里更快一些。

如果你最近在考虑OCR的问题,请进来~~~的更多相关文章

  1. 如果你的unordered_map头文件报错请看这里

    请将include<unordered_map>头文件换成下面代码 #if(__cplusplus == 201103L) #include <unordered_map> # ...

  2. 谈谈书本《c#物联网程序设计基础》中的技术瑕疵,如果你将要读本书,请进来看看!

    今天去书店看到一本名为<c#物联网程序设计基础>的书,对物联网感兴趣的我抓起来就看,书中的项目都是上位机开发项目,较简单,如果物联网开发只是这样,看起来我做物联网开发也是绰绰有余.这边书我 ...

  3. 阿里巴巴-OS事业群-OS手机事业部-系统服务部门招聘Java开发工程师,有意者请进来

    我是阿里巴巴-OS事业群-OS手机事业部-系统服务部的开发工程师,正在招聘Java开发工程师. 以下是职位描述: 岗位名称:Java开发工程师 招聘人数:5人 生效日期:2014-03-12 结束日期 ...

  4. 说说我在项目中为什么不用实体框架,如果说我在诋毁你所爱的EF,请进来.

    1.坑多. 这一点没有人会否定.当然你可以说你很牛,但事实不会因为你牛就可以说不存在.从博客园中的博问中大家关于EF的提问量就问题的怪异程度就可以看出来. 1.Entity Framework 查询历 ...

  5. C\C++各路高手以及操作系统专家请进来杀死这个进程

    通常情况下编写一个程序,能够点击关闭button正常结束程序,也能够使用任务管理器结束任务,还能够使用taskkill等命令杀死进程,实在都不行也能够直接重新启动计算机. 可是,这些方法真的都管用吗? ...

  6. 【MongoDB】从入门到精通mongdb系列学习宝典,想学mongodb小伙伴请进来

    最近一段时间在学习MongoDB,在学习过程中总共编写了四十余篇博客.从mongodb软件下载到分片集群的搭建. 从理论讲解到实例练习.现在把所有博客的内容做个简单目录,方便阅读的小伙伴查询. 一. ...

  7. Postman SMTP 存在跨站脚本(XSS)漏洞,请换用Post SMTP Mailer/Email Log

    Postman SMTP 是一个安装量超过10W的WordPress插件,但是已经2年多没有更新,2017年6月29日,被发现存在跨站脚本(XSS)漏洞(查看详情),并且作者一直没有更新,所以被从Wo ...

  8. 毕业N年后,请不要像我一样被档案烦死

    目录 一. 提醒大学生:深刻重视档案,避免以后麻烦! 二.说说我因为档案造成的烦心事! 三.说说档案这档子事: 四.档案如此重要,为什么有些人却成了弃档族? 五.档案该怎么操作才能不当"弃档 ...

  9. [转自老马的文章]用MODI OCR 21种语言

    作者:马健邮箱:stronghorse_mj@hotmail.com发布:2007.12.08更新:2012.07.09按照<MODI中的OCR模块>一文相关内容进行修订2012.07.0 ...

随机推荐

  1. XAMPP非本地访问被拒绝解决办法

    问题场景: 本机搭建一个apache服务器,正常访问XAMPP目录下的页面. 手机接入同一wifi,以电脑ip方式访问该目录下的页面:提示:Access Denied Access to the re ...

  2. nignx reload的时候报错invalid PID number

    第一种思路是从PID号着手,提示无效PID号时nginx.pid文件为空,将进程的PID号追加到空的nginx.pid上,问题解决: 还有问题,请参考:https://www.cnblogs.com/ ...

  3. Linux的进程与服务(一)

    启动的配置文件/etc/inittab,修改完配置文件以后 init q立即生效 # Default runlevel. The runlevels used by RHS are: # - halt ...

  4. (转)centos liveCD liveDVD netinstall minimal DVD1 DVD2 版本区别

    LiveCD 和 LiveDVD 是可以直接光盘运行系统,但不能安装,两者差别在于容量大小,dvd包含的软件要多一些. netinstall 是用于网络安装和系统救援的镜像文件. minimal 这个 ...

  5. type="submit" 和type="button"

    今天,小菜鸟又遇到一个问题,当不小心在页面输入框回车一下,结果莫名的页面发出了一个请求. 把问题定位在一个button上,代码是这样写的<button class="btn btn-d ...

  6. 第二周leetcode

    4/4 这周莫名得忙,一天是做编译,一天是做模式识别作业,(一天刷魔兽皮肤),周末玩了两天,总的来说还是松懈了,大概只做了两天的leetcode,刷了10道题,羞愧羞愧. 决定每次把代码附上在这个总结 ...

  7. 配置IIS Express,支持JSON

    方法有2种: 1. 命令行 a. cd "iis express的安装目录"  例如:cd C:\Program Files (x86)\IIS Express b. appcmd ...

  8. Linux Socket - 内核非阻塞功能

    select 函数 int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval*tim ...

  9. 【转载】45个设计师们不常见的html5和css3漂亮模板

    对于Web开发人员来说,当他们需要创建一个非常时尚和新潮的CSS3和HTML5网站时需要非常专业的水准.html5和css3的结合能够做出非同寻常的网站效果..所以,今天,我推荐给大家45个免费的时尚 ...

  10. Hadoop各个组件与端口

    组件 Daemon 端口 配置 说明 HDFS DataNode 50010 dfs.datanode.address datanode服务端口,用于数据传输 HDFS DataNode 50075 ...