Python 實(shí)現(xiàn)任意區(qū)域文字識(shí)別(OCR)操作
本文的OCR當(dāng)然不是自己從頭開(kāi)發(fā)的,是基于百度智能云提供的API(我感覺(jué)是百度在中國(guó)的人工智能領(lǐng)域值得稱(chēng)贊的一大貢獻(xiàn)),其提供的API完全可以滿(mǎn)足個(gè)人使用,相對(duì)來(lái)說(shuō)簡(jiǎn)潔準(zhǔn)確率高。
安裝OCR Python SDK
OCR Python SDK目錄結(jié)構(gòu)
├── README.md ├── aip //SDK目錄 │ ├── __init__.py //導(dǎo)出類(lèi) │ ├── base.py //aip基類(lèi) │ ├── http.py //http請(qǐng)求 │ └── ocr.py //OCR └── setup.py //setuptools安裝
支持Python版本:2.7.+ ,3.+
安裝使用Python SDK有如下方式:
如果已安裝pip,執(zhí)行pip install baidu-aip即可。
如果已安裝setuptools,下載后執(zhí)行python setup.py install即可。
代碼實(shí)現(xiàn)
下面讓我們來(lái)看一下代碼實(shí)現(xiàn)。
主要使用的模塊有
import os # 操作系統(tǒng)相關(guān) import sys # 系統(tǒng)相關(guān) import time # 時(shí)間獲取 import signal # 系統(tǒng)信號(hào) import winsound # 提示音 from aip import AipOcr # 百度OCR API from PIL import ImageGrab # 捕獲剪切板中的圖片 import win32clipboard as wc # WINDOWS 剪切板操作 import win32con # 這里用于獲取 WINDOWS 剪貼板數(shù)據(jù)的標(biāo)準(zhǔn)格式
第一步 這里的APP_ID,API_KEY,SECRET_KEY是通過(guò)登陸百度智能云后自己在OCR板塊申請(qǐng)的, 實(shí)現(xiàn)基本的OCR程序,可以通過(guò)圖片獲取文字。
""" 你的 APPID AK SK """ APP_ID = 'xxx' API_KEY = 'xxx' SECRET_KEY = 'xxx' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) """ 讀取圖片 """ def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() """ 從API的返回字典中獲取文字 """ def getOcrText(txt_dict): txt = "" if type(txt_dict) == dict: for i in txt_dict['words_result']: txt = txt + i["words"] if len(i["words"]) < 25: # 這里使用字符串長(zhǎng)度決定了文本是否換行,讀者可以根據(jù)自己的喜好控制回車(chē)符的輸出,實(shí)現(xiàn)可控的文本顯示形式 txt = txt + "\n\n" return txt """ 調(diào)用通用/高精度文字識(shí)別, 圖片參數(shù)為本地圖片 """ def BaiduOcr(imageName,Accurate=True): image = get_file_content(imageName) if Accurate: return getOcrText(client.basicGeneral(image)) else: return getOcrText(client.basicAccurate(image)) """ 帶參數(shù)調(diào)用通用文字識(shí)別, 圖片參數(shù)為遠(yuǎn)程url圖片 """ def BaiduOcrUrl(url): return getOcrText(client.basicGeneralUrl(url))
第二步,實(shí)現(xiàn)快捷鍵獲取文字,將識(shí)別文字放入剪切板中,提示音提醒以及快捷鍵退出程序
""" 剪切板操作函數(shù) """
def get_clipboard():
wc.OpenClipboard()
txt = wc.GetClipboardData(win32con.CF_UNICODETEXT)
wc.CloseClipboard()
return txt
def empty_clipboard():
wc.OpenClipboard()
wc.EmptyClipboard()
wc.CloseClipboard()
def set_clipboard(txt):
wc.OpenClipboard()
wc.EmptyClipboard()
wc.SetClipboardData(win32con.CF_UNICODETEXT, txt)
wc.CloseClipboard()
""" 截圖后,調(diào)用通用/高精度文字識(shí)別"""
def BaiduOcrScreenshots(Accurate=True,path="./",ifauto=False):
if not os.path.exists(path):
os.makedirs(path)
image = ImageGrab.grabclipboard()
if image != None:
print("\rThe image has been obtained. Please wait a moment!",end=" ")
filename = str(time.time_ns())
image.save(path+filename+".png")
if Accurate:
txt = getOcrText(client.basicAccurate(get_file_content(path+filename+".png")))
else:
txt = getOcrText(client.basicGeneral(get_file_content(path+filename+".png")))
os.remove(path+filename+".png")
# f = open(os.path.abspath(path)+"\\"+filename+".txt",'w')
# f.write(txt)
set_clipboard(txt)
winsound.PlaySound('SystemAsterisk',winsound.SND_ASYNC)
# os.startfile(os.path.abspath(path)+"\\"+filename+".txt")
# empty_clipboard()
return txt
else :
if not ifauto:
print("Please get the screenshots by Shift+Win+S! ",end="")
return ""
else:
print("\rPlease get the screenshots by Shift+Win+S ! ",end="")
def sig_handler(signum, frame):
sys.exit(0)
def removeTempFile(file = [".txt",".png"],path="./"):
if not os.path.exists(path):
os.makedirs(path)
pathDir = os.listdir(path)
for i in pathDir:
for j in file:
if j in i:
os.remove(path+i)
def AutoOcrFile(path="./",filetype=[".png",".jpg",".bmp"]):
if not os.path.exists(path):
os.makedirs(path)
pathDir = os.listdir(path)
for i in pathDir:
for j in filetype:
if j in i:
f = open(os.path.abspath(path)+"\\"+str(time.time_ns())+".txt",'w')
f.write(BaiduOcr(path+i))
break
def AutoOcrScreenshots():
signal.signal(signal.SIGINT, sig_handler)
signal.signal(signal.SIGTERM, sig_handler)
print("Waiting For Ctrl+C to exit ater removing all picture files and txt files!")
print("Please get the screenshots by Shift+Win+S !",end="")
while(1):
try:
BaiduOcrScreenshots(ifauto=True)
time.sleep(0.1)
except SystemExit:
removeTempFile()
break
else :
pass
finally:
pass
最終運(yùn)行函數(shù) AutoOcrScreenshots 函數(shù)便可以實(shí)現(xiàn)了:
if __name__ == '__main__': AutoOcrScreenshots()
使用方法
使用 Windows 10 系統(tǒng)時(shí),將以上代碼放置在一個(gè) .py 文件下,然后運(yùn)行便可以使用Shift+Win+S快捷鍵實(shí)現(xiàn)任意區(qū)域截取,截取后圖片將暫時(shí)存放在剪切板中,程序自動(dòng)使用Windows API獲取圖片內(nèi)容,之后使用百度的OCR API獲取文字,并將文字放置在剪切版內(nèi)存中后發(fā)出提示音。
使用者則可以在開(kāi)啟程序后,使用快捷鍵截圖后靜待提示音后使用Ctrl+V將文字內(nèi)容放置在自己所需的位置。
補(bǔ)充:Python 中文OCR
有個(gè)需求,需要從一張圖片中識(shí)別出中文,通過(guò)python來(lái)實(shí)現(xiàn),這種這么高大上的黑科技我們普通人自然搞不了,去github找了一個(gè)似乎能滿(mǎn)足需求的開(kāi)源庫(kù)-tesseract-ocr:
Tesseract的OCR引擎目前已作為開(kāi)源項(xiàng)目發(fā)布在Google Project,其項(xiàng)目主頁(yè)在這里查看https://github.com/tesseract-ocr,
它支持中文OCR,并提供了一個(gè)命令行工具。python中對(duì)應(yīng)的包是pytesseract. 通過(guò)這個(gè)工具我們可以識(shí)別圖片上的文字。
筆者的開(kāi)發(fā)環(huán)境如下:
macosx
python 3.6
brew
安裝tesseract
brew install tesseract
安裝python對(duì)應(yīng)的包:pytesseract
pip install pytesseract

怎么用?
如果要識(shí)別中文需要下載對(duì)應(yīng)的訓(xùn)練集:https://github.com/tesseract-ocr/tessdata,下載”chi_sim.traineddata”,然后copy到訓(xùn)練數(shù)據(jù)集的存放路徑,如:


具體代碼就幾行:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pytesseract
from PIL import Image
# open image
image = Image.open('test.png')
code = pytesseract.image_to_string(image, lang='chi_sim')
print(code)
OCR速度比較慢,大家可以拿一張包含中文的圖片試驗(yàn)一下。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
- python 識(shí)別圖片中的文字信息方法
- Python圖像處理之識(shí)別圖像中的文字(實(shí)例講解)
- python實(shí)現(xiàn)簡(jiǎn)單的文字識(shí)別
- 如何使用Python進(jìn)行OCR識(shí)別圖片中的文字
- python批量識(shí)別圖片指定區(qū)域文字內(nèi)容
- python識(shí)別圖像并提取文字的實(shí)現(xiàn)方法
- 如何利用Python識(shí)別圖片中的文字
- 如何利用Python識(shí)別圖片中的文字詳解
- Python 圖片文字識(shí)別的實(shí)現(xiàn)之PaddleOCR
- Python圖片文字識(shí)別與提取實(shí)戰(zhàn)記錄
相關(guān)文章
pandas實(shí)戰(zhàn):分析三國(guó)志人物示例實(shí)現(xiàn)
這篇文章主要介紹了pandas實(shí)戰(zhàn):分析三國(guó)志人物示例實(shí)現(xiàn),本文章內(nèi)容詳細(xì),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,需要的朋友可以參考下2023-01-01
使用Matplotlib 繪制精美的數(shù)學(xué)圖形例子
今天小編就為大家分享一篇使用Matplotlib 繪制精美的數(shù)學(xué)圖形例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
Python常用模塊之threading和Thread模塊及線(xiàn)程通信
這篇文章主要介紹了Python常用模塊之threading和Thread模塊及線(xiàn)程通信,文章為圍繞主題的相關(guān)內(nèi)容展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友看可以參考一下方法2022-06-06
Python對(duì)中國(guó)500強(qiáng)排行榜數(shù)據(jù)進(jìn)行可視化分析實(shí)戰(zhàn)
這篇文章主要介紹了Python對(duì)中國(guó)500強(qiáng)排行榜數(shù)據(jù)進(jìn)行可視化分析實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

