python 3調(diào)用百度OCR API實(shí)現(xiàn)剪貼板文字識別
更新時間:2018年09月04日 11:08:33 作者:方工
這篇文章主要為大家詳細(xì)介紹了python 3調(diào)用百度OCR API實(shí)現(xiàn)剪貼板文字識別,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本程序調(diào)用百度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')
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python 圖片文字識別的實(shí)現(xiàn)之PaddleOCR
- Python 實(shí)現(xiàn)任意區(qū)域文字識別(OCR)操作
- Python3使用騰訊云文字識別(騰訊OCR)提取圖片中的文字內(nèi)容實(shí)例詳解
- Python圖像處理之圖片文字識別功能(OCR)
- Python調(diào)用百度OCR實(shí)現(xiàn)圖片文字識別的示例代碼
- Python基于百度AI實(shí)現(xiàn)OCR文字識別
- python調(diào)用文字識別OCR輕松搞定驗(yàn)證碼
- 基于Python實(shí)現(xiàn)圖像文字識別OCR工具
- python實(shí)戰(zhàn)教程之OCR文字識別方法匯總
相關(guān)文章
Python3 關(guān)于pycharm自動導(dǎo)入包快捷設(shè)置的方法
今天小編就為大家分享一篇Python3 關(guān)于pycharm自動導(dǎo)入包快捷設(shè)置的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
python 實(shí)現(xiàn)ping測試延遲的兩種方法
這篇文章主要介紹了python 實(shí)現(xiàn)ping測試延遲的兩種方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
2020-12-12
python獲取當(dāng)前用戶的主目錄路徑方法(推薦)
下面小編就為大家?guī)硪黄猵ython獲取當(dāng)前用戶的主目錄路徑方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
2017-01-01
Python求解排列中的逆序數(shù)個數(shù)實(shí)例
這篇文章主要介紹了Python求解排列中的逆序數(shù)個數(shù)實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
2020-05-05 
