- 在线时间
- 7 小时
- 最后登录
- 2021-1-20
- 注册时间
- 2021-1-9
- 听众数
- 3
- 收听数
- 0
- 能力
- 0 分
- 体力
- 23 点
- 威望
- 0 点
- 阅读权限
- 20
- 积分
- 10
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 7
- 主题
- 7
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   5.26% 该用户从未签到
- 自我介绍
- 我本名为我,那就是我
 |
6 ?+ x( H6 q* \8 v& ~
 - #!/usr/bin/python$ }' U7 c; J1 P7 r
- # -*- coding: utf-8 -*-1 P* o0 C0 l9 ?# B' \; Y7 d
- . W( v; f+ o/ m$ y1 R\" n
- import base64
- 1 i a+ G6 G2 ~\" x1 R8 L0 c8 J- hfrom os.path import exists
- # \: X' f% c0 Y3 u# e7 Zfrom tkinter import Tk, e* h; u8 h+ X- w; L5 {
- from tkinter.filedialog import askopenfilename
- # d* q; P# t& |: Q ^* Mfrom urllib.parse import urlencode
- ) e; n3 P6 N% b; k7 U1 h+ Q
- : Z; z+ @3 P6 \9 u# F& C t5 e& ~6 fimport requests& x# q- y# R% k7 L/ E% S3 |1 e4 U
- & r\" Y( q7 _7 f! L% q
- # s$ |6 a( h v& W
- def Dialog_box():
- & M( y- [& j2 h root = Tk()6 W1 L8 _1 a+ S6 D' ~! A+ Y& U
- root.withdraw(); W7 t: v& X- _8 ~
- C; p P h/ t6 I7 y filetypes = [(1 u\" ]7 `/ L# x6 ~1 s
- '图片文件(*.jpg;*.jpeg;*.gif;*.png;*.bmp)', '*.jpg;*.jpeg;*.gif;*.png;*.bmp'),. ?+ Y& p* y; [& w3 d' W
- ('所有文件(*.*)', '*.*')]
- # B+ s, l7 I; F5 \/ D! {: x' N/ p: J7 k( M) Y0 Z) @- I' ?- |\" e G
- file_path = askopenfilename(filetypes=filetypes), P$ ]+ l5 _! p$ U: f: D
- return file_path
- 6 z0 N F( y* f6 p. g [) F# }1 w/ M
- 7 f# x9 R$ t2 }0 D2 ?
- def Read_file(file_path):0 `/ J0 q5 [! D' v3 Y
- with open(file_path, mode='rb') as file_object:& M/ K9 u: U6 d\" V R
- contents = file_object.read()
- % w% D# _- N- W\" y9 o return contents.rstrip()
- 7 j1 F [$ v# W6 k2 \- B/ k2 t2 e M\" ?- P- R8 W) h& E
- - B4 Z( w0 _: s j7 `8 cdef Baidu_ocr(binary):
- ; C+ H/ x, A' n* k4 N url = "https://aip.baidubce.com/oauth/2.0/token?"
- ! e/ x' \ b\" p: s7 ^ data = {3 ]! c+ G% \$ P* ?1 x; m
- "grant_type": "client_credentials",& I+ o7 b/ i5 t$ b( p. ]
- "client_id": "", # 必填
- $ s( B2 B1 L5 } "client_secret": "" # 必填
- 1 y2 @& l, Q2 D+ j* O4 u( N o }# Q1 t8 G. _2 G& I5 A
- url += urlencode(data)' v, g* I1 z% E% u$ }3 @
- response = requests.get(url=url).json()
- . U$ k; v; {/ H- m: r7 n access_token = response['access_token']8 E0 l\" o* e2 M3 p5 b
- 6 ]' x g& ] U, [ url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + access_token4 m\" N4 g K7 m8 i\" E& s
- post_data = {
- 4 O6 n( V) t+ F c3 w5 f* p4 _ "image": base64.b64encode(binary),
- $ R: b, s\" Y( }. v; p$ a/ x "url": "",
- & {- Z1 f# k9 H5 Z: ? { "language_type": "CHN_ENG", N+ R6 P; s& K& D' B% k/ k
- "detect_direction": "false",& t\" u2 v* a: H5 P& n
- "detect_language": "false",
- 9 j$ K8 h6 F% _. J- J) k "probability": "false"
- u, K+ h) \% W v' n }- _( | k }4 C+ I- D$ a5 A& m
- response = requests.post(url=url, data=post_data).json()
- - l2 O! C* G! u G! J* S4 J+ l2 I num = response['words_result_num']# M& g\" g. ]- m; s4 F7 j
- result = ''5 {3 V- [; v7 f; f! }' ~1 j4 m. Q
- for i in range(num):
- ) R, j0 Z8 e8 M1 m( Z) p5 i result += response['words_result'][ i]['words'] + "\r\n"+ _/ V6 d( ~/ d
- return result
- F! d8 g/ s\" N; ?: C- J: a, \- r! a' W: C# L0 S2 D8 t
- . }& @$ ~7 P0 d1 G. h
- if __name__ == "__main__":
- ! \. U, }' a! ~8 ?\" i6 P. D file_path = Dialog_box()
- 4 _3 r5 C\" E4 d1 ^\" k9 r O. P( n if exists(file_path):
- 6 [. [6 A$ V, Q3 a data = Read_file(file_path)1 u/ |( P2 L% n- W
- print(Baidu_ocr(data))
- G7 u' o b- }* S\" O7 X: I5 u
- 6 s/ D9 J7 S5 L/ m
# a% S5 r3 y; l4 W
1 W" F. l, \0 I4 u$ y6 D
' `! N9 `, j. D# X$ Y6 uPython交流群:1047602540 4 [1 t) G( F: N: I4 m" M6 p- T
|
zan
|