Python OpenCV OCR数字数组不按顺序打印for循环

时间:2017-10-04 02:34:33

标签: python arrays opencv numpy

对于手写数字的Python 2.7 OCR,我有大约55行代码可能是一个简单的问题。我从博客中获取此代码,并将其用于业余爱好目的。我正在利用cv2,sklearn,skimage和numpy协助数字识别。

我对这里的代码有一个简单的问题 - 在for循环结束时,我追加了#34;识别的数字"从sklearn到numpy数组。这很好,但是,数字都是乱序的。例如,如果我上传的图片有手写的" 9 8 7 5 4 3"它将打印为[5,4,3,9,7,8]

我已经盯着这一段时间了,而我似乎无法弄清楚它为什么会循环播放"乱序。"我不知道OpenCV是如何检测数字的,或者它是否是sklearn的函数 - 或者只是一个简单的逻辑问题。

以下是代码(问题是我最后的问题 - 附加到数组):

# Import the modules
import cv2
from sklearn.externals import joblib
from skimage.feature import hog
import numpy as np

# Load the classifier
clf = joblib.load("digits_cls.pkl")

# Read the input image 
im = cv2.imread("4.jpg")

# Convert to grayscale and apply Gaussian filtering
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
im_gray = cv2.GaussianBlur(im_gray, (5, 5), 0)


# Threshold the image
ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV)


cv2.imshow("Threshhold/gray", im_th)

# Find contours in the image
hier, ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, 
cv2.CHAIN_APPROX_SIMPLE)

# Get rectangles contains each contour
rects = [cv2.boundingRect(ctr) for ctr in ctrs]

# For each rectangular region, calculate HOG features and predict
# the digit using Linear SVM.

numlist = []
for rect in rects:
    # Draw the rectangles
    cv2.rectangle(im, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + 
    rect[3]), (0, 255, 0), 3)
    # Make the rectangular region around the digit
    leng = int(rect[3] * 1.6)
    pt1 = int(rect[1] + rect[3] // 2 - leng // 2)
    pt2 = int(rect[0] + rect[2] // 2 - leng // 2)
    roi = im_th[pt1:pt1+leng, pt2:pt2+leng]
    # Resize the image
    roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
    roi = cv2.dilate(roi, (3, 3))
    # Calculate the HOG features
    roi_hog_fd = hog(roi, orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False)
    nbr = clf.predict(np.array([roi_hog_fd], 'float64'))
    cv2.putText(im, str(int(nbr[0])), (rect[0], rect[1]),cv2.FONT_HERSHEY_DUPLEX, 2, (0, 255, 255), 3)


    # Appending output to array for further processing
    number = (int(nbr[0]))
    numlist.append(number)

print numlist

cv2.imshow("Resulting Image with Rectangular ROIs", im)
#cv2.destroyAllWindows()
cv2.waitKey()

1 个答案:

答案 0 :(得分:1)

你应该在ocr之前按x值排序。

rects = sorted(rects, key = lambda rect: rect[0] + rect[2]//2)