精易论坛

标题: Python-百docr识别 [打印本页]

作者: 大司命    时间: 2020-12-11 10:59
标题: Python-百docr识别
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-

  3. import base64
  4. from os.path import exists
  5. from tkinter import Tk
  6. from tkinter.filedialog import askopenfilename
  7. from urllib.parse import urlencode

  8. import requests


  9. def Dialog_box():
  10.     root = Tk()
  11.     root.withdraw()

  12.     filetypes = [(
  13.         '图片文件(*.jpg;*.jpeg;*.gif;*.png;*.bmp)', '*.jpg;*.jpeg;*.gif;*.png;*.bmp'),
  14.         ('所有文件(*.*)', '*.*')]

  15.     file_path = askopenfilename(filetypes=filetypes)
  16.     return file_path


  17. def Read_file(file_path):
  18.     with open(file_path, mode='rb') as file_object:
  19.         contents = file_object.read()
  20.         return contents.rstrip()


  21. def Baidu_ocr(binary):
  22.     url = "https://aip.baidubce.com/oauth/2.0/token?"
  23.     data = {
  24.         "grant_type": "client_credentials",
  25.         "client_id": "",  # 必填
  26.         "client_secret": ""  # 必填
  27.     }
  28.     url += urlencode(data)
  29.     response = requests.get(url=url).json()
  30.     access_token = response['access_token']

  31.     url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + access_token
  32.     post_data = {
  33.         "image": base64.b64encode(binary),
  34.         "url": "",
  35.         "language_type": "CHN_ENG",
  36.         "detect_direction": "false",
  37.         "detect_language": "false",
  38.         "probability": "false"
  39.     }
  40.     response = requests.post(url=url, data=post_data).json()
  41.     num = response['words_result_num']
  42.     result = ''
  43.     for i in range(num):
  44.         result += response['words_result'][i]['words'] + "\r\n"
  45.     return result


  46. if __name__ == "__main__":
  47.     file_path = Dialog_box()
  48.     if exists(file_path):
  49.         data = Read_file(file_path)
  50.         print(Baidu_ocr(data))
复制代码

Python-百度ocr识别
https://bbs.266.la/forum.php?mod=viewthread&tid=452
(出处: 派生社区)

作者: 用户被删除    时间: 2020-12-11 11:03
大佬 请教一下 以前2011年学过手机版的 塞班 大蟒蛇平台 现在学pc的是不是差不多




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4