- 在线时间
- 7 小时
- 最后登录
- 2021-1-20
- 注册时间
- 2021-1-9
- 听众数
- 3
- 收听数
- 0
- 能力
- 0 分
- 体力
- 23 点
- 威望
- 0 点
- 阅读权限
- 20
- 积分
- 10
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 7
- 主题
- 7
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   5.26% 该用户从未签到
- 自我介绍
- 我本名为我,那就是我
 |
" y% U: f; _" B+ Z3 y8 v$ s. G - #!/usr/bin/python
- % {( r\" D& @$ v% n1 C# -*- coding: utf-8 -*-0 C\" ?\" q, U- U( ~0 b, x
- : z/ j4 z4 z2 c0 H6 yimport base64
- ; H( r0 y$ ?. rfrom os.path import exists
- + f/ `6 Q7 D9 O. Ufrom tkinter import Tk
- ( s+ o! s, X9 D5 \1 D9 P& E* R3 Xfrom tkinter.filedialog import askopenfilename
- 6 h\" _7 q8 j; M4 i8 _0 @from urllib.parse import urlencode
- . h0 s: I6 W0 _\" _4 E( C* |8 w b
- ; ]! Z* N2 _4 timport requests
- 4 s8 b7 W0 `6 i! [3 |+ P( D, `+ ?! k; h: w* x9 R l
- % A6 j( D) `$ j7 C
- def Dialog_box():
- / G6 C/ ~7 ~+ ~' K% |% f root = Tk()
- & ~1 j, f6 M% U, T# O. o3 x root.withdraw()' z. Y2 d. @6 B2 J0 b v
- 0 F) j7 s. N. C4 b filetypes = [(; @ m2 ^& \( O6 [: s
- '图片文件(*.jpg;*.jpeg;*.gif;*.png;*.bmp)', '*.jpg;*.jpeg;*.gif;*.png;*.bmp'),* P, @, H$ V5 Q- J
- ('所有文件(*.*)', '*.*')]
- $ `* |8 N0 i+ S; g: _) m- B- X; j7 d3 F7 |\" |5 M$ f ?' a) E* ~
- file_path = askopenfilename(filetypes=filetypes)
- # n& @5 @1 t; c return file_path$ l, s1 }- p9 s. t, o5 X
- 3 v; e8 m$ ]0 g H& L4 q3 }
- b7 y, h; A\" H6 c5 @
- def Read_file(file_path):
- ! a; T& [8 y P6 ~ with open(file_path, mode='rb') as file_object:
- % c/ j8 J* o/ z- x2 o contents = file_object.read(); ?9 s* L c6 {( `( x
- return contents.rstrip()
- : _2 ^+ l7 v }1 H2 A' |7 P. b$ t% ]/ p2 l4 o# c; R. J8 a
- & j: w0 {, F$ |7 F/ fdef Baidu_ocr(binary):
- ; o* v9 G4 j% h+ K0 k url = "https://aip.baidubce.com/oauth/2.0/token?"- v* W& o0 k+ L# j' n0 \+ v
- data = {& B/ V8 n$ m& P: Y9 K( [\" U+ H
- "grant_type": "client_credentials",- ~8 Z5 z( E) c5 `
- "client_id": "", # 必填
- t5 h ?2 C/ A, Y2 P' X2 o "client_secret": "" # 必填
- R Z+ }3 s% @# h1 L8 } }
- ! @: I3 V' ^5 V5 b9 U& Q url += urlencode(data)
- 8 X n0 M0 h% r2 _% \/ Z; U& Q3 V response = requests.get(url=url).json()
- 6 A3 O: } ~% E3 V2 L9 @0 c access_token = response['access_token']
- 7 y6 c( |8 b2 C7 f$ w7 C8 d% u
- * v- w7 s: A8 Y+ T1 r: x1 y url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + access_token
- ! b0 `( J+ z; ^) l1 ~, p post_data = {: n7 O/ {. i6 u
- "image": base64.b64encode(binary),
- / [, c\" C9 Z$ j/ _7 Q* ~% i, f "url": "",
- & \; S7 Y7 B7 Q) j0 ] ^ "language_type": "CHN_ENG",4 D E+ q! i3 f: _$ z
- "detect_direction": "false",
- ( a6 e: d+ v. s "detect_language": "false",! d1 h# c# \' r z2 F( M5 W
- "probability": "false"
- + c4 O\" w\" N5 E! C9 u/ b5 b }6 m1 M% W q; }0 d; s, U
- response = requests.post(url=url, data=post_data).json(), ~( N/ V4 m: \. @9 c) s N K
- num = response['words_result_num']. X( z4 S5 n1 O3 d& h\" A1 ^/ I$ n
- result = ''\" a: d$ w- n E6 `3 h
- for i in range(num):0 A+ B\" e) F6 v* `. G/ N. j/ c\" E) }
- result += response['words_result'][ i]['words'] + "\r\n"* S1 q8 W6 _) K% g- M+ O9 W0 p
- return result
- , |* X9 L. C: T1 h- g0 n2 c
- ( v/ `0 w7 v* r( N2 |: ]* T0 F. T8 k: z* |+ V
- if __name__ == "__main__":
- * E2 J! G- z' z3 \ I i! t9 R7 Q file_path = Dialog_box()
- : a7 w7 v' P5 d( [& K* q! X if exists(file_path):
- 1 z7 Z, V( U* U& t, |/ \7 ~ data = Read_file(file_path)
- 8 e: y$ A Q7 O9 j print(Baidu_ocr(data))
- ' x( P* G `1 s8 W; g# H/ T0 x
- 7 \3 L2 R. a- E; W9 B- h$ C9 V
8 j5 p y, N% d5 x1 S& l) Y* m9 U0 H
# Z7 g+ b2 E; ?6 R& Q7 m
2 M7 ~" j5 U7 ?# d( |. PPython交流群:1047602540 : G4 r8 U% _" `
|
zan
|