QQ登录

只需要一步,快速开始

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

Python-百度ocr识别

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

7

主题

3

听众

10

积分

升级  5.26%

该用户从未签到

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

; U4 m' K8 m, }( n
  1. #!/usr/bin/python
  2. 7 O: E2 R% o- L; a4 r; o# -*- coding: utf-8 -*-
  3. + U. J8 v, _) T& x, a% R$ [5 r: ~3 V
  4. ( ]5 m2 Q0 b6 H1 i) Iimport base64
  5. - Q: v7 y! v; H8 C( yfrom os.path import exists1 X3 ^! R3 @) n! D! t& R( t
  6. from tkinter import Tk: W- M+ a( G8 N5 S, T: [
  7. from tkinter.filedialog import askopenfilename
  8. / U- m' K5 W: s# o: |1 Vfrom urllib.parse import urlencode
  9. - V) U# J' L1 Y\" M3 ]0 A) T
  10. 4 l0 L/ e1 R1 ?( D* I* bimport requests& A! k4 i+ M! [2 b1 L! s$ P
  11. ; k+ ?6 m, y& G# @( L
  12. 9 [# i3 z& k0 S. }3 `( H0 X
  13. def Dialog_box():
  14. 5 `5 M$ K: e9 V) F* \    root = Tk()% y+ ?: a# ^3 a+ i( E2 O\" R1 |
  15.     root.withdraw()
  16. / X' }: t; I. Q; u7 _9 L' f2 t) N+ ]* D, j( @5 k\" ?* n3 ^
  17.     filetypes = [(- @+ j( u2 i/ X, U* f8 L
  18.         '图片文件(*.jpg;*.jpeg;*.gif;*.png;*.bmp)', '*.jpg;*.jpeg;*.gif;*.png;*.bmp'),
  19. 4 R2 p: Q( Y$ O\" y        ('所有文件(*.*)', '*.*')]
  20. * h# Y. `9 S- K8 h. f3 H* i7 j
  21. - @: p* e3 `' Y\" t2 h& Y# O) @    file_path = askopenfilename(filetypes=filetypes)
  22. ; m0 _! a& n- [! ]3 Q- p    return file_path
  23. 6 r. K! v5 N- [% z\" `% V
  24. 9 `5 T' C$ z% ~# j$ F
  25. 4 ?8 h. f) ?4 m& E+ h3 Fdef Read_file(file_path):  V- N: P$ _4 V! ~- L7 g
  26.     with open(file_path, mode='rb') as file_object:5 ?* [* c% X4 h. s8 t
  27.         contents = file_object.read()1 P: T& C3 S& S
  28.         return contents.rstrip()
  29. $ L. e* \7 J( s  T: b: Q; u2 V
  30. 3 A( Z( A9 O. F1 ]6 T  i$ I4 B# L' h9 U9 y
  31. def Baidu_ocr(binary):& \: o3 g1 \- x
  32.     url = "https://aip.baidubce.com/oauth/2.0/token?"
  33. : x9 t6 \/ J9 l    data = {
  34. % @3 T' D% W' w0 l        "grant_type": "client_credentials",! E& S# u3 d6 k$ M
  35.         "client_id": "",  # 必填; N$ U8 ]  A% E; }4 S\" d
  36.         "client_secret": ""  # 必填3 e0 W% q9 ^3 R9 L8 E. p
  37.     }
  38. 4 i* f. B& ]  c    url += urlencode(data)1 Z  u# w! a. W! \  N, _# a- c% y
  39.     response = requests.get(url=url).json()9 R# A6 h1 y* q/ ?
  40.     access_token = response['access_token']% L\" N7 r4 P' b5 _. B6 \; B

  41. 4 M* b: ?( j. \9 p& K. y# P\" X    url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + access_token% X\" E4 Z+ j3 k$ T) Z
  42.     post_data = {% Y* I9 E- u5 j\" l' e/ ?
  43.         "image": base64.b64encode(binary),
  44. 3 ?9 Y& _! `6 ?% R  f' ^) G        "url": "",. M/ {; N2 h8 Z\" D7 h( i
  45.         "language_type": "CHN_ENG",; G. R. Y, f8 L7 V: G
  46.         "detect_direction": "false",# S+ u3 U' K2 w6 o/ s\" y
  47.         "detect_language": "false",
  48. 3 _  P7 O% V  h( [2 _        "probability": "false"
  49. $ T5 U7 z. G# h2 h% M    }
  50. \" P5 l  Q/ l/ m- O% B) Y    response = requests.post(url=url, data=post_data).json()
  51. 6 B+ E1 b5 m\" H( i    num = response['words_result_num']4 |/ f% E4 C8 j0 x- v
  52.     result = ''* w) \; Q$ g7 K9 e
  53.     for i in range(num):
  54. * t$ H- A. ]7 U$ h! y  W# b( w        result += response['words_result'][ i]['words'] + "\r\n"
  55. 0 S  K$ |9 T% g6 y    return result
  56. ; A; r$ f$ d3 R' \' b0 G% e+ V
  57. ' y' `$ g6 U  v$ X+ U- D4 @) i* G  @% p\" I9 `- ?+ F# y
  58. if __name__ == "__main__":
  59. ! Q8 n1 k5 i8 k    file_path = Dialog_box()
  60. + }& l9 A/ f  x/ O0 l    if exists(file_path):
  61. ( `: H5 ~  m\" v6 C4 B, |7 Z# G        data = Read_file(file_path)
  62. % n+ W( Z4 W! T# d- ?+ I. B, \        print(Baidu_ocr(data)): R9 \% Q* ?, q

  63. 9 p  s+ i3 j+ g) B- _( h0 U
5 Z, w+ Z' A# E

) |2 q+ X! G+ x7 M3 h+ }2 _$ J, s
! ?- c3 r' S) @0 n* O
转发自派生社区
Python交流群:1047602540
2 \/ r- |, S+ b, `% I0 ^
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-3 06:31 , Processed in 0.273863 second(s), 49 queries .

回顶部