QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2814|回复: 0
打印 上一主题 下一主题

Python-百度ocr识别

[复制链接]
字体大小: 正常 放大
檀俾九        

7

主题

3

听众

10

积分

升级  5.26%

该用户从未签到

自我介绍
我本名为我,那就是我
跳转到指定楼层
1#
发表于 2021-1-12 09:38 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
6 ?+ x( H6 q* \8 v& ~
  1. #!/usr/bin/python$ }' U7 c; J1 P7 r
  2. # -*- coding: utf-8 -*-1 P* o0 C0 l9 ?# B' \; Y7 d
  3. . W( v; f+ o/ m$ y1 R\" n
  4. import base64
  5. 1 i  a+ G6 G2 ~\" x1 R8 L0 c8 J- hfrom os.path import exists
  6. # \: X' f% c0 Y3 u# e7 Zfrom tkinter import Tk, e* h; u8 h+ X- w; L5 {
  7. from tkinter.filedialog import askopenfilename
  8. # d* q; P# t& |: Q  ^* Mfrom urllib.parse import urlencode
  9. ) e; n3 P6 N% b; k7 U1 h+ Q
  10. : Z; z+ @3 P6 \9 u# F& C  t5 e& ~6 fimport requests& x# q- y# R% k7 L/ E% S3 |1 e4 U
  11. & r\" Y( q7 _7 f! L% q
  12. # s$ |6 a( h  v& W
  13. def Dialog_box():
  14. & M( y- [& j2 h    root = Tk()6 W1 L8 _1 a+ S6 D' ~! A+ Y& U
  15.     root.withdraw(); W7 t: v& X- _8 ~

  16.   C; p  P  h/ t6 I7 y    filetypes = [(1 u\" ]7 `/ L# x6 ~1 s
  17.         '图片文件(*.jpg;*.jpeg;*.gif;*.png;*.bmp)', '*.jpg;*.jpeg;*.gif;*.png;*.bmp'),. ?+ Y& p* y; [& w3 d' W
  18.         ('所有文件(*.*)', '*.*')]
  19. # B+ s, l7 I; F5 \/ D! {: x' N/ p: J7 k( M) Y0 Z) @- I' ?- |\" e  G
  20.     file_path = askopenfilename(filetypes=filetypes), P$ ]+ l5 _! p$ U: f: D
  21.     return file_path
  22. 6 z0 N  F( y* f6 p. g  [) F# }1 w/ M
  23. 7 f# x9 R$ t2 }0 D2 ?
  24. def Read_file(file_path):0 `/ J0 q5 [! D' v3 Y
  25.     with open(file_path, mode='rb') as file_object:& M/ K9 u: U6 d\" V  R
  26.         contents = file_object.read()
  27. % w% D# _- N- W\" y9 o        return contents.rstrip()
  28. 7 j1 F  [$ v# W6 k2 \- B/ k2 t2 e  M\" ?- P- R8 W) h& E

  29. - B4 Z( w0 _: s  j7 `8 cdef Baidu_ocr(binary):
  30. ; C+ H/ x, A' n* k4 N    url = "https://aip.baidubce.com/oauth/2.0/token?"
  31. ! e/ x' \  b\" p: s7 ^    data = {3 ]! c+ G% \$ P* ?1 x; m
  32.         "grant_type": "client_credentials",& I+ o7 b/ i5 t$ b( p. ]
  33.         "client_id": "",  # 必填
  34. $ s( B2 B1 L5 }        "client_secret": ""  # 必填
  35. 1 y2 @& l, Q2 D+ j* O4 u( N  o    }# Q1 t8 G. _2 G& I5 A
  36.     url += urlencode(data)' v, g* I1 z% E% u$ }3 @
  37.     response = requests.get(url=url).json()
  38. . U$ k; v; {/ H- m: r7 n    access_token = response['access_token']8 E0 l\" o* e2 M3 p5 b

  39. 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
  40.     post_data = {
  41. 4 O6 n( V) t+ F  c3 w5 f* p4 _        "image": base64.b64encode(binary),
  42. $ R: b, s\" Y( }. v; p$ a/ x        "url": "",
  43. & {- Z1 f# k9 H5 Z: ?  {        "language_type": "CHN_ENG",  N+ R6 P; s& K& D' B% k/ k
  44.         "detect_direction": "false",& t\" u2 v* a: H5 P& n
  45.         "detect_language": "false",
  46. 9 j$ K8 h6 F% _. J- J) k        "probability": "false"
  47.   u, K+ h) \% W  v' n  }- _( |  k    }4 C+ I- D$ a5 A& m
  48.     response = requests.post(url=url, data=post_data).json()
  49. - l2 O! C* G! u  G! J* S4 J+ l2 I    num = response['words_result_num']# M& g\" g. ]- m; s4 F7 j
  50.     result = ''5 {3 V- [; v7 f; f! }' ~1 j4 m. Q
  51.     for i in range(num):
  52. ) R, j0 Z8 e8 M1 m( Z) p5 i        result += response['words_result'][ i]['words'] + "\r\n"+ _/ V6 d( ~/ d
  53.     return result
  54.   F! d8 g/ s\" N; ?: C- J: a, \- r! a' W: C# L0 S2 D8 t
  55. . }& @$ ~7 P0 d1 G. h
  56. if __name__ == "__main__":
  57. ! \. U, }' a! ~8 ?\" i6 P. D    file_path = Dialog_box()
  58. 4 _3 r5 C\" E4 d1 ^\" k9 r  O. P( n    if exists(file_path):
  59. 6 [. [6 A$ V, Q3 a        data = Read_file(file_path)1 u/ |( P2 L% n- W
  60.         print(Baidu_ocr(data))
  61.   G7 u' o  b- }* S\" O7 X: I5 u
  62. 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 u
转发自派生社区
Python交流群:1047602540
4 [1 t) G( F: N: I4 m" M6 p- T
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-7-18 11:01 , Processed in 0.406857 second(s), 50 queries .

回顶部