|
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import base64
- from os.path import exists
- from tkinter import Tk
- from tkinter.filedialog import askopenfilename
- from urllib.parse import urlencode
- import requests
- def Dialog_box():
- root = Tk()
- root.withdraw()
- filetypes = [(
- '图片文件(*.jpg;*.jpeg;*.gif;*.png;*.bmp)', '*.jpg;*.jpeg;*.gif;*.png;*.bmp'),
- ('所有文件(*.*)', '*.*')]
- file_path = askopenfilename(filetypes=filetypes)
- return file_path
- def Read_file(file_path):
- with open(file_path, mode='rb') as file_object:
- contents = file_object.read()
- return contents.rstrip()
- def Baidu_ocr(binary):
- url = "https://aip.baidubce.com/oauth/2.0/token?"
- data = {
- "grant_type": "client_credentials",
- "client_id": "", # 必填
- "client_secret": "" # 必填
- }
- url += urlencode(data)
- response = requests.get(url=url).json()
- access_token = response['access_token']
- url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + access_token
- post_data = {
- "image": base64.b64encode(binary),
- "url": "",
- "language_type": "CHN_ENG",
- "detect_direction": "false",
- "detect_language": "false",
- "probability": "false"
- }
- response = requests.post(url=url, data=post_data).json()
- num = response['words_result_num']
- result = ''
- for i in range(num):
- result += response['words_result'][i]['words'] + "\r\n"
- return result
- if __name__ == "__main__":
- file_path = Dialog_box()
- if exists(file_path):
- data = Read_file(file_path)
- print(Baidu_ocr(data))
复制代码
|
|