当前位置:   article > 正文

Python 基于百度 AI 开放平台 图片识别 封装使用 (验证码识别)_免费图片验证平台

免费图片验证平台

百度AI开放平台链接:https://ai.baidu.com/
图片识别API文档链接:https://ai.baidu.com/docs#/OCR-API-GeneralBasic/db0895e7
应用注册链接:https://console.bce.baidu.com/ai/?fromai=1#/ai/ocr/app/list

 

  1. #coding=utf-8
  2. __author__ = 'kai.yang'
  3. __date__ = '2019/10/16 22:53'
  4. import requests
  5. import json
  6. import base64
  7. import time
  8. class baiduCode(object):
  9. @classmethod
  10. def get_token(cls):
  11. """
  12. 当前函数只用调用一次,用来获取当前账号的token
  13. :return:
  14. """
  15. # 标记当前精准识别是否使用完
  16. cls.curr_url = ''
  17. cls.basic_flag = False
  18. cls.max_num = 5
  19. cls.login_url = 'https://aip.baidubce.com/oauth/2.0/token'
  20. cls.login_params = {
  21. 'grant_type': 'client_credentials',
  22. 'client_id': 'xxxxxxxxxxxxxxxxxxxxxxxx',
  23. 'client_secret': 'xxxxxxxxxxxxxxxxxxxxxxxx'
  24. }
  25. cls.headers = {
  26. 'Content-Type': 'application/x-www-form-urlencoded'
  27. }
  28. response = requests.post(cls.login_url, params=cls.login_params, headers=cls.headers)
  29. result = json.loads(response.text)
  30. cls.token = result['access_token']
  31. @classmethod
  32. def get_code(cls, path='', url=''):
  33. cls.num = 0
  34. # 普通图片识别的请求链接,正确率50%(测试了500张图片)
  35. cls.general_basic_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic'
  36. # 高精度图片识别的请求链接,正确率80%(测试了500张图片)
  37. cls.accurate_basic_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic'
  38. if path:
  39. if not cls.basic_flag:
  40. cls.curr_url = cls.accurate_basic_url
  41. with open(path, 'rb') as f:
  42. base64_data = base64.b64encode(f.read())
  43. base = base64_data.decode()
  44. cls.basic_params = {
  45. 'image': base,
  46. 'access_token': cls.token
  47. }
  48. elif url:
  49. cls.curr_url = cls.general_basic_url
  50. cls.basic_params = {
  51. 'url': url,
  52. 'access_token': cls.token
  53. }
  54. else:
  55. raise ValueError('当前path和url参数均错误')
  56. time.sleep(0.5)
  57. while True:
  58. try:
  59. response = requests.post(cls.curr_url, params=cls.basic_params, headers=cls.headers)
  60. result = json.loads(response.text)
  61. # 判断当前精准识别是否被使用完
  62. try:
  63. result['words_result']
  64. except Exception as e:
  65. print(e)
  66. cls.num += 1
  67. if cls.num > cls.max_num:
  68. raise ValueError('当前尝试的错误次数超过%d次,请重新调用'%(cls.max_num))
  69. cls.basic_flag = True
  70. cls.curr_url = cls.general_basic_url
  71. continue
  72. code = result['words_result'][0]['words']
  73. return code
  74. break
  75. except Exception as e:
  76. print('error: ', e)
  77. cls.num += 1
  78. if cls.num > cls.max_num:
  79. raise ValueError('当前尝试的错误次数超过%d次,请重新调用'%(cls.max_num))
  80. if __name__ == '__main__':
  81. # 当前获取token的函数只用调用一次
  82. baiduCode.get_token()
  83. # 直接传图片的地址就好
  84. code = baiduCode.get_code(path='image/22.png')
  85. print(code)
  86. code = baiduCode.get_code(url='https://www.showapi.com/auth/checkCode?t=1571279918029')
  87. print(code)

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/703393
推荐阅读
相关标签
  

闽ICP备14008679号