- 在线时间
- 7 小时
- 最后登录
- 2021-1-20
- 注册时间
- 2021-1-9
- 听众数
- 3
- 收听数
- 0
- 能力
- 0 分
- 体力
- 23 点
- 威望
- 0 点
- 阅读权限
- 20
- 积分
- 10
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 7
- 主题
- 7
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   5.26% 该用户从未签到
- 自我介绍
- 我本名为我,那就是我
 |
: ^$ k! W; l1 P0 ^: ~ - #!/usr/bin/python
- 1 m8 f5 x' D1 E# -*- coding: utf-8 -*-
- * {# R7 N- A7 R7 W+ f U5 `, ]
- # T' V, @- }# o9 h$ {import base64\" j$ Q\" J( U/ p9 Z
- from os.path import exists
- - w7 ?$ v6 ]; V9 r3 n6 r% g7 D: r+ W+ Tfrom tkinter import Tk
- \" {+ P3 r6 ?5 t1 o4 Q% ffrom tkinter.filedialog import askopenfilename
- $ A4 `% w) n2 w: ]& Q7 a* hfrom urllib.parse import urlencode
- / H! [! j+ A1 T1 \
- & C' A' d1 w. Z2 ?* ?9 Y: e) himport requests% B, Z4 L( b: R `7 T) r9 x
- 1 R5 q& ^' d, j( |# Y' K
- ! B- d7 Z8 k% o/ Xdef Dialog_box():1 r8 ~# ~( R* b* F# h1 a\" L
- root = Tk(). F\" G\" [9 o _
- root.withdraw()
- & P% }3 Y4 @6 _+ C) J9 d* j4 ~: f, Y' \; F. H- Y
- filetypes = [(0 n0 ]4 s7 M* p+ a0 }2 `! r
- '图片文件(*.jpg;*.jpeg;*.gif;*.png;*.bmp)', '*.jpg;*.jpeg;*.gif;*.png;*.bmp'),2 u( {/ A8 p/ f1 _% a8 u
- ('所有文件(*.*)', '*.*')]
- 7 J1 X- y\" F# v1 ?- F# @
- 5 I {2 V$ l\" b6 d% n w file_path = askopenfilename(filetypes=filetypes)
- , V5 Z% P0 @\" W2 k- a return file_path
- ( R( ?+ @# Z6 ~) [# U$ S2 i
- % G; x, S* N8 z! k$ W! @+ w9 @% q3 W' _: Y8 i1 H4 e0 T% D
- def Read_file(file_path):
- 5 s, s1 \+ m& _8 N with open(file_path, mode='rb') as file_object:% o: |( S. X( O3 g) x- H
- contents = file_object.read()0 I5 }/ M3 `; z% J* D. _
- return contents.rstrip() A, t# }5 u5 R+ d& [ f1 }2 K9 w4 `9 v
- 1 c) M( G6 L- ?! ?9 Y9 R9 Z* e
- ) o: F q( \0 i, Q/ Odef Baidu_ocr(binary):1 [7 y9 H2 f. K% X% k
- url = "https://aip.baidubce.com/oauth/2.0/token?"
- , B1 Q3 q! v* Q' Z; K( @ data = {
- 7 ?\" ]. [0 B9 S6 ]1 X+ s "grant_type": "client_credentials",1 M i' M& e7 u- |! H+ |
- "client_id": "", # 必填
- - i5 z( n; \' L\" g0 h# h "client_secret": "" # 必填
- 0 v9 S1 L% R& t9 ?+ _ }
- * S& p; w+ ?1 I. B2 A url += urlencode(data). B1 h# R# {% }$ B, n' C2 c: x' Q# l
- response = requests.get(url=url).json()
- 5 q* L5 u. ~, [6 p# p access_token = response['access_token'], \$ x$ K e# P- w5 g0 J1 F
- , x. E* w( k' ~8 n1 {
- url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + access_token
- l7 Q5 R* \) O. S( s post_data = {- {' M: O3 F* J; R; h+ l2 |7 a* p
- "image": base64.b64encode(binary),
- ( O+ c$ K; I4 W7 n4 ?$ o- S: k, e "url": "",
- ' k4 b6 x m6 [1 M "language_type": "CHN_ENG",3 ~) X) p/ J1 Y$ B
- "detect_direction": "false",+ T% u# ~! ~! X
- "detect_language": "false",
- & C8 A% S& F1 V, B "probability": "false"9 \2 ]. T/ J# `6 Q. \
- }$ t\" u. I& k6 K! i
- response = requests.post(url=url, data=post_data).json()5 T0 W1 e3 f) g K. V. A# k* V
- num = response['words_result_num']
- 3 `/ B7 o8 V# P# g+ v result = ''. k3 b0 z4 z* W2 c6 j2 z; }2 K
- for i in range(num):& d0 s7 ? C\" ] h, w
- result += response['words_result'][ i]['words'] + "\r\n"# m# V# J3 g% Z( D4 c% R2 ~
- return result4 D5 T7 e w& Y# ^ l- Q4 z\" A
- 3 j' \# F: t& E/ o/ w0 q/ D' S
- $ g: W- ^% _1 S/ v, Pif __name__ == "__main__":2 B: w# j! c1 n0 G+ S# V
- file_path = Dialog_box()
- 9 o& _6 {1 Q0 B) g4 O, P if exists(file_path):- `9 }+ q- `5 {% ]. ~
- data = Read_file(file_path)
- ( \! f# a2 G+ \! d; E/ j print(Baidu_ocr(data))
- # _6 X3 q7 J9 c3 ?; @ u& i6 t\" S |
2 E U4 j/ D- L6 S3 e* N# o& p% N" J+ q' m' C% S1 a( y. W+ M
, Q0 R. ]: u1 Y2 m0 n. xPython交流群:1047602540 8 J; C) D4 t- @9 x3 `
|
zan
|