- 在线时间
- 7 小时
- 最后登录
- 2021-1-20
- 注册时间
- 2021-1-9
- 听众数
- 3
- 收听数
- 0
- 能力
- 0 分
- 体力
- 23 点
- 威望
- 0 点
- 阅读权限
- 20
- 积分
- 10
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 7
- 主题
- 7
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   5.26% 该用户从未签到
- 自我介绍
- 我本名为我,那就是我
 |
! s, a& }. K9 r% A* d0 r' C - #!/usr/bin/python
- / {% i/ J- s- U) D\" c/ ~# -*- coding: utf-8 -*-
- 4 w; s9 I! {; a- H/ o( ?1 ~9 t! x% k
- import base64' v: n7 w0 d6 T$ _7 Z: Z
- from os.path import exists
- 0 j3 A/ V# w9 f- [5 I5 Wfrom tkinter import Tk
- / M3 L/ z+ v! c2 [from tkinter.filedialog import askopenfilename' g. Q b' u9 h, s; X B5 q h9 C, _
- from urllib.parse import urlencode
- . Q6 F3 ]/ l5 m7 l
- # n9 v8 H! W8 ~0 V% \import requests
- * U5 O8 ^\" @' c: ~
- ( }/ K% N' j4 X5 D8 Y$ e
- ) P( Z# Q5 F3 F! [+ ldef Dialog_box():- n' U+ n' a+ J$ B: K
- root = Tk()$ I1 J* A9 T0 V9 O2 X# m; ^
- root.withdraw()
- , o6 ?4 R1 ?- P% x5 ~: q/ O4 s# _* D- ^4 o
- filetypes = [(
- % [2 m. \) s' d' d5 e# g3 G '图片文件(*.jpg;*.jpeg;*.gif;*.png;*.bmp)', '*.jpg;*.jpeg;*.gif;*.png;*.bmp'),1 i4 I, x! T8 y8 [8 U\" l! V+ B, @
- ('所有文件(*.*)', '*.*')]# n1 f: A' x! Q6 c* P% ]
- / S8 O' g: b' E/ ^
- file_path = askopenfilename(filetypes=filetypes)/ T1 _3 u0 P& U' v\" f$ x
- return file_path
- ! x. e: {4 o0 e0 K: ~5 o; M6 V0 x/ E$ n6 A8 v* m) [5 s; Y
- % V6 {/ t- E- ~
- def Read_file(file_path):4 O. C! T7 F2 A) ^0 ]& C
- with open(file_path, mode='rb') as file_object:# d' ]# w8 d) u0 I, K\" P
- contents = file_object.read()4 T8 W5 x& [. m% A5 p
- return contents.rstrip()% _: p9 o* @9 K* g( ?2 q! ?
- : v2 R7 ?/ N/ J# h
- - r1 d2 L7 j. L T3 y) Adef Baidu_ocr(binary):
- v+ H. C: g0 u( B( [ url = "https://aip.baidubce.com/oauth/2.0/token?"
- $ |* ]9 z2 I) g# o: p data = {9 j0 c3 j+ I9 {- E6 s- o
- "grant_type": "client_credentials",
- 1 a! R1 g. c# ^ "client_id": "", # 必填
- 1 C( K* V: x3 @9 J "client_secret": "" # 必填( F9 f) I\" y\" e( u5 j
- }( d5 c2 R+ V |* C
- url += urlencode(data)5 U- P& o5 v\" U' d5 A v\" |
- response = requests.get(url=url).json(). I8 H& F% b V; ~; c
- access_token = response['access_token']5 ?\" ~( k1 B\" A8 y0 d: a) n
- % V1 j7 j D: N$ o9 p1 ` x
- url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + access_token2 m: P! Q! n* q5 w5 Y+ A. X% A
- post_data = {
- 3 G* ~& c- t$ a4 F I "image": base64.b64encode(binary),; o! I, h/ i: s2 D# x
- "url": "",1 |0 Y: p' Q$ n3 c2 V E! l, F
- "language_type": "CHN_ENG",3 W+ X3 T/ I0 Q9 f2 p# `
- "detect_direction": "false",
- 9 t% I6 N8 I8 ]: z: B0 C3 Y8 e "detect_language": "false",6 h7 e: Z4 v' U
- "probability": "false"7 i# g% p9 y6 W M0 i
- }4 K0 |/ W% J' c* h
- response = requests.post(url=url, data=post_data).json()+ ] g( a1 v6 w( x* y7 V, P
- num = response['words_result_num']
- + Q F3 n, a' o\" z5 q1 L4 s1 b& Z2 ~* @ result = ''
- ; f\" d( u9 w6 S$ u for i in range(num):
- ) g1 Z\" O1 S1 ^! R0 r* P result += response['words_result'][ i]['words'] + "\r\n"/ a4 F6 ^\" S- m) B\" e
- return result
- - b: Y/ y# l; n8 \/ v' H3 Z8 e9 p( p9 F7 }5 X) ^6 s# y- y
- ) k4 L# q) Q\" \. p! }+ ^
- if __name__ == "__main__":
- 7 d- B/ Z1 }) t/ ]+ e# Q file_path = Dialog_box()
- ( E\" n! D6 c: {; h6 P3 l if exists(file_path):- ]. X6 r; p0 F! R( Q, d1 K2 |
- data = Read_file(file_path)
- ) {# ]( E+ [- J7 }: _ print(Baidu_ocr(data))$ g+ F X0 S% g+ L& f% C
- : N: Q' x/ ]) K/ ^8 _7 j5 i
2 x: {/ z% `1 }" A; X1 C
L) g% |( B! [ \# A# m
! a6 G4 _1 _4 o( l% c2 jPython交流群:1047602540 # i0 d. }; M* ?6 ?! S( [/ V, N
|
zan
|