- 在线时间
- 7 小时
- 最后登录
- 2021-1-20
- 注册时间
- 2021-1-9
- 听众数
- 3
- 收听数
- 0
- 能力
- 0 分
- 体力
- 23 点
- 威望
- 0 点
- 阅读权限
- 20
- 积分
- 10
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 7
- 主题
- 7
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   5.26% 该用户从未签到
- 自我介绍
- 我本名为我,那就是我
 |
8 ~, D& P0 d' G3 [2 @: R0 G- U - #!/usr/bin/python
- + l+ c6 |* S- Z1 ]5 Y( n# -*- coding: utf-8 -*-
- * O; N) ^& ], ]
- 8 k9 i9 L! {- pimport base64
- 3 @, E4 G. W Y- z2 pfrom os.path import exists
- 1 r4 [* i( F8 s) hfrom tkinter import Tk8 y. H# C; |. G; `6 G* r
- from tkinter.filedialog import askopenfilename H) a1 c0 h; z* f
- from urllib.parse import urlencode) }9 I( q# c. @4 \) `6 f) H% F
- . N; \: }4 t2 u/ himport requests
- , a H& @' {$ B- c# y; ~/ l
- ) c5 v8 C6 i; U( l$ w$ N6 D( w6 B5 q* l
- def Dialog_box():\" }3 ~6 Y6 c7 E. z9 p- O
- root = Tk()
- 4 k; v4 t2 K' h% R' y$ e root.withdraw()% p1 {% f0 p& g! m V$ B\" S
- % a/ M$ K/ i( G+ F9 c filetypes = [(8 W& u4 U+ P) c0 o% }6 b
- '图片文件(*.jpg;*.jpeg;*.gif;*.png;*.bmp)', '*.jpg;*.jpeg;*.gif;*.png;*.bmp'),/ M$ M! C. g* `1 t+ e8 u8 t
- ('所有文件(*.*)', '*.*')]/ s' l# A0 D$ \. }- O) o) @
- 6 d. U5 F2 L9 x* l- s$ g\" O
- file_path = askopenfilename(filetypes=filetypes)
- # y7 [9 g: e: }& i$ U- r return file_path
- ; r- d+ `+ [+ \1 u* Y6 O4 v
- 2 D: o. ~4 w$ ^' |
- 4 G+ x) Q% Q( _7 D\" B2 J# ndef Read_file(file_path):
- 6 G8 A w C7 x0 ]\" z\" A; ] with open(file_path, mode='rb') as file_object:# a7 C7 G* w! w7 P+ O6 J6 ?& r
- contents = file_object.read()0 f+ t6 B, C* c0 G
- return contents.rstrip()9 K\" G5 t3 r* q* w4 j: b) Z
- 7 A( j' s. q5 J
- g/ ?6 q6 C\" j; f$ l0 j- qdef Baidu_ocr(binary):3 R; h6 ]& u# C9 O0 U3 I8 Q
- url = "https://aip.baidubce.com/oauth/2.0/token?"8 d+ n+ n\" K( p# E
- data = {
- : w' x V+ l: h9 u; u9 X0 V "grant_type": "client_credentials",
- & x- O, D. H, O ]2 b1 v "client_id": "", # 必填
- . U0 B ~! F% | "client_secret": "" # 必填
- 2 y6 O$ Q# _) U, R* r3 r }& s! V' e) t+ X
- url += urlencode(data)
- 1 U9 y; x; j; i& A response = requests.get(url=url).json()
- $ R8 G% N: k' R access_token = response['access_token']
- : |* E\" O2 k8 U3 L( s) g, t- K( u* k5 ^8 w$ A$ e
- url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + access_token
- ! e\" S3 K. x% e post_data = {
- G\" u/ m0 e2 D6 E' t5 j2 b "image": base64.b64encode(binary),( _6 \* B( v/ ?3 O
- "url": "",+ z( l( G! H1 r+ [
- "language_type": "CHN_ENG",
- . d/ q% S% {0 S; [* Y' ~3 w "detect_direction": "false",# X/ w8 U& q9 |$ U8 e
- "detect_language": "false",1 e6 w; ~( u3 L/ M5 A% t
- "probability": "false"& `- x+ g n\" E# ?/ Z* g
- }
- ! O; \3 x4 h. c0 k' _% B response = requests.post(url=url, data=post_data).json()
- ! }& S; x0 H5 [/ t num = response['words_result_num']
- % A& @2 Z( V, U result = ''
- * Y$ W& m- {& y\" p! j for i in range(num):) D/ u& A; ?/ H7 S. Q
- result += response['words_result'][ i]['words'] + "\r\n"& y n4 F+ y& i- b- L' t\" m
- return result0 p4 n8 J* a1 \
- $ p9 e. K& G+ \ C+ x3 S0 D! y. d0 o/ m7 Z: O3 o- J8 z
- if __name__ == "__main__":6 j4 u3 X* ~3 {5 `0 C' R: I# x
- file_path = Dialog_box()
- 5 X' g9 n# T1 N\" [1 S if exists(file_path):
- ) L D0 i) @ m4 ?4 e8 e data = Read_file(file_path)
- + p. k4 e- o8 D, l5 Y: y print(Baidu_ocr(data))
- 4 s+ h2 b! e- r A
- : N/ p0 B( }) Z6 b8 K' K
. w0 r/ J- T. S1 N, U+ m, p
* ~; G. z# A7 i8 m" ^! v8 i' r+ c- L" J& X# H9 S( T& W/ h3 f
Python交流群:1047602540 h, c7 q) ~$ s
|
zan
|