def read_test_xy(filenames=None): n = len(filenames) batch_size = n batch_x = np.zeros([batch_size, Config.image_h * Config.image_w]) batch_y = np.zeros([batch_size, Config.max_captcha * Config.char_set_len]) def get_image_sample(file): # image = cv2.imread(file) image = Image.open(file) # image.show() image = np.array(image) filename = os.path.basename(file) text = filename.replace('.png', '') return text, image for i in range(batch_size): text, image = get_image_sample(filenames[i]) # 去噪、灰度、二值化处理 image = nd.noise_del(image) image = cut_box(image, resize=(Config.image_w, Config.image_h)) # 剪切 image = nd.convert2gray(image) # 三维变2维 # image = nd.binarizing(image, threshold=200, cov=False) # image1 = Image.fromarray(image) # image1.show() # batch_x[i, :] = (image.flatten() - 128) / 128 # image.flatten() / 255 # mean为0 batch_x[ i, :] = image.flatten() / 255 # (image.flatten()-128)/128 mean为0 batch_y[i, :] = text2vec(text) return batch_x, batch_y
def get_next_batch(batch_size=128): batch_x = np.zeros([batch_size, Config.image_h * Config.image_w]) batch_y = np.zeros([batch_size, Config.max_captcha * Config.char_set_len]) # 有时生成图像大小不是(60, 160, 3) def wrap_gen_captcha_text_and_image(): while True: text = random_captcha_text(captcha_size=4) image = gen_captcha_text_and_image(text) if image.shape == (30, 106, 3): return text, image for i in range(batch_size): text, image = wrap_gen_captcha_text_and_image() if batch_size == 1: print('test text:', text) # image1 = Image.fromarray(image) # image1.show() # 去噪、灰度、二值化处理 image = nd.noise_del(image) image = cut_box(image, resize=(Config.image_w, Config.image_h)) # 剪切最小框图 image = nd.convert2gray(image) # 三维变2维 # image = nd.binarizing(image,threshold=200, cov=False) # image1 = Image.fromarray(image) # image1.show() batch_x[ i, :] = image.flatten() / 255 # (image.flatten()-128)/128 mean为0 batch_y[i, :] = text2vec(text) return batch_x, batch_y
def predict_img(img, clf): text = '' image = nd.noise_del(img) image = cut_box(image) image_list = seg_img(image) for im in image_list: im = nd.convert2gray(im) im = im.reshape(-1) c = clf.predict([im,])[0] text += c return text
def img_cut_to_arry(img): imgs = [] image = nd.noise_del(img) # ima = Image.fromarray(image,'RGB') # ima.show() image = cut_box(image) # ima = Image.fromarray(image) # ima.show() image_list = seg_img(image) for im in image_list: im = nd.convert2gray(im) # ima = Image.fromarray(im) # ima.show() im = im.reshape(-1) imgs.append(im) return imgs
def get_text_xy(text): batch_x = np.zeros([1, Config.image_h * Config.image_w]) batch_y = np.zeros([1, Config.max_captcha * Config.char_set_len]) image = gen_captcha_text_and_image(text) # 去噪、灰度、二值化处理 image = nd.noise_del(image) image = cut_box(image, resize=(Config.image_w, Config.image_h)) # 剪切 image = nd.convert2gray(image) # 三维变2维 # image = nd.binarizing(image, threshold=200, cov=False) # image1 = Image.fromarray(image) # image1.show() batch_x[0, :] = image.flatten() / 255 # (image.flatten()-128)/128 mean为0 batch_y[0, :] = text2vec(text) return batch_x, batch_y