python 3調(diào)用百度OCR API實(shí)現(xiàn)剪貼板文字識(shí)別
本程序調(diào)用百度OCR API對(duì)剪貼板的圖片文字識(shí)別,配合CaptureScreen軟件,可快速識(shí)別文字。
#!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')
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python 圖片文字識(shí)別的實(shí)現(xiàn)之PaddleOCR
- Python 實(shí)現(xiàn)任意區(qū)域文字識(shí)別(OCR)操作
- Python3使用騰訊云文字識(shí)別(騰訊OCR)提取圖片中的文字內(nèi)容實(shí)例詳解
- Python圖像處理之圖片文字識(shí)別功能(OCR)
- Python調(diào)用百度OCR實(shí)現(xiàn)圖片文字識(shí)別的示例代碼
- Python基于百度AI實(shí)現(xiàn)OCR文字識(shí)別
- python調(diào)用文字識(shí)別OCR輕松搞定驗(yàn)證碼
- 基于Python實(shí)現(xiàn)圖像文字識(shí)別OCR工具
- python實(shí)戰(zhàn)教程之OCR文字識(shí)別方法匯總
相關(guān)文章
利用Python將彩色圖像轉(zhuǎn)為灰度圖像的兩種方法
這篇文章主要給大家介紹了關(guān)于利用Python將彩色圖像轉(zhuǎn)為灰度圖像的兩種方法,以及python 批量將圖片轉(zhuǎn)為灰度圖的方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12
pytorch GAN偽造手寫體mnist數(shù)據(jù)集方式
今天小編就為大家分享一篇pytorch GAN偽造手寫體mnist數(shù)據(jù)集方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python如何將jpg圖像修改大小并轉(zhuǎn)換為png
這篇文章主要介紹了Python如何將jpg圖像修改大小并轉(zhuǎn)換為png問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Python語言檢測(cè)模塊langid和langdetect的使用實(shí)例
今天小編就為大家分享一篇關(guān)于Python語言檢測(cè)模塊langid和langdetect的使用實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02
Python-Tkinter Text輸入內(nèi)容在界面顯示的實(shí)例
今天小編就為大家分享一篇Python-Tkinter Text輸入內(nèi)容在界面顯示的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07
python實(shí)現(xiàn)給數(shù)組按片賦值的方法
這篇文章主要介紹了python實(shí)現(xiàn)給數(shù)組按片賦值的方法,實(shí)例分析了Python在指定位置進(jìn)行賦值的相關(guān)技巧,需要的朋友可以參考下2015-07-07
pandas中concatenate和combine_first的用法詳解
本文主要介紹了pandas中concatenate和combine_first的用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

