国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python 3調用百度OCR API實現剪貼板文字識別

2020-02-15 22:53:48
字體:
來源:轉載
供稿:網友

本程序調用百度OCR API對剪貼板的圖片文字識別,配合CaptureScreen軟件,可快速識別文字。

#!python3import urllib.request, urllib.parseimport os, io, sys, json, socketimport base64from 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')

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新邵县| 岚皋县| 屏东市| 贵阳市| 延寿县| 富源县| 化州市| 孟州市| 亳州市| 丰原市| 普兰县| 南雄市| 鄂尔多斯市| 桓台县| 手游| 塔河县| 资兴市| 措美县| 行唐县| 富蕴县| 抚松县| 密山市| 陈巴尔虎旗| 沈阳市| 且末县| 思南县| 卫辉市| 吉木乃县| 伊通| 韶关市| 台中市| 屯留县| 罗江县| 峨边| 集安市| 饶河县| 博野县| 泊头市| 临潭县| 夹江县| 高淳县|