百度云API接口文字识别代码
新建一个config.txt文件,按如下格式保存,Key需要去百度云API获取
Action Type:1
API Key:2EMIAkgqN8i**********
Secret Key:TZLWkQkiG41L7******
# encoding:utf-8
import os
import requests
import base64
import json
import pymysql
import mysql.connector
# 百度AI开放平台鉴权函数
def get_access_token(API):
url = 'https://aip.baidubce.com/oauth/2.0/token'
data = {
'grant_type': 'client_credentials', # 固定值
'client_id': API["API Key"], # 在开放平台注册后所建应用的API Key,填入config.txt的第一行
'client_secret': API["Secret Key"] # 所建应用的Secret Key填入config.txt的第二行
}
res = requests.post(url, data=data)
res = res.json()
access_token = res['access_token']
return access_token
#通用文字识别
def general_word(config):
#通用文字识别接口url
general_word_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
access_token = get_access_token(config)
lujing = '图片3.png'
image_names = lujing.split(',')
for i in image_names:
# 二进制方式打开图片文件
f = open(i, 'rb')
img = base64.b64encode(f.read())
params = {"image":img,
"language_type":"CHN_ENG"}
request_url = general_word_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
res = response.json()["words_result"]
file_name = i.split(".")[0]+".txt"
with open(file_name, 'w', encoding='utf-8') as f:
for j in res:
f.write(j["words"]+"\n")
#身份证识别
def idcard(config):
idcard_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"
access_token = get_access_token(config)
image_names = input("请输入需要识别的身份证图片(多张图片用英文,隔开):").split(",")
for i in image_names:
# 二进制方式打开图片文件
f = open(i, 'rb')
img = base64.b64encode(f.read())
params = {"id_card_side":"front","image":img}
request_url = idcard_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
res = response.json()["words_result"]
file_name = "菜鸟小白的学习分享_图片"+i.split(".")[0]+".txt"
with open(file_name, 'w', encoding='utf-8') as f:
f.write("住址:"+res["住址"]["words"]+"\n")
f.write("出生日期:" + res["出生"]["words"] + "\n")
f.write("姓名:" + res["姓名"]["words"] + "\n")
f.write("公民身份号码:" + res["公民身份号码"]["words"] + "\n")
f.write("性别:" + res["性别"]["words"] + "\n")
f.write("民族:" + res["民族"]["words"] + "\n")
#银行卡识别
def bankcard(config):
#银行卡识别接口url
general_word_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard"
access_token = get_access_token(config)
image_names = input("请输入需要识别的银行卡图片(多张图片用英文,隔开):").split(",")
for i in image_names:
# 二进制方式打开图片文件
f = open(i, 'rb')
img = base64.b64encode(f.read())
params = {"image":img,}
request_url = general_word_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
res = response.json()["result"]
file_name = "菜鸟小白的学习分享_图片"+i.split(".")[0]+".txt"
with open(file_name, 'w', encoding='utf-8') as f:
f.write("卡号:" + res["bank_card_number"] + "\n")
f.write("有效期:" + res["valid_date"] + "\n")
f.write("银行:" + res["bank_name"] + "\n")
if __name__ == '__main__':
config = {}
with open('config.txt', 'r') as f:
lines = f.read().splitlines()
for i in lines:
if i:
key, values = i.split(':')
config[key] = values
general_word(config)