百度 orc识别图片转文字

# -*- coding: utf-8 -*-
"""
Created on Tue May  1 13:16:01 2018

@author: Administrator
"""
######################################################################   
from aip import AipOcr
import json

# 定义常量
APP_ID = ' '
API_KEY = ' '
SECRET_KEY = ' '



######
i = 1
a = str(i)+'.txt'
f = open(a,'a')


# 初始化AipFace对象
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)

# 读取图片
for i in range(2):
    a = str(i+1)+'.png'
    filePath = a
    def get_file_content(filePath):
        with open(filePath, 'rb') as fp:
            return fp.read()
    
    # 定义参数变量
    options = {
      'detect_direction': 'true',
      'language_type': 'CHN_ENG',
    }
    
    # 调用通用文字识别接口
    result = aipOcr.basicGeneral(get_file_content(filePath), options)
    for item in result['words_result']:
        if('中考' in item['words'] and '20' in item['words']): ###题型
            f.close() 
            i = i+1
            a = str(i)+'.txt'
            f = open(a,'a')
            f.write(item['words'])
        else:
            f.write(item['words'])
            f.write("\n")#换行   
            print(item['words'])

猜你喜欢

转载自blog.csdn.net/qiqzhang/article/details/80157177
今日推荐