码迷,mamicode.com
首页 > 其他好文 > 详细

记一次百度OCR的使用

时间:2020-01-30 17:03:10      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:word文档   def   turn   地图   sorted   date   evel   str   本地图片   


title: 记一次百度OCR的使用
copyright: true
tags: python
abbrlink: 8d4a5af0
date: 2018-11-12 11:04:27
---

恰巧用到了OCR批量识别,鉴于准确度没有使用在本地训练的TensorFlow-OCR,而是选择了百度OCR,可选的方式多种多样,比如Google文字识别,腾讯OCR等等,不一一列举

很简单的demo,参照开发文档

http://ai.baidu.com/docs#/OCR-Python-SDK/80d64770

技术图片

先去控制台注册一个开发者账号,并创建一个文字识别应用,在管理应用中可以看到AppID等相关信息

安装SDK pip install baidu-aip

新建一个python文件

from aip import AipOcr
from glob import glob
from docx import Document
import os
import json
""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
root_path = os.getcwd() #获取当前路径
document = Document() #初始化word文档
num = 0
""" 读取图片 """
def get_file_content(FilePath):
    with open(FilePath, 'rb') as fp:
        return fp.read()

""" 调用通用文字识别, 图片参数为本地图片 """
def result(image_file,image):
    """ 如果有可选参数 """
    options = {}
    #options["language_type"] = "CHN_ENG"#
    options["detect_direction"] = "true"
    #options["detect_language"] = "true"#
    options["probability"] = "true"
    #m = client.basicGeneral(image);
    """带参数调用通用文字识别, 图片参数为本地图片 """
    #client.basicGeneral(image,options)
    #url = "https//www.x.com/sample.jpg"
    #调用通用文字识别, 图片参数为远程url图片
    #client.basicGeneralUrl(url);
    #普通
    #res = client.basicGeneral(image,options)
    #高精度
    res = client.basicAccurate(image,options)
    global num
    num = num + 1
    print("这是第"+str(num)+"个文件")
    print(res)
    if 'error_code' in res:
        print("有错误")
        File("出错了,第"+image_file.split("/")[-1]+'个','/error')
    else:
        if(num>0):
            document.add_page_break()
            fname = image_file.split("/")[-1]
            document.add_heading(fname,level=2) #添加二级标题
        os.remove(image_file)
        for item in res['words_result']:
            print(item['words'])
            File(item['words'],image_file)
            write_word(item['words'])

'''文件写入'''
def File(string,name):
    with open(root_path+'/demo/text/'+name.split("/")[-1]+'.txt','a+') as f:
        f.write('\n'+string)

def write_word(string):
        document.add_paragraph(string)
        document.save(root_path+'/demo/text/'+'test.docx')
"""指定目录下所有文件路径"""
image_files = glob(root_path+'/demo/image/*.*')
#image_files = glob('./image/*.*')
for image_file in sorted(image_files):
    print(image_file)
    image = get_file_content(image_file)
    result(image_file,image)

支持导出到word文档,可以在第51,52行中添加/修改将结果写入的文件,7,8,9改成自己控制台的内容才可以使用,相应目录都写在文件中,只需稍微修改

自动读取image文件夹下的所有文件,识别后将结果保存在text目录下

目录树

.
├── BaiduOcr.py
├── error.txt
├── image
│?? └── 028.jpg
└── text

记一次百度OCR的使用

标签:word文档   def   turn   地图   sorted   date   evel   str   本地图片   

原文地址:https://www.cnblogs.com/tsvico/p/12242999.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!