百度api实现文字识别

#coding=utf-8
import urllib,sys
import ssl
import json
import base64
import requests
import cv2
import os
import numpy as np
import datetime
# client_id 为官网获取的AK, client_secret 为官网获取的SK
def get_token():
	# host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'
	AK = "oYYb7zDqH2xoRMbFu85WSIuy"
	CK = "CfuG0wmlvoKoPwQXOU3je2yQ7qbCB36V"
	host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+AK+'&client_secret='+CK
	request = urllib.request.Request(host)
	request.add_header('Content-Type', 'application/json; charset=UTF-8')
	response = urllib.request.urlopen(request)
	content = response.read()
	to_json = json.loads(content)
	token = to_json['access_token']
	return token

token = get_token()

# 当前路径下的JPG
path = os.getcwd()
pathlist = os.listdir(path)
if "errorfile" not in pathlist:
	os.system("mkdir errorfile")
for p in pathlist:
	if ("JPG" in p and "test" not in p )or "jpg" in p :
		print ("start to detect %s" % (p))		
		filename = p
		img = cv2.imread(filename, -1)  
		height, width = img.shape[:2]  
		if height<width:
			img =np.rot90(img,-1)
		cropped = img[512:1360, 512:1360]  # 裁剪坐标为[y0:y1, x0:x1]
		cv2.imwrite("test_out"+filename+".jpg", cropped)
		with open("test_out"+filename+".jpg",'rb') as f:
			img = base64.b64encode(f.read())
			host = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic'
			headers={
			   'Content-Type':'application/x-www-form-urlencoded'
			}
			access_token = token
			host=host+'?access_token='+access_token
			data={}
			data['access_token']=access_token
			data['image'] =img
			res = requests.post(url=host,headers=headers,data=data)
			req=res.json()
			ok = 0
			os.system("rm test_out%s.jpg" % (filename))
			with open("get.txt","a") as gg:
				print (filename,datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),file=gg)
			for mydict in req['words_result']:
				for (k,v) in mydict.items():	
					with open("get.txt","a") as gg:
						print (k,v,file=gg)
					# to modify
					if "ZSTU" in v:  #主要改的就是这个
					# if "CollectSN" in v or "CollectS" in v or "Collects" in v:
						index = v.find("ZSTU")
						# index = v.find(":")
						# while v[index].isdigit()==0 and v[index].isalpha()==0: #既不是数字也不是字母
						# 	index += 1
						ok = 1
						with open("done.txt","a") as fff:
							print (filename,v[index:],file=fff)
			if ok ==0 :
				os.system("mv %s errorfile/" % (filename))
				with open("error.log","a") as ff:
					print (filename,file=ff)

python3

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值