Python/OpenCV - 基于机器学习的 OCR(图像到文本)

标签 python opencv machine-learning ocr

我正在尝试通过 Python 2.7 接口(interface)使用 OpenCV 来实现基于机器学习的 OCR 应用程序来解析图像文件中的文本。我正在使用 this tutorial (为方便起见,我重新发布了下面的代码)。我对机器学习完全陌生,对 OpenCV 也比较陌生。

手写数字的 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

...大约有 20,000 行。数据描述字符的轮廓。

我对它的工作原理有基本的了解,但我对如何使用它对图像实际执行 OCR 感到困惑。我如何使用这段代码编写一个函数,将 cv2 图像作为参数并返回表示已识别文本的字符串?

最佳答案

一般来说,机器学习是这样工作的:首先,您必须训练您的程序理解您的问题领域。然后你开始提问。

因此,如果您要创建 OCR,第一步就是教您的程序 A 字母是什么样子,B 字母是什么样子,等等。

您使用 OpenCV 清除图像中的噪声并识别可能是字母的像素组并将它们隔离。

然后将这些字母输入 OCR 程序。在训练模式下,您将输入图像并解释图像代表的字母。在询问模式下,您将输入图像并询问它是哪个字母。训练得越好,您的答案就越准确(程序可能会弄错字母,这种情况总是有可能的)。

关于Python/OpenCV - 基于机器学习的 OCR(图像到文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40561786/

相关文章:

python - 在列表中将姓氏、名字切换为名字姓氏

opencv - 使用 OpenCv 保存时图像透明度变暗

c++ - 使用 copyTo 函数断言失败 (C++)

opencv - 高斯平滑的方差 (sigma) 影响

python - 计算矩阵列平均值

image-processing - 用于多对象分类的支持向量机

python - 有效删除除重复结束字符之外的所有内容

python - 两个巨大稠密矩阵的乘法 Hadamard-乘以一个稀疏矩阵

python - 我如何在 TensorFlow 中使用我自己的图像?

python - 用于视频分类的 CNN LSTM keras