亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python實現(xiàn)連續(xù)圖文識別

 更新時間:2018年12月18日 08:41:09   作者:布衣弓長  
這篇文章主要為大家詳細介紹了python實現(xiàn)連續(xù)圖文識別功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python實現(xiàn)連續(xù)圖文識別的具體代碼,供大家參考,具體內(nèi)容如下

1.工具:

1.1 剪切板。我下載并安裝使用的是剪切板查看器(clipbrd.exe),成功后顯示“剪貼薄查看器.exe”

1.2  截圖工具并設置熱鍵。保存圖片鍵和退出鍵可任意設置,注意不能同其它熱鍵沖突。我使用的是微信截圖,進入設置---進入快捷按鍵---把截取屏幕鍵改為F1。

1.3 Python 3.x,Windows環(huán)境

1.4 注冊百度云帳號,獲取Appid  API Key   Secret Key 

1.5 新建文件夾。我建的名叫‘圖文識別'文件夾(C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\圖文識別)

1.6 ‘圖文識別'文件夾下,有3個.py文件,分別是:screenshot.py  baiduap.py  getText.py;有1個配置文本文件是password.ini;有個圖像文件是Picture.png。

1.7 需要有以下第三方庫:keyboard、PIL、aip、configparser、win32con、win32clipboard。

2 完整代碼:

2.1 screenshot.py是主程序,可獨立運行,主要功能為截圖并保存。

""" 本程序可獨立運行,主要功能是截圖并保存"""
import sys
from time import sleep
import keyboard
from PIL import ImageGrab #pillow
from baiduap import BaiDuAPI
from getText import GetText
 
def screenShot():
   """用于截圖并保存"""
   print('請按F1開始截圖')
   if keyboard.wait(hotkey='f1')==None:
     print('復制剪切板的圖片,請按Ctrl+b,不復制繼續(xù)截圖')
     if keyboard.wait(hotkey='Ctrl+b')==None:
        sleep(0.02) #防止獲取的是上一張截圖
        #復制剪貼板里面的圖片
        im=ImageGrab.grabclipboard()
        im.save('Picture.png')
 
if __name__=='__main__':
   baiduapi=BaiDuAPI('password.ini')
   for _ in range(sys.maxsize):
     screenShot()
     texts=baiduapi.picture2Text('Picture.png') 
     print(texts)
     GetText.setText(texts)  #剪貼板剪貼
     sleep(0.02)
     GetText.getText()
     print('退出請按Ctrl+x') 
     if keyboard.wait(hotkey='Ctrl+x')==None:
        name=input('請輸入保存圖像識別文字文件名:')
        f=open(name+'.txt','w')
        f.write(texts)
        f.close()
        break

2.2 baiduap.py 程序可獨立使用,主要功能是圖像文字識別。

from aip import AipOcr
import configparser
 
class BaiDuAPI:
   """圖片文字識別"""
   #初識化方法
   def __init__(self,filePath): #self 就是BaiDuAPI()
     #讀取工單信息
     target=configparser.ConfigParser()
     target.read(filePath)
     app_id=target.get('我的工單','App_ID')
     app_key=target.get('我的工單','App_KEY')
     secret_key=target.get('我的工單','SECRET_KEY')
     self.client=AipOcr( app_id, app_key,secret_key)
 
   def picture2Text(self,filePath):
     #讀取圖片
     image=self.getPicture(filePath)
     texts=self.client.basicGeneral(image)
     #print(texts['words_result'])
     allTexts=''
     for word in texts['words_result']:
        allTexts=allTexts+word.get('words','')
     return allTexts
    
   @staticmethod  
   def getPicture(filePath):
     with open(filePath,'rb') as fp:
        return fp.read()
 
if __name__=='__main__':
   baiduapi=BaiDuAPI('password.ini')
   print(baiduapi.picture2Text('Picture.png'))

2.3 getText.py 程序,主要功能是把圖像識別出來的文字,保存到剪切板。

import sys 
import os.path 
import win32clipboard as w  
import win32con 
 
class GetText:
   
   def getText():#讀取剪切板 
     w.OpenClipboard() 
     d = w.GetClipboardData(win32con.CF_TEXT) 
     w.CloseClipboard() 
     return d 
   def setText(aString):#寫入剪切板 
     w.OpenClipboard() 
     w.EmptyClipboard() 
     w.SetClipboardText(aString) 
     w.CloseClipboard() 
 
if __name__=='__main__': 
   GetText.setText('布衣弓長')
   GetText.getText()

2.4 password.ini 文件,用記事本編寫,把百度云獲取的相關(guān)信息填進去。內(nèi)容是:

[我的工單];節(jié)
App_ID=151313**
App_KEY=1V2LlBhLUYaHu2Y9*******
SECRET_KEY=fGufC1CbiZ0tw1imTGoIsaGO******

3.運行。啟動qq和剪貼薄查看器.exe,在python環(huán)境下運行screenshot.py。

經(jīng)實測:識別率高,能快速抓取多圖,但每運行一次程序,只能識別保存在Picture.png文件里圖像的文字。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論