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

首頁 > 編程 > Python > 正文

python 3調(diào)用百度OCR API實(shí)現(xiàn)剪貼板文字識(shí)別

2020-01-04 14:35:19
字體:
供稿:網(wǎng)友

本程序調(diào)用百度OCR API對(duì)剪貼板的圖片文字識(shí)別,配合CaptureScreen軟件,可快速識(shí)別文字。

#!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')

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 额尔古纳市| 枣阳市| 景宁| 宁乡县| 涿鹿县| 温州市| 蕲春县| 滨州市| 乐东| 谷城县| 临夏市| 普陀区| 千阳县| 潜江市| 安西县| 江源县| 焦作市| 永宁县| 通州市| 蓬溪县| 永平县| 丰原市| 固阳县| 通榆县| 佛学| 盘锦市| 伊金霍洛旗| 常山县| 广灵县| 区。| 象山县| 南昌县| 阿图什市| 平度市| 屯门区| 元谋县| 天等县| 乐清市| 绥江县| 禹州市| 嘉荫县|