Python-图片文字识别
百度AI接口(手写文字识别):https://ai.baidu.com/docs#/OCR-API/9ef46660
实现效果:
步骤一:接入接口
进入上述网站申请账号,然后运行相关代码,获取 access_token 即算完成(由于百度json每30天更新一次,故代码中进行日期更新了的,如何获取accss_token也可见代码)
步骤二:功能介绍:用户输入的图片路径可为网络上的url,也可为本机上的地址,为图省事,图片名称为 ValidateCode.jpg ,由于本人接入的的百度AI接口的手写文字识别,所以一般的验证码应该都可以通过,如果想加入其它功能,那么返回json数据就会有所改变,具体可以见API接口,本人是为了简化理解百度文档介绍
1 request.urlretrieve(imagepath, \’ValidateCode.jpg\’) # 下载图片
更新access_json:因为百度API规定:30天更新一次,所以我就把时间提前了。(别乱搞我的密钥呀,我也是为了分享呀QAQ)
2 def accesjson(): 3 flag = 0 4 fromtime = 1546061002 #起始时间 5 nowtime = int(time.time()) 6 7 #2592000恰好为30天,故提前 8 if nowtime - fromtime > 2000000: 9 flag = 1 10 gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1) 11 # client_id 为官网获取的AK, client_secret 为官网获取的SK 12 host = \'https://aip.baidubce.com/oauth/2.0/token?grant_\' \ 13 \'type=client_credentials&client_id=Ooj730ZD0Rm7E1dmcPwoZX9s&client_secret=dr5T1icZGqK8ZFyTr4wi2AWbtNKMIsNd\' 14 req = request.Request(host) 15 response = request.urlopen(req, context=gcontext).read().decode(\'UTF-8\') 16 result = json.loads(response) 17 if flag == 1: 18 return result 19 else: 20 return None
图片正式识别:注意,接入功能不一样,放回json数据不一样,具体看返回json就明白了
1 #返回图片验证码 2 def vercode(): 3 f = open(\'ValidateCode.jpg\', \'rb\') 4 img = base64.b64encode(f.read()) 5 #不同百度API接口不一样,传递参数不一样,返回json也不一样 6 host = \'https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting\' 7 headers = { 8 \'Content-Type\': \'application/x-www-form-urlencoded\' 9 } 10 #更换json 11 if accesjson() == None: 12 access_token = \'24.18591b2e4c97956e0f830db9f66e5373.2592000.1548646630.282335-15301065\' 13 else: 14 access_token = accesjson() 15 print(\'已更换最新json,欢迎继续使用!\') 16 host = host + \'?access_token=\' + access_token 17 18 data = {} 19 data[\'access_token\'] = access_token 20 data[\'image\'] = img 21 res = requests.post(url=host, headers=headers, data=data) 22 req = res.json() 23 return req[\'words_result\'][0][\'words\']
完整代码:目前可实现的功能就是网络上面的文字图片识别,或本机图片识别(和之前的抖音图片加载类似。)
拓展:https://ai.qq.com/ (啥B腾讯的API接口,全是PHP,用都知道怎么用,凉凉。)
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # @Time : 2018/12/29 10:48 4 # @Author : Empirefree 5 # @File : 17-2-验证码.py 6 # @Software: PyCharm Community Edition 7 8 import base64 9 import requests 10 from urllib import request 11 import os 12 import ssl 13 import json 14 import time 15 import re 16 17 def IsHttp(imagepath): 18 if re.search(\'http\', imagepath) != None: 19 return 1 20 else: 21 return 0 22 23 #下载验证码 24 def downloadpic(imagepath): 25 # imagepath = "http://210.42.38.26:84/jwc_glxt/ValidateCode.aspx" 26 if IsHttp(imagepath): 27 request.urlretrieve(imagepath, \'ValidateCode.jpg\') # 下载图片 28 29 print(os.path.abspath(\'ValidateCode.jpg\')) 30 31 #百度限制,每30天更换一次access_json 32 def accesjson(): 33 flag = 0 34 fromtime = 1546061002 #起始时间 35 nowtime = int(time.time()) 36 37 #2592000恰好为30天,故提前 38 if nowtime - fromtime > 2000000: 39 flag = 1 40 gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1) 41 # client_id 为官网获取的AK, client_secret 为官网获取的SK 42 host = \'https://aip.baidubce.com/oauth/2.0/token?grant_\' \ 43 \'type=client_credentials&client_id=Ooj730ZD0Rm7E1dmcPwoZX9s&client_secret=dr5T1icZGqK8ZFyTr4wi2AWbtNKMIsNd\' 44 req = request.Request(host) 45 response = request.urlopen(req, context=gcontext).read().decode(\'UTF-8\') 46 result = json.loads(response) 47 if flag == 1: 48 return result 49 else: 50 return None 51 52 #返回图片验证码 53 def vercode(): 54 f = open(\'ValidateCode.jpg\', \'rb\') 55 img = base64.b64encode(f.read()) 56 #不同百度API接口不一样,传递参数不一样,返回json也不一样 57 host = \'https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting\' 58 headers = { 59 \'Content-Type\': \'application/x-www-form-urlencoded\' 60 } 61 #更换json 62 if accesjson() == None: 63 access_token = \'24.18591b2e4c97956e0f830db9f66e5373.2592000.1548646630.282335-15301065\' 64 else: 65 access_token = accesjson() 66 print(\'已更换最新json,欢迎继续使用!\') 67 host = host + \'?access_token=\' + access_token 68 69 data = {} 70 data[\'access_token\'] = access_token 71 data[\'image\'] = img 72 res = requests.post(url=host, headers=headers, data=data) 73 req = res.json() 74 return req[\'words_result\'][0][\'words\'] 75 76 def checkcode(): 77 imagepath = input(\'请输入您的图片路径: \') 78 downloadpic(imagepath) 79 str = vercode() 80 return str 81 82 if __name__ == \'__main__\': 83 84 str = checkcode() 85 print(str)
View Code