1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
import os import cv2 import numpy as np
def split_picture(imagepath):
gray = cv2.imread(imagepath, 0)
height, width = gray.shape for i in range(width): gray[0, i] = 255 gray[height-1, i] = 255 for j in range(height): gray[j, 0] = 255 gray[j, width-1] = 255
blur = cv2.medianBlur(gray, 3)
ret,thresh1 = cv2.threshold(blur, 200, 255, cv2.THRESH_BINARY)
chars_list = [] image, contours, hierarchy = cv2.findContours(thresh1, 2, 2) for cnt in contours: x, y, w, h = cv2.boundingRect(cnt) if x != 0 and y != 0 and w*h >= 100: chars_list.append((x,y,w,h))
sorted_chars_list = sorted(chars_list, key=lambda x:x[0]) for i,item in enumerate(sorted_chars_list): x, y, w, h = item cv2.imwrite('F://test_verifycode/chars/%d.jpg'%(i+1), thresh1[y:y+h, x:x+w])
def remove_edge_picture(imagepath):
image = cv2.imread(imagepath, 0) height, width = image.shape corner_list = [image[0,0] < 127, image[height-1, 0] < 127, image[0, width-1]<127, image[ height-1, width-1] < 127 ] if sum(corner_list) >= 3: os.remove(imagepath)
def resplit_with_parts(imagepath, parts): image = cv2.imread(imagepath, 0) os.remove(imagepath) height, width = image.shape
file_name = imagepath.split('/')[-1].split(r'.')[0] step = width//parts start = 0 for i in range(parts): cv2.imwrite('F://test_verifycode/chars/%s.jpg'%(file_name+'-'+str(i)), \ image[:, start:start+step]) start += step
def resplit(imagepath):
image = cv2.imread(imagepath, 0) height, width = image.shape
if width >= 64: resplit_with_parts(imagepath, 4) elif width >= 48: resplit_with_parts(imagepath, 3) elif width >= 26: resplit_with_parts(imagepath, 2)
def convert(dir, file):
imagepath = dir+'/'+file image = cv2.imread(imagepath, 0) ret, thresh = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY) img = cv2.resize(thresh, (16, 20), interpolation=cv2.INTER_AREA) cv2.imwrite('%s/%s' % (dir, file), img)
def Read_Data(dir, file):
imagepath = dir+'/'+file image = cv2.imread(imagepath, 0) ret, thresh = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY) bin_values = [1 if pixel==255 else 0 for pixel in thresh.ravel()]
return bin_values
def predict(VerifyCodePath):
dir = 'F://test_verifycode/chars' files = os.listdir(dir)
if files: for file in files: os.remove(dir + '/' + file)
split_picture(VerifyCodePath)
files = os.listdir(dir) if not files: print('查看的文件夹为空!') else:
for file in files: remove_edge_picture(dir + '/' + file)
for file in os.listdir(dir): resplit(dir + '/' + file)
for file in os.listdir(dir): convert(dir, file)
files = sorted(os.listdir(dir), key=lambda x: x[0]) table = np.array([Read_Data(dir, file) for file in files]).reshape(-1,20,16,1)
mp = 'F://verifycode_data/verifycode_Keras.h5' from keras.models import load_model cnn = load_model(mp) y_pred = cnn.predict(table) predictions = np.argmax(y_pred, axis=1)
keys = range(31) vals = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Y', 'Z'] label_dict = dict(zip(keys, vals))
return ''.join([label_dict[pred] for pred in predictions])
def main():
dir = 'F://VerifyCode/' correct = 0 for i, file in enumerate(os.listdir(dir)): true_label = file.split('.')[0] VerifyCodePath = dir+file pred = predict(VerifyCodePath)
if true_label == pred: correct += 1 print(i+1, (true_label, pred), true_label == pred, correct)
total = len(os.listdir(dir)) print('\n总共图片:%d张\n识别正确:%d张\n识别准确率:%.2f%%.'\ %(total, correct, correct*100/total))
main()
|