def captcha2text(height=CAPTCHA_HEIGHT, width=CAPTCHA_WIDTH): ''' 验证码图片转化为文本 :param image_list: :param height: :param width: :return: ''' x = tf.placeholder(tf.float32, [None, height * width]) keep_prob = tf.placeholder(tf.float32) y_conv = cnn_graph(x, keep_prob, (height, width)) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, RESTORE_PATH) success = 0 error = 0 for num in range(0, len(os.listdir(IMAGE_TEST_PATH))): text, image = read_dir_image_for_position(num, IMAGE_TEST_PATH) image = convert2gray(image) image = image.flatten() / 255 image = [image] predict = tf.argmax( tf.reshape( y_conv, [-1, CAPTCHA_LEN, len(CAPTCHA_LIST)]), 2) vector_list = sess.run(predict, feed_dict={x: image, keep_prob: 1}) vector_list = vector_list.tolist() pre_text = [vec2text(vector) for vector in vector_list] if (text == ''.join(pre_text)): success += 1 else: error += 1 print('Label:', text, ' Predict:', pre_text) print('total:', success + error, ',success:', success, ',error:', error)
def getCaptcha(self): captcha = self.sess.get(captchaUrl, headers=accessHeaders) image = BytesIO(captcha.content) img = Image.open(image) imgArray = convert2gray(np.array(img)).flatten() / 255 captcha_text = self.capthca_break.predictByArray(imgArray) captcha_text = ''.join(captcha_text) return captcha_text
def predictaByPath(self, img): '''通过图片路径来预测验证码''' img = np.array(Image.open(img)) img = convert2gray(img).flatten() / 255 pred = self.pred.eval(feed_dict={self._x: [img], self._prob: 1}) captcha_text = [] for i, c in enumerate(pred): for j in range(MAX_CAPTCHA): captcha_text.append(chr(getIdx(c[j]))) return captcha_text
def image2text(image, height=CAPTCHA_HEIGHT, width=CAPTCHA_WIDTH): pre_text = '1111' tf.reset_default_graph() x = tf.placeholder(tf.float32, [None, height * width]) keep_prob = tf.placeholder(tf.float32) y_conv = cnn_graph(x, keep_prob, (height, width)) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, RESTORE_PATH) image = convert2gray(image) image = image.flatten() / 255 image = [image] predict = tf.argmax( tf.reshape(y_conv, [-1, CAPTCHA_LEN, len(CAPTCHA_LIST)]), 2) vector_list = sess.run(predict, feed_dict={x: image, keep_prob: 1}) vector_list = vector_list.tolist() pre_text = [vec2text(vector) for vector in vector_list] return pre_text
def getImage(self): count = 1 while count < 100000: try: captcha = self.sess.get(captchaUrl, headers=accessHeaders) image = BytesIO(captcha.content) img = Image.open(image) imgArray = convert2gray(np.array(img)).flatten() / 255 captcha_text = self.capthca_break.predictByArray(imgArray) captcha_text = ''.join(captcha_text) self.data['captcha'] = captcha_text res = self.sess.post( url=loginUrl, data=self.data, headers=loginHeaders) msg = re.findall('showmsg."(.*)",', res.text) print(msg) if len(msg) == 0: path = 'data/' + captcha_text + '.png' if not os.path.exists(path): img.save(path) print('img', str(count) + '.png', '预测值', captcha_text) count += 1 except requests.exceptions.ConnectionError as e: print(e)
if not isdir('./model'): print('Model directory does not exists.') return x = placeholder(float32, [None, height * width]) keep_prob = placeholder(float32) y_conv = cnn_graph(x, keep_prob, (height, width)) saver = Saver() with Session() as sess: saver.restore(sess, latest_checkpoint('./model/')) predict = argmax(reshape( y_conv, [-1, CAPTCHA_LEN, len(CAPTCHA_LIST)]), 2) vector_list = sess.run(predict, feed_dict={ x: image_list, keep_prob: 1 }) vector_list = vector_list.tolist() text_list = [vec2text(vector) for vector in vector_list] return text_list if __name__ == '__main__': text, image = gen_captcha_text_and_image() img = Image.fromarray(image) image = convert2gray(image) image = image.flatten() / 255 pre_text = captcha2text([image]) print('Text:', text, ', Actual Text:', pre_text, ', Result:', pre_text[0] == text) img.show()
with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint('model/')) predict = tf.argmax( tf.reshape(y_conv, [-1, CAPTCHA_LEN, len(CAPTCHA_LIST)]), 2) vector_list = sess.run(predict, feed_dict={ x: image_list, keep_prob: 1 }) vector_list = vector_list.tolist() text_list = [vec2text(vector) for vector in vector_list] return text_list if __name__ == '__main__': # text, image = gen_captcha_text_and_image() # img = Image.fromarray(image) # image = convert2gray(image) # image = image.flatten() / 255 # pre_text = captcha2text([image]) # print("验证码正确值:", text, ' 模型预测值:', pre_text , '正确与否:',pre_text[0] == text) # img.show() image = Image.open('./validImage/1.jpg') im = np.array(image) im = convert2gray(im) im = im.flatten() / 255 pre_text = captcha2text([im]) print("验证码正确值:", '4R62', ' 模型预测值:', pre_text[0], '正确与否:', pre_text[0] == '4R62')