精易论坛
标题:
Python-百docr识别
[打印本页]
作者:
大司命
时间:
2020-12-11 10:59
标题:
Python-百docr识别
#!/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))
复制代码
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