0 Z/ y2 N* v6 L% w3 J& Y. S0 j; o4 ~" s2 K
def Read_file(file_path):, c, X, T; p0 c. V
with open(file_path, mode='rb') as file_object: , A W9 w9 n. h+ y contents = file_object.read()/ L+ N$ B8 f- f
return contents.rstrip(): u+ I- O6 L; y( W0 [5 r+ G
. E8 C2 n0 R+ K: F8 a 3 Y! @1 Q+ z% q( {6 Odef Baidu_ocr(binary):1 `# L* L# }4 u( ~) X& u) y; |
url = "https://aip.baidubce.com/oauth/2.0/token?"! X1 t2 K5 m2 O9 `
data = {9 S/ A |. E( I' E- G# F' v
"grant_type": "client_credentials",1 ]6 U5 V1 K2 M) [
"client_id": "", # 必填 - D% k- r$ O8 Z2 V "client_secret": "" # 必填 0 p' j& O" ]% G3 E& q( H }. p) T3 Q# a1 H9 }
url += urlencode(data) 4 n5 C! {! `. A, V/ D: m7 Y$ H( S# y response = requests.get(url=url).json()6 `* }# Y& g5 H3 g
access_token = response['access_token']' A+ H' ~; y. X
& c3 N8 K' `4 N
url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + access_token5 M+ _% T1 ?1 O6 G0 m: D0 e
post_data = {7 }0 x# r& t8 I8 i2 B. }
"image": base64.b64encode(binary),7 Y3 h% W ~ T9 Y0 S
"url": "", " g' U, j5 }8 l% m$ k7 I, ` "language_type": "CHN_ENG",( c( k, n+ B" n7 e' b; }" c* j1 u5 S
"detect_direction": "false", " a( Y8 h" [" r6 S5 Y5 H0 A6 a "detect_language": "false",4 I4 Y) L8 ]2 ?- z8 J
"probability": "false" . C8 J) j" p6 b3 T } % N/ A) f5 [% b+ S7 o, n response = requests.post(url=url, data=post_data).json()2 b0 Y m& W% C0 J; m* y: _" K" \2 x
num = response['words_result_num']( ] P1 v- J8 C2 U. Q% O
result = ''5 O3 ^' i5 r( E* ?1 X
for i in range(num): ) v7 X' m+ \4 _" j7 J result += response['words_result'][ i]['words'] + "\r\n" 4 l9 E& F3 q" d return result 5 E* N0 y! t: f1 h- h # D! j8 }: S1 J, m- B0 z! O 4 R4 l( [' F3 `" |+ K: o8 uif __name__ == "__main__":: b, S/ D9 @$ {. P2 \
file_path = Dialog_box() 9 h# N( ]: W1 [ if exists(file_path): ) W# Q/ G: C& J8 n) b. A data = Read_file(file_path)! K; p$ H1 Y8 z' M/ N1 m. ]! \0 @
print(Baidu_ocr(data))2 [7 `& r; t4 p& I; R
# c! D9 y; B$ v- Q- T J[/code] G% o1 W; P' I( B2 u$ o
6 O9 y; G9 {: e( h5 F$ r. ?3 y. \% n ' u, B8 ?- d- j2 q* @6 [