本文主要是各类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的问题,请进来~~~的更多相关文章

  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. DSOframer 的简单介绍和资源整理(2015-09-02重新整理)

    DSOframer 是微软提供一款开源的用于在线编辑 Word. Excel .PowerPoint 的 ActiveX 控件.国内很多著名的 OA 中间件,电子印章,签名留痕等大多数是依此改进而来的 ...

  2. Resolving multicopy duplications de novo using polyploid phasing 用多倍体相位法解决多拷贝复制的新问题

    抽象.虽然单分子测序系统的兴起已经实现组装复杂地区的能力空前提高在基因组中,基因组中的长节段重复仍然是装配中具有挑战性的前沿. 分段重复同时具有丰富的基因并且倾向于大的结构重排,使得它们的序列的分辨率 ...

  3. 洛谷 P1260 工程规划(差分约束)

    题目描述 造一幢大楼是一项艰巨的工程,它是由n个子任务构成的,给它们分别编号1,2,…,n(5≤n≤1000).由于对一些任务的起始条件有着严格的限制,所以每个任务的起始时间T1,T2,…,Tn并不是 ...

  4. Dom4j入门

    一.Dom4j API生成xml文件 @Test public void bulidXmlByDom4j(){ //创建document对象 Document document = DocumentH ...

  5. String 、 StringBuffer 和 StringBuilder

    StringBuffer (一个线程安全的可变字符串序列,用于多线程) A thread-safe, mutable sequence of characters. StringBuilder (可变 ...

  6. handsontable-cell type

    在单元格中呈现自定义的元素:不能使用html元素 var data = [ { title: "<a href='http://www.amazon.com/Professional- ...

  7. Asp.NetCore MVC Web 应用

    Asp.NetCore MVC 与 普通的MVC 基本一致, 只是代码结构稍有改动 一.创建项目 1. 2. 3. 项目结构 二. 构建数据模型 1. Startup类中配置EF Core MySql ...

  8. cocos2dx常见场景切换动画(转)

    本文转载自:http://www.cnblogs.com/linux-ios/archive/2013/04/09/3010779.html bool HelloWorld::init() { /// ...

  9. [转载]利用近场探头和频谱仪查找EMI辐射问题

    原文链接 http://www.pesmatrix.com/news/html/?412.html 电磁兼容性(Electromagnetic Compatibility,简称EMC)是指设备或系统在 ...

  10. Linux带有时间控制的多进程bash脚本

    目标 以可控制的多进程执行,达到最大执行时长后停止脚本. 思路 1.产生fifo管道,并预填充n个值(与并发数相等) 2.记录脚本本身PID并启动计时器进程(计时终止后杀脚本本身PID) 3.并发执行 ...