QQ登录

只需要一步,快速开始

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

Python-百度ocr识别

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

7

主题

3

听众

10

积分

升级  5.26%

该用户从未签到

自我介绍
我本名为我,那就是我
跳转到指定楼层
1#
发表于 2021-1-12 09:38 |只看该作者 |倒序浏览
|招呼Ta 关注Ta

: ^$ k! W; l1 P0 ^: ~
  1. #!/usr/bin/python
  2. 1 m8 f5 x' D1 E# -*- coding: utf-8 -*-
  3. * {# R7 N- A7 R7 W+ f  U5 `, ]
  4. # T' V, @- }# o9 h$ {import base64\" j$ Q\" J( U/ p9 Z
  5. from os.path import exists
  6. - w7 ?$ v6 ]; V9 r3 n6 r% g7 D: r+ W+ Tfrom tkinter import Tk
  7. \" {+ P3 r6 ?5 t1 o4 Q% ffrom tkinter.filedialog import askopenfilename
  8. $ A4 `% w) n2 w: ]& Q7 a* hfrom urllib.parse import urlencode
  9. / H! [! j+ A1 T1 \
  10. & C' A' d1 w. Z2 ?* ?9 Y: e) himport requests% B, Z4 L( b: R  `7 T) r9 x

  11. 1 R5 q& ^' d, j( |# Y' K
  12. ! B- d7 Z8 k% o/ Xdef Dialog_box():1 r8 ~# ~( R* b* F# h1 a\" L
  13.     root = Tk(). F\" G\" [9 o  _
  14.     root.withdraw()
  15. & P% }3 Y4 @6 _+ C) J9 d* j4 ~: f, Y' \; F. H- Y
  16.     filetypes = [(0 n0 ]4 s7 M* p+ a0 }2 `! r
  17.         '图片文件(*.jpg;*.jpeg;*.gif;*.png;*.bmp)', '*.jpg;*.jpeg;*.gif;*.png;*.bmp'),2 u( {/ A8 p/ f1 _% a8 u
  18.         ('所有文件(*.*)', '*.*')]
  19. 7 J1 X- y\" F# v1 ?- F# @
  20. 5 I  {2 V$ l\" b6 d% n  w    file_path = askopenfilename(filetypes=filetypes)
  21. , V5 Z% P0 @\" W2 k- a    return file_path
  22. ( R( ?+ @# Z6 ~) [# U$ S2 i
  23. % G; x, S* N8 z! k$ W! @+ w9 @% q3 W' _: Y8 i1 H4 e0 T% D
  24. def Read_file(file_path):
  25. 5 s, s1 \+ m& _8 N    with open(file_path, mode='rb') as file_object:% o: |( S. X( O3 g) x- H
  26.         contents = file_object.read()0 I5 }/ M3 `; z% J* D. _
  27.         return contents.rstrip()  A, t# }5 u5 R+ d& [  f1 }2 K9 w4 `9 v

  28. 1 c) M( G6 L- ?! ?9 Y9 R9 Z* e
  29. ) o: F  q( \0 i, Q/ Odef Baidu_ocr(binary):1 [7 y9 H2 f. K% X% k
  30.     url = "https://aip.baidubce.com/oauth/2.0/token?"
  31. , B1 Q3 q! v* Q' Z; K( @    data = {
  32. 7 ?\" ]. [0 B9 S6 ]1 X+ s        "grant_type": "client_credentials",1 M  i' M& e7 u- |! H+ |
  33.         "client_id": "",  # 必填
  34. - i5 z( n; \' L\" g0 h# h        "client_secret": ""  # 必填
  35. 0 v9 S1 L% R& t9 ?+ _    }
  36. * S& p; w+ ?1 I. B2 A    url += urlencode(data). B1 h# R# {% }$ B, n' C2 c: x' Q# l
  37.     response = requests.get(url=url).json()
  38. 5 q* L5 u. ~, [6 p# p    access_token = response['access_token'], \$ x$ K  e# P- w5 g0 J1 F
  39. , x. E* w( k' ~8 n1 {
  40.     url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + access_token
  41.   l7 Q5 R* \) O. S( s    post_data = {- {' M: O3 F* J; R; h+ l2 |7 a* p
  42.         "image": base64.b64encode(binary),
  43. ( O+ c$ K; I4 W7 n4 ?$ o- S: k, e        "url": "",
  44. ' k4 b6 x  m6 [1 M        "language_type": "CHN_ENG",3 ~) X) p/ J1 Y$ B
  45.         "detect_direction": "false",+ T% u# ~! ~! X
  46.         "detect_language": "false",
  47. & C8 A% S& F1 V, B        "probability": "false"9 \2 ]. T/ J# `6 Q. \
  48.     }$ t\" u. I& k6 K! i
  49.     response = requests.post(url=url, data=post_data).json()5 T0 W1 e3 f) g  K. V. A# k* V
  50.     num = response['words_result_num']
  51. 3 `/ B7 o8 V# P# g+ v    result = ''. k3 b0 z4 z* W2 c6 j2 z; }2 K
  52.     for i in range(num):& d0 s7 ?  C\" ]  h, w
  53.         result += response['words_result'][ i]['words'] + "\r\n"# m# V# J3 g% Z( D4 c% R2 ~
  54.     return result4 D5 T7 e  w& Y# ^  l- Q4 z\" A
  55. 3 j' \# F: t& E/ o/ w0 q/ D' S

  56. $ g: W- ^% _1 S/ v, Pif __name__ == "__main__":2 B: w# j! c1 n0 G+ S# V
  57.     file_path = Dialog_box()
  58. 9 o& _6 {1 Q0 B) g4 O, P    if exists(file_path):- `9 }+ q- `5 {% ]. ~
  59.         data = Read_file(file_path)
  60. ( \! f# a2 G+ \! d; E/ j        print(Baidu_ocr(data))
  61. # _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. x
转发自派生社区
Python交流群:1047602540
8 J; C) D4 t- @9 x3 `
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-9-2 16:06 , Processed in 0.436803 second(s), 50 queries .

回顶部