python 3调用百度OCR API实现剪贴板文字识别

作者:方工 时间:2022-12-13 19:01:14 

本程序调用百度OCR API对剪贴板的图片文字识别,配合CaptureScreen软件,可快速识别文字。


#!python3
import urllib.request, urllib.parse
import os, io, sys, json, socket
import base64
from PIL import ImageGrab

socket.setdefaulttimeout(30)

def get_auth():
 apikey = 'your apikey'
 secret_key = 'your secret key'
 host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s' % (apikey, secret_key)
 req = urllib.request.Request(host)
 req.add_header('Content-Type', 'application/json; charset=UTF-8')
 res = urllib.request.urlopen(req)
 content = res.read()
 if (content):
   o = json.loads(content.decode())
   return o['access_token']
 return None

def ocr_clipboard():
 im = ImageGrab.grabclipboard()
 if im is None:
   print('No image in clipboard')
   return
 print('image size: %sx%s\n>>>\n' % (im.size[0], im.size[1]))
 mf = io.BytesIO()
 im.save(mf, 'JPEG')
 mf.seek(0)
 buf = mf.read()
 b64 = base64.encodebytes(buf)
 access_token = get_auth()
 if access_token is not None:
   url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=%s' % access_token
   data = urllib.parse.urlencode({'image' : b64}).encode()
   req = urllib.request.Request(url, method='POST')
   req.add_header('Content-Type', 'application/x-www-form-urlencoded')
   with urllib.request.urlopen(req, data) as p:
     res = p.read().decode('utf-8')
     o = json.loads(res)
     if o['words_result'] is not None:
       for w in o['words_result']:
         print(w['words'])
     print('\n<<<')
 else:
   print('access_token is none')

if __name__ == '__main__':

x = input('ocr form clipboard image: z to ocr, q to quit-->')
 while(x != 'q'):
   if x=='z':
     ocr_clipboard()
   x = input('ocr from clipboard image: r to ocr, q to quit-->')
 print('bye')

来源:https://blog.csdn.net/onestab/article/details/79091289

标签:python,OCR,API,文字识别
0
投稿

猜你喜欢

  • Python flask使用ajax上传文件的示例代码

    2021-06-25 17:40:29
  • PHP编程实现多维数组按照某个键值排序的方法小结【2种方法】

    2023-09-11 08:28:26
  • Python常见数据类型转换操作示例

    2022-11-20 19:13:05
  • python通过装饰器检查函数参数数据类型的方法

    2022-07-22 14:11:08
  • Ubuntu20.04环境安装tensorflow2的方法步骤

    2023-07-04 06:41:21
  • Python3.7 读取 mp3 音频文件生成波形图效果

    2022-09-08 18:02:25
  • Facebook:产品设计评价体系解密

    2011-05-24 17:13:00
  • Python实现关键路径和七格图计算详解

    2022-04-25 12:17:46
  • SQL Server 数据库管理常用的SQL和T-SQL语句

    2024-01-27 22:01:09
  • python中的eval函数使用实例

    2021-06-20 00:34:07
  • Python基础之getpass模块详细介绍

    2021-03-06 13:47:13
  • python GUI库图形界面开发之PyQt5拖放控件实例详解

    2023-04-26 08:43:24
  • Python设计模式编程中解释器模式的简单程序示例分享

    2023-01-16 08:44:29
  • WEB2.0网页制作标准教程(8)CSS布局入门

    2007-09-11 13:21:00
  • python pygame实现挡板弹球游戏

    2022-11-25 17:50:42
  • vue项目中使用axios遇到的相对路径和绝对路径问题

    2024-05-13 09:37:40
  • 简单的Vue SSR的示例代码

    2023-07-02 17:08:46
  • 浅谈Python的list中的选取范围

    2023-06-23 01:58:01
  • 如何提升JavaScript的运行速度(DOM篇)[译]

    2009-02-25 12:24:00
  • Python yield 关键词,

    2021-01-20 07:14:46
  • asp之家 网络编程 m.aspxhome.com