如果你最近在考虑OCR的问题,请进来~~~
本文主要是各类ocr的api对比问题,至于app推荐几款:合合信息(扫面全能王),TextGrabber,白描等等等等
工作需要,搞文字识别技术,对比了几家
百度的OCR:
- #!/usr/bin/env python3
- # -*- coding: 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
- 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 = '你自己的appid' # 刚才获取的 ID,下同
- API_KEY = '创建完实例人家给'
- SECRECT_KEY = '创建完实例人家给'
- 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+', encoding='utf8') as fo:
- fo.writelines("+" * 60 + '\n')
- fo.writelines("识别图片:\t" + filename + "\n" * 2)
- fo.writelines("文本内容:\n")
- # 输出文本内容
- for text in message.get('words_result'):
- fo.writelines(text.get('words') + '\n')
- fo.writelines('\n' * 2)
- print("文本导出成功!")
- print()
- if __name__ == "__main__":
- outfile = '最后写入的文件名'
- outdir ='图片压缩后存储的路径'
- if path.exists(outfile):
- os.remove(outfile)
- if not path.exists(outdir):
- os.mkdir(outdir)
- print("压缩过大的图片...")
- # 首先对过大的图片进行压缩,以提高识别速度,将压缩的图片保存与临时文件夹中
- for picfile in glob.glob("picture\*"): # 在picture文件夹里放图片
- convertimg(picfile, outdir)
- print("图片识别...")
- for picfile in glob.glob("outdir\*"):
- baiduOCR(picfile, outfile)
- os.remove(picfile)
- print('图片文本提取结束!文本输出结果位于 %s 文件中。' % outfile)
- os.removedirs(outdir)
有道的OCR(加入了自己写的图片压缩以及循环的逻辑,如果有不对的地方请指出):
- #/usr/bin/env python
- #coding=utf8
- import hashlib
- from PIL import Image
- import os
- import requests
- import random
- import glob
- import json
- from os import path
- import base64
- import time
- 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 youdaoOCR(picfile, outfile):
- """利用有道api识别文本,并保存提取的文字
- picfile: 图片文件名
- outfile: 输出文件
- """
- appKey = '自己的appkey' # 需要到有道云官网去注册实例
- secretKey = '自己的secretKey'
- httpClient = None
- try:
- filename = path.basename(picfile)
- print('正在识别%s' % filename)
- # print(picfile)
- f = open(picfile, 'rb') # 二进制方式打开图文件
- img = base64.b64encode(f.read()) # 读取文件内容,转换为base64编码
- f.close()
- img = str(img, 'utf-8')
- detectType = ''
- imageType = ''
- langType = 'auto'
- salt = random.randint(1, 65536)
- sign = appKey + img + str(salt) + secretKey
- m1 = hashlib.md5()
- m1.update(sign.encode('utf-8'))
- sign = m1.hexdigest()
- data = {'appKey': appKey, 'img': img, 'detectType': detectType, 'imageType': imageType,
- 'langType': langType, 'salt': str(salt), 'sign': sign}
- req = requests.post('http://openapi.youdao.com/ocrapi', data)
- content = req.text
- j = json.loads(content)
- # print(j)
- # print(j['Result']['regions'])
- lst = []
- for regionstr in j['Result']['regions']:
- for lineStr in regionstr['lines']:
- # print(lineStr['text'])
- lst.append(lineStr['text'])
- result = ','.join(lst)
- print(result)
- except ValueError:
- print("error")
- if __name__ == '__main__':
- outfile = 'youdao.txt' # 最后写入的文件
- outdir = '压缩的图片存储位置'
- if path.exists(outfile):
- os.remove(outfile) # 如果有要写入的文件,删除
- if not path.exists(outdir):
- os.mkdir(outdir) # 如果存储压缩后的图片的文件夹,创建
- print("正在压缩过大的图片....")
- for picfile in glob.glob(r"picture\*"): # 图片存储路径
- convertimg(picfile, outdir)
- print('进行图片识别....')
- for picfile in glob.glob('压缩的图片存储位置\*'):
- youdaoOCR(picfile, outfile)
- os.remove(picfile)
- # print('图片提取结束!文本输出结果位于 %s 文件中。' % outfile)
- os.removedirs(outdir)
阿里的OCR(也加入了图片压缩以及循环功能(官网没有)):
- import urllib.request
- import urllib.parse
- import json
- import time
- import base64
- import glob
- import re
- from PIL import Image
- from os import path
- import os
- import time
- 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)))
- #请求头
- headers = {
- 'Authorization': 'APPCODE 自己的appcode', # 阿里云官网上创建实例
- 'Content-Type': 'application/json; charset=UTF-8'
- }
- def posturl(url,data={}): # data为第一张的默认参数, data1为第二张的默认参数
- try:
- params = json.dumps(dict).encode(encoding='UTF8')
- req = urllib.request.Request(url, params, headers)
- r = urllib.request.urlopen(req)
- html = r.read()
- r.close()
- return html.decode("utf8")
- except urllib.error.HTTPError as e:
- print(e.code)
- print(e.read().decode("utf8"))
- time.sleep(1)
- if __name__=="__main__":
- url_request = "https://ocrapi-document.taobao.com/ocrservice/document"
- outfile = 'ali.txt'
- outdir = '压缩图片的存储位置'
- if path.exists(outfile):
- os.remove(outfile)
- if not path.exists(outdir):
- os.mkdir(outdir)
- print('开始进行图片压缩....')
- for picfile in glob.glob(r"picture\*"): # 图片的存储位置
- convertimg(picfile, outdir)
- print('开始进行图片识别....')
- for picfile in glob.glob("outdir\*"):
- filename = path.basename(picfile)
- print('正在识别%s' % filename)
- with open(picfile, 'rb') as f: # 以二进制读取本地图片
- data = f.read()
- encodestr = str(base64.b64encode(data), 'utf-8')
- dict = {'img': encodestr}
- html = posturl(url_request, data=dict)
- os.remove(picfile)
- html1 = json.loads(html)
- # print(html)
- # print(type(html))
- lst = []
- # print(html1['prism_wordsInfo'])
- for i in html1['prism_wordsInfo']:
- if i.get('pos'):
- i.pop('pos')
- for k, v in i.items():
- lst.append(v)
- # print(lst)
- str1 = ','.join(lst)
- print(str1)
- os.removedirs(outdir)
以下仅为个人意见:
我主要考虑的是有道和阿里:在准确度上差别不是很大,阿里稍微准确一丢丢
价格方面阿里是有道的两倍左右(阿里的为高精度文字识别)
速度上阿里更快一些。
如果你最近在考虑OCR的问题,请进来~~~的更多相关文章
- 如果你的unordered_map头文件报错请看这里
请将include<unordered_map>头文件换成下面代码 #if(__cplusplus == 201103L) #include <unordered_map> # ...
- 谈谈书本《c#物联网程序设计基础》中的技术瑕疵,如果你将要读本书,请进来看看!
今天去书店看到一本名为<c#物联网程序设计基础>的书,对物联网感兴趣的我抓起来就看,书中的项目都是上位机开发项目,较简单,如果物联网开发只是这样,看起来我做物联网开发也是绰绰有余.这边书我 ...
- 阿里巴巴-OS事业群-OS手机事业部-系统服务部门招聘Java开发工程师,有意者请进来
我是阿里巴巴-OS事业群-OS手机事业部-系统服务部的开发工程师,正在招聘Java开发工程师. 以下是职位描述: 岗位名称:Java开发工程师 招聘人数:5人 生效日期:2014-03-12 结束日期 ...
- 说说我在项目中为什么不用实体框架,如果说我在诋毁你所爱的EF,请进来.
1.坑多. 这一点没有人会否定.当然你可以说你很牛,但事实不会因为你牛就可以说不存在.从博客园中的博问中大家关于EF的提问量就问题的怪异程度就可以看出来. 1.Entity Framework 查询历 ...
- C\C++各路高手以及操作系统专家请进来杀死这个进程
通常情况下编写一个程序,能够点击关闭button正常结束程序,也能够使用任务管理器结束任务,还能够使用taskkill等命令杀死进程,实在都不行也能够直接重新启动计算机. 可是,这些方法真的都管用吗? ...
- 【MongoDB】从入门到精通mongdb系列学习宝典,想学mongodb小伙伴请进来
最近一段时间在学习MongoDB,在学习过程中总共编写了四十余篇博客.从mongodb软件下载到分片集群的搭建. 从理论讲解到实例练习.现在把所有博客的内容做个简单目录,方便阅读的小伙伴查询. 一. ...
- Postman SMTP 存在跨站脚本(XSS)漏洞,请换用Post SMTP Mailer/Email Log
Postman SMTP 是一个安装量超过10W的WordPress插件,但是已经2年多没有更新,2017年6月29日,被发现存在跨站脚本(XSS)漏洞(查看详情),并且作者一直没有更新,所以被从Wo ...
- 毕业N年后,请不要像我一样被档案烦死
目录 一. 提醒大学生:深刻重视档案,避免以后麻烦! 二.说说我因为档案造成的烦心事! 三.说说档案这档子事: 四.档案如此重要,为什么有些人却成了弃档族? 五.档案该怎么操作才能不当"弃档 ...
- [转自老马的文章]用MODI OCR 21种语言
作者:马健邮箱:stronghorse_mj@hotmail.com发布:2007.12.08更新:2012.07.09按照<MODI中的OCR模块>一文相关内容进行修订2012.07.0 ...
随机推荐
- XAMPP非本地访问被拒绝解决办法
问题场景: 本机搭建一个apache服务器,正常访问XAMPP目录下的页面. 手机接入同一wifi,以电脑ip方式访问该目录下的页面:提示:Access Denied Access to the re ...
- nignx reload的时候报错invalid PID number
第一种思路是从PID号着手,提示无效PID号时nginx.pid文件为空,将进程的PID号追加到空的nginx.pid上,问题解决: 还有问题,请参考:https://www.cnblogs.com/ ...
- Linux的进程与服务(一)
启动的配置文件/etc/inittab,修改完配置文件以后 init q立即生效 # Default runlevel. The runlevels used by RHS are: # - halt ...
- (转)centos liveCD liveDVD netinstall minimal DVD1 DVD2 版本区别
LiveCD 和 LiveDVD 是可以直接光盘运行系统,但不能安装,两者差别在于容量大小,dvd包含的软件要多一些. netinstall 是用于网络安装和系统救援的镜像文件. minimal 这个 ...
- type="submit" 和type="button"
今天,小菜鸟又遇到一个问题,当不小心在页面输入框回车一下,结果莫名的页面发出了一个请求. 把问题定位在一个button上,代码是这样写的<button class="btn btn-d ...
- 第二周leetcode
4/4 这周莫名得忙,一天是做编译,一天是做模式识别作业,(一天刷魔兽皮肤),周末玩了两天,总的来说还是松懈了,大概只做了两天的leetcode,刷了10道题,羞愧羞愧. 决定每次把代码附上在这个总结 ...
- 配置IIS Express,支持JSON
方法有2种: 1. 命令行 a. cd "iis express的安装目录" 例如:cd C:\Program Files (x86)\IIS Express b. appcmd ...
- Linux Socket - 内核非阻塞功能
select 函数 int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval*tim ...
- 【转载】45个设计师们不常见的html5和css3漂亮模板
对于Web开发人员来说,当他们需要创建一个非常时尚和新潮的CSS3和HTML5网站时需要非常专业的水准.html5和css3的结合能够做出非同寻常的网站效果..所以,今天,我推荐给大家45个免费的时尚 ...
- Hadoop各个组件与端口
组件 Daemon 端口 配置 说明 HDFS DataNode 50010 dfs.datanode.address datanode服务端口,用于数据传输 HDFS DataNode 50075 ...