请选择 进入手机版 | 继续访问电脑版

QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2303|回复: 0

Python-百度ocr识别

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

7

主题

3

听众

10

积分

升级  5.26%

该用户从未签到

自我介绍
我本名为我,那就是我
发表于 2021-1-12 09:38 |显示全部楼层
|招呼Ta 关注Ta
4 _6 P4 I" `" N6 l8 l6 ~
  1. #!/usr/bin/python\" ~, ?' S5 g# z; F/ E8 U, _  S
  2. # -*- coding: utf-8 -*-
  3. - O- h4 U8 P7 ]  q\" Z/ t5 r/ @\" q. ^8 f& }, Y/ P
  4. import base64
  5. 2 Q$ p: w1 ~/ ofrom os.path import exists
  6. 9 B: S# w  ~3 m* ~4 O, a# Jfrom tkinter import Tk
  7.   j5 s4 p: Z$ Afrom tkinter.filedialog import askopenfilename0 I: d( i\" B7 t
  8. from urllib.parse import urlencode
  9. 1 `\" _  e* ]8 M0 {: B
  10. ! U8 b' ^$ r3 Q; @) _6 n) |import requests
  11. 1 I% d: o7 B+ T! t# C
  12. 5 e( S$ W5 ~( A$ n5 ]
  13. & v: L( T7 }. z; B& D7 Ddef Dialog_box():
  14. + S. Y0 ^$ E% I7 X* `/ P# B    root = Tk()1 x0 Z: f9 p! @+ b' q
  15.     root.withdraw()
  16. 4 n  {4 W0 {6 S9 _2 Q, Q, L& j+ P% A
  17.     filetypes = [(2 c' Z* f  E\" ?# E  z6 G( S
  18.         '图片文件(*.jpg;*.jpeg;*.gif;*.png;*.bmp)', '*.jpg;*.jpeg;*.gif;*.png;*.bmp'),
  19. . \$ p* T- f( P6 q6 j: \        ('所有文件(*.*)', '*.*')]
  20. 3 r& ~  j6 F, F) u: N* t% f8 C
  21. . [\" l2 B4 r  J$ N    file_path = askopenfilename(filetypes=filetypes)
  22. 1 ]9 n/ a5 r- ]$ G    return file_path
  23. + L, _+ v/ n; X0 H1 w( Y1 U9 v8 V- k- ~1 _
  24. , t* Q3 h: p9 \
  25. def Read_file(file_path):
  26. + `$ O8 p  u; P- [7 m/ Q, |    with open(file_path, mode='rb') as file_object:
  27. ) S. A1 H$ m  P$ u6 q! j  ~        contents = file_object.read()
  28. + O8 y! m1 L: U! U: s  G/ V( C        return contents.rstrip()$ s! ]- H, z4 ?
  29. 2 A) G) F; Q; c8 c$ o
  30. , }' W1 ^8 M0 b( Z+ J
  31. def Baidu_ocr(binary):
  32. ; d! A4 L# x& g' d' f    url = "https://aip.baidubce.com/oauth/2.0/token?"
  33. 0 R) @2 X+ |( r6 \    data = {3 \& ?- ~' k7 [\" k& ?
  34.         "grant_type": "client_credentials",  m, ~3 [  |& ^1 D- v
  35.         "client_id": "",  # 必填
  36. 9 E6 ]' M2 I$ p1 `- `9 X        "client_secret": ""  # 必填
  37. + L1 Z4 m2 `  O( B7 }+ S. e7 C. k    }
  38. : _4 d; u1 x& j  {. A) D& L    url += urlencode(data)
  39. 7 r8 W; M* h# M+ i$ j  [0 l    response = requests.get(url=url).json()
  40. # F% X- Z- d. z: o4 r  x! \    access_token = response['access_token']
  41. 5 G1 ~7 c1 x. p; R\" ^\" J% l
  42. ( M+ @+ ?; u% U* w- S\" ]    url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + access_token
  43. , ?, g4 s* ]) I$ c7 z! V    post_data = {
  44. 6 x- l1 {9 Z8 D5 U        "image": base64.b64encode(binary),
  45. # b  ^; H/ n: |& W        "url": "",
  46. ; b, Z% y+ [. m* b: n+ {        "language_type": "CHN_ENG",
  47. , x4 y1 {2 _\" _8 b\" j        "detect_direction": "false",; g6 ?2 Y0 y) L0 G, `
  48.         "detect_language": "false",
  49. : I, k5 a( Q1 o5 H0 `+ g        "probability": "false". r6 Z9 L2 \+ a8 B' q0 L  R$ A4 q2 h
  50.     }& U6 u8 s3 [6 A3 g) y& A+ m
  51.     response = requests.post(url=url, data=post_data).json()
  52. + b( c! p0 z4 S( g/ r$ o. A    num = response['words_result_num']
  53. : X( A0 g, J5 h; X    result = ''6 |5 X4 |1 k$ R- k0 k
  54.     for i in range(num):- ~: W. _& _  Z\" t% g$ s( O2 o
  55.         result += response['words_result'][ i]['words'] + "\r\n"
  56. \" j) C2 ^/ u6 W    return result
  57. . p* U: v4 N. c\" G/ q! a; R
  58. 8 N# |! V1 |/ K3 N! U+ k; b
  59. 2 b# B2 r3 L% l8 x/ Fif __name__ == "__main__":+ _$ E/ h/ Z+ W7 i& q8 x\" X
  60.     file_path = Dialog_box()$ ?8 M+ y7 D1 z/ x& j. U, X$ E
  61.     if exists(file_path):
  62. 1 _% w, }8 R/ o; U/ S' ]+ x- G        data = Read_file(file_path)
  63. : V7 k' y0 f, X8 m2 J        print(Baidu_ocr(data))
  64. 2 ~& K# m8 y\" _$ Z3 J
  65. ( n- O6 d) U! u8 I% W1 _+ ?
: n& C% O( C0 y' e( z1 K5 i3 L# w
/ g' }6 |8 u; r  ?0 o

4 R2 G  h# k8 p
转发自派生社区
Python交流群:1047602540
; f8 }( H+ I1 @7 x
zan
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

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

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

蒙公网安备 15010502000194号

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

GMT+8, 2024-4-16 16:39 , Processed in 0.386576 second(s), 50 queries .

回顶部