光学字符检测效果不佳(TensorFlow和OpenCV)

时间:2019-04-29 11:06:06

标签: opencv tensorflow keras ocr object-detection

我正在尝试在图像上使用滑动窗口检测器来找到字母并对其进行预测。我已经使用TensorFlow和Keras训练了模型,并在测试集上获得了70%的分类精度。

分类算法仅限于20x20像素的灰度图像。因此,当使用带有openCV的滑动窗口时,需要调整窗口的大小,我需要检查其中的字符(如果不是20x20)。向下滚动到底部以查看问题图片!

我训练了分类算法的数据集都是20x20像素的灰度图像。

由于我在未应用任何特征工程的情况下对原始数据具有如此高的准确性,因此我选择仅将针对该数据训练的模型用于光学字符识别。

## Sliding window detection:

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

# read the image and define the stepSize and window size 
# (width,height)
image = cv2.imread("./detection-images/detection-1.jpg") # your image path
tmp = image # for drawing a rectangle
stepSize = 30
(w_width, w_height) = (30,30) # window size
class_letter = []
for x in range(0, image.shape[1] - w_width , stepSize):
    for y in range(0, image.shape[0] - w_height, stepSize):
        window = image[x:x + w_width, y:y + w_height, :]
        resize = cv2.resize(window,(20,20),interpolation =cv2.INTER_AREA)
        np.asarray(resize)
        window = resize[:, :, 0]
        window=np.reshape(window,20*20)
        window = pd.DataFrame(data=window).T
        list1 = (window.values[0])

        if all(x == list1[0] for x in list1):
            print(" ",end="\r")
            # print("All elements in list are same. No need for 
         prediction")
        else:
            prediction  = model_tensor.predict(window.values)
            class_letter.append(prediction)


        # knn
        # class_letter.append(test_classifier_sliding(neigh,window))

    # classify content of the window with your classifier and  
    # determine if the window includes an object (cell) or not
    # draw window on image
        cv2.rectangle(tmp, (x, y), (x + w_width, y + w_height), (255, 0, 0), 2) # draw rectangle on image
        plt.imshow(np.array(tmp).astype('uint8'))
# show all windows
plt.show()
# pred = test_classifier_sliding(neigh,find_letter)
# print(class_letter)

关于的代码是从其他地方复制的,但是我已经对其进行了调整,以调整每个窗口的大小,如您所见。

我猜想问题出在上面的代码中,而不是分类器中。

有什么我没想到的吗?目前,我没有任何正确的分类。

这是上面代码中带有设置的框的图像。

The image of the boxes with the setup in the code above.

我无法以某种方式在此窗口中显示图像。当然,应该轻松地对“ S”和“ E”进行分类吗?

0 个答案:

没有答案