2016-11-12 202 views
1

我正在嘗試通過Python 2.7接口使用OpenCV實現基於機器學習的OCR應用程序來解析圖像文件中的文本。我正在使用this tutorial(爲方便起見,我已經轉發了以下代碼)。我對機器學習是全新的,對於OpenCV來說也相對較新。Python/OpenCV - 基於機器學習的OCR(圖像到文本)

手寫位數的OCR:

import numpy as np 
import cv2 
from matplotlib import pyplot as plt 

img = cv2.imread('digits.png') 
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 

# Now we split the image to 5000 cells, each 20x20 size 
cells = [np.hsplit(row,100) for row in np.vsplit(gray,50)] 

# Make it into a Numpy array. It size will be (50,100,20,20) 
x = np.array(cells) 

# Now we prepare train_data and test_data. 
train = x[:,:50].reshape(-1,400).astype(np.float32) # Size = (2500,400) 
test = x[:,50:100].reshape(-1,400).astype(np.float32) # Size = (2500,400) 

# Create labels for train and test data 
k = np.arange(10) 
train_labels = np.repeat(k,250)[:,np.newaxis] 
test_labels = train_labels.copy() 

# Initiate kNN, train the data, then test it with test data for k=1 
knn = cv2.KNearest() 
knn.train(train,train_labels) 
ret,result,neighbours,dist = knn.find_nearest(test,k=5) 

# Now we check the accuracy of classification 
# For that, compare the result with test_labels and check which are wrong 
matches = result==test_labels 
correct = np.count_nonzero(matches) 
accuracy = correct*100.0/result.size 
print accuracy 

# save the data 
np.savez('knn_data.npz',train=train, train_labels=train_labels) 

# Now load the data 
with np.load('knn_data.npz') as data: 
    print data.files 
    train = data['train'] 
    train_labels = data['train_labels'] 

英文字母的OCR:

import cv2 
import numpy as np 
import matplotlib.pyplot as plt 

# Load the data, converters convert the letter to a number 
data= np.loadtxt('letter-recognition.data', dtype= 'float32', delimiter = ',', 
        converters= {0: lambda ch: ord(ch)-ord('A')}) 

# split the data to two, 10000 each for train and test 
train, test = np.vsplit(data,2) 

# split trainData and testData to features and responses 
responses, trainData = np.hsplit(train,[1]) 
labels, testData = np.hsplit(test,[1]) 

# Initiate the kNN, classify, measure accuracy. 
knn = cv2.KNearest() 
knn.train(trainData, responses) 
ret, result, neighbours, dist = knn.find_nearest(testData, k=5) 

correct = np.count_nonzero(result == labels) 
accuracy = correct*100.0/10000 
print accuracy 

第二屆代碼片段(爲英文字母)需要從.data文件按以下格式輸入:

T,2,8,3,5,1,8,13,0,6,6,10,8,0,8,0,8 
I,5,12,3,7,2,10,5,5,4,13,3,9,2,8,4,10 
D,4,11,6,8,6,10,6,2,6,10,3,7,3,7,3,9 
N,7,11,6,6,3,5,9,4,6,4,4,10,6,10,2,8 
G,2,1,3,1,1,8,6,6,6,6,5,9,1,7,5,10 
S,4,11,5,8,3,8,8,6,9,5,6,6,0,8,9,7 
B,4,2,5,4,4,8,7,6,6,7,6,6,2,8,7,10 

......那裏約有20000行。數據描述了角色的輪廓。

我對這是如何工作有一個基本的把握,但我很困惑,我可以如何使用它來實際上在圖像上執行OCR。如何使用此代碼編寫以cv2圖像作爲參數並返回表示識別文本的字符串的函數?

回答

3

一般來說,機器學習的工作原理是這樣的:首先,你必須訓練你的程序以理解你的問題的領域。然後你開始提問。

所以,如果你正在創建一個OCR,第一步是教你的程序看起來像一個字母,B等等。

您可以使用OpenCV從噪聲中清除圖像,並確定可能是字母並將其隔離的像素組。

然後你將這些字母送到你的OCR程序。在訓練模式中,您將提供圖像並解釋圖像代表的字母。在詢問模式下,您將會提供圖像並詢問它是哪個字母。訓練越好,答案就越準確(程序可能會弄錯字母,總會有這種機會)。

+0

我想你誤解了我的問題;我明白這就是機器學習的原理。我不太瞭解本教程示例代碼中的數據結構。實際上,我對第一部分(對於數字)進行了計算,但對於第二部分(對於英文字母),它從'.data'文件讀取輪廓數據,而不是分析圖像。所以,一旦我訓練它,我如何使用它來解析圖像中的文本?我從哪個數據結構中提取字符? –

+0

我已經更新了我的問題,並附上了字母識別訓練'.data'文件的摘錄。 –

+0

對不起,我的困惑。這個.data文件是已處理的字母集合。你可以通過這個鏈接http://archive.ics.uci.edu/ml/datasets/Letter+Recognition找出數字是什麼。用簡單的英語,用openCV來隔離字母並測量字母。你可以採取一個新的字母,並採取相同的測量,然後將其與這個數據集進行比較,找出哪個字母。本教程不包括:/ – Rodrigo