- 在线时间
- 7 小时
- 最后登录
- 2021-1-20
- 注册时间
- 2021-1-9
- 听众数
- 3
- 收听数
- 0
- 能力
- 0 分
- 体力
- 23 点
- 威望
- 0 点
- 阅读权限
- 20
- 积分
- 10
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 7
- 主题
- 7
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   5.26% 该用户从未签到
- 自我介绍
- 我本名为我,那就是我
 |
; U4 m' K8 m, }( n - #!/usr/bin/python
- 7 O: E2 R% o- L; a4 r; o# -*- coding: utf-8 -*-
- + U. J8 v, _) T& x, a% R$ [5 r: ~3 V
- ( ]5 m2 Q0 b6 H1 i) Iimport base64
- - Q: v7 y! v; H8 C( yfrom os.path import exists1 X3 ^! R3 @) n! D! t& R( t
- from tkinter import Tk: W- M+ a( G8 N5 S, T: [
- from tkinter.filedialog import askopenfilename
- / U- m' K5 W: s# o: |1 Vfrom urllib.parse import urlencode
- - V) U# J' L1 Y\" M3 ]0 A) T
- 4 l0 L/ e1 R1 ?( D* I* bimport requests& A! k4 i+ M! [2 b1 L! s$ P
- ; k+ ?6 m, y& G# @( L
- 9 [# i3 z& k0 S. }3 `( H0 X
- def Dialog_box():
- 5 `5 M$ K: e9 V) F* \ root = Tk()% y+ ?: a# ^3 a+ i( E2 O\" R1 |
- root.withdraw()
- / X' }: t; I. Q; u7 _9 L' f2 t) N+ ]* D, j( @5 k\" ?* n3 ^
- filetypes = [(- @+ j( u2 i/ X, U* f8 L
- '图片文件(*.jpg;*.jpeg;*.gif;*.png;*.bmp)', '*.jpg;*.jpeg;*.gif;*.png;*.bmp'),
- 4 R2 p: Q( Y$ O\" y ('所有文件(*.*)', '*.*')]
- * h# Y. `9 S- K8 h. f3 H* i7 j
- - @: p* e3 `' Y\" t2 h& Y# O) @ file_path = askopenfilename(filetypes=filetypes)
- ; m0 _! a& n- [! ]3 Q- p return file_path
- 6 r. K! v5 N- [% z\" `% V
- 9 `5 T' C$ z% ~# j$ F
- 4 ?8 h. f) ?4 m& E+ h3 Fdef Read_file(file_path): V- N: P$ _4 V! ~- L7 g
- with open(file_path, mode='rb') as file_object:5 ?* [* c% X4 h. s8 t
- contents = file_object.read()1 P: T& C3 S& S
- return contents.rstrip()
- $ L. e* \7 J( s T: b: Q; u2 V
- 3 A( Z( A9 O. F1 ]6 T i$ I4 B# L' h9 U9 y
- def Baidu_ocr(binary):& \: o3 g1 \- x
- url = "https://aip.baidubce.com/oauth/2.0/token?"
- : x9 t6 \/ J9 l data = {
- % @3 T' D% W' w0 l "grant_type": "client_credentials",! E& S# u3 d6 k$ M
- "client_id": "", # 必填; N$ U8 ] A% E; }4 S\" d
- "client_secret": "" # 必填3 e0 W% q9 ^3 R9 L8 E. p
- }
- 4 i* f. B& ] c url += urlencode(data)1 Z u# w! a. W! \ N, _# a- c% y
- response = requests.get(url=url).json()9 R# A6 h1 y* q/ ?
- access_token = response['access_token']% L\" N7 r4 P' b5 _. B6 \; B
- 4 M* b: ?( j. \9 p& K. y# P\" X url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + access_token% X\" E4 Z+ j3 k$ T) Z
- post_data = {% Y* I9 E- u5 j\" l' e/ ?
- "image": base64.b64encode(binary),
- 3 ?9 Y& _! `6 ?% R f' ^) G "url": "",. M/ {; N2 h8 Z\" D7 h( i
- "language_type": "CHN_ENG",; G. R. Y, f8 L7 V: G
- "detect_direction": "false",# S+ u3 U' K2 w6 o/ s\" y
- "detect_language": "false",
- 3 _ P7 O% V h( [2 _ "probability": "false"
- $ T5 U7 z. G# h2 h% M }
- \" P5 l Q/ l/ m- O% B) Y response = requests.post(url=url, data=post_data).json()
- 6 B+ E1 b5 m\" H( i num = response['words_result_num']4 |/ f% E4 C8 j0 x- v
- result = ''* w) \; Q$ g7 K9 e
- for i in range(num):
- * t$ H- A. ]7 U$ h! y W# b( w result += response['words_result'][ i]['words'] + "\r\n"
- 0 S K$ |9 T% g6 y return result
- ; A; r$ f$ d3 R' \' b0 G% e+ V
- ' y' `$ g6 U v$ X+ U- D4 @) i* G @% p\" I9 `- ?+ F# y
- if __name__ == "__main__":
- ! Q8 n1 k5 i8 k file_path = Dialog_box()
- + }& l9 A/ f x/ O0 l if exists(file_path):
- ( `: H5 ~ m\" v6 C4 B, |7 Z# G data = Read_file(file_path)
- % n+ W( Z4 W! T# d- ?+ I. B, \ print(Baidu_ocr(data)): R9 \% Q* ?, q
- 9 p s+ i3 j+ g) B- _( h0 U
5 Z, w+ Z' A# E
) |2 q+ X! G+ x7 M3 h+ }2 _$ J, s
! ?- c3 r' S) @0 n* OPython交流群:1047602540 2 \/ r- |, S+ b, `% I0 ^
|
zan
|