def rect_boundary2(img, show=False): # 通过HSV获取黑色部分 converted = preprocess.convert_red_to_black(img, show) mat = preprocess.get_black_mat(converted, show=show) angle, rotated = utils.correct_skew(mat, is_gray=True) if show: cv2.imshow('rotated', rotated) # 水平投影 v, groups, counts = preprocess.line_shadow(rotated, color=255)
if h1 >= heightMedium / 2: wordRects.append(t) # 步骤6:通过字符的宽度判断是否包含多个字符 wordRects = _validate_by_width(closed, wordRects, words=words) # 步骤7:通过间隔判断不合理的数据 wordRects = _validate_by_interval(wordRects) # 步骤8:重新二值化后再次收缩字符范围 for i in range(0, len(wordRects)): r = wordRects[i] r1 = tight_word(gray, r) wordRects[i] = r1 if show: for (x1, y1, w1, h1) in wordRects: cv2.rectangle(img, (x1, y1), (x1 + w1, y1 + h1), (0, 0, 255), 2) # 用矩形显示最终字符 cv2.imshow('words', img) return wordRects # 返回每个字符的(x,y,w,h) if __name__ == '__main__': # img = cv2.imread('./test0309/34.jpg') # 4,29,34,36,37,44,45,46 # img = cv2.imread('./area/13.png') # 5,18,31,42,51,54,55 img = cv2.imread('./area/1.png') # 34,37,45,46 angle, img = utils.correct_skew(img, is_gray=False) cv2.imshow('skew', img) oriHeight, oriWidth = img.shape[:2] resizedHeight = int(oriHeight / (oriWidth / float(800))) img = cv2.resize(img, (800, resizedHeight)) # 将图片宽度固定为800 img_to_words(img, show=True, words=6) cv2.waitKey(0) cv2.destroyAllWindows()
import numpy as np import wordSplit import utils #TRAINING--------------------------------------------------------------------------------------------------------------- samples = np.loadtxt('./data/general_samples.data', np.float32) responses = np.loadtxt('./data/general_responses.data', np.float32) responses = responses.reshape((responses.size, 1)) model = cv2.ml.KNearest_create() model.train(samples, cv2.ml.ROW_SAMPLE, responses) if __name__ == '__main__': show = False imgName = '26.png' img = cv2.imread('./area/{}'.format(imgName)) angle, img = utils.correct_skew(img) oriHeight, oriWidth = img.shape[:2] resizedHeight = int(oriHeight / (oriWidth / float(800))) img = cv2.resize(img, (800, resizedHeight)) # 将图片宽度固定为800 wordRects = wordSplit.img_to_words(img, show) # 字符分割 gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # 把输入图像灰度化 utils.color_reverse(gray) gris = cv2.GaussianBlur(gray, (3, 3), 0) # 高斯滤波 chars = [] for (x, y, w, h) in wordRects: roi = gris[y:y + h, x:x + w] roi = support_library.recon_borde(roi) roi_small = cv2.resize(roi, (10, 10)) roi_small = roi_small.reshape((1, 100)) roi_small = np.float32(roi_small)
easyreader = easyocr.Reader(['en']) easyresult = easyreader.readtext("cropped_image.jpg") easywiskew = [] for e in easyresult: easywiskew.append(e[1].upper()) pipeline = keras_ocr.pipeline.Pipeline() images = ['cropped_image.jpg'] prediction = pipeline.recognize(images)[0] keraswiskew = [] for p in prediction: keraswiskew.append(p[0].upper()) # Correcting Skewness image = cv2.imread("cropped_image.jpg") angle, rotated = utils.correct_skew(image) cv2.imwrite("rotated_image.jpg", rotated) # With Skewness outputs easyreader = easyocr.Reader(['en']) easyresult = easyreader.readtext("rotated_image.jpg") easynoskew = [] for e in easyresult: easynoskew.append(e[1].upper()) pipeline = keras_ocr.pipeline.Pipeline() images = ["rotated_image.jpg"] prediction = pipeline.recognize(images)[0] kerasnoskew = [] for p in prediction: kerasnoskew.append(p[0].upper())