实战七:手把手教你用TensorFlow进行验证码识别(下)

实战七:手把手教你用Tensorflow进行验证码识别(下)

github下载地址

目录

  • 准备模型开发环境
  • 生成验证码数据集
  • 输入与输出数据处理
  • 模型结构设计
  • 模型损失函数设计
  • 模型训练过程分析
  • 模型部署与效果演示

七、模型部署与效果演示

1.数据-模型-服务流水线

实战七:手把手教你用TensorFlow进行验证码识别(下)
2.启动一个flask服务

将下述代码另存为hello.py,然后本地python hello.py运行

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

实战七:手把手教你用TensorFlow进行验证码识别(下)
访问127.0.0.1:5000,便可看到结果

实战七:手把手教你用TensorFlow进行验证码识别(下)
3.部署验证码识别服务

将下述代码另外存app.py,启动flask来加载app.py文件

import base64

import numpy as np
import tensorflow as tf

from io import BytesIO
from flask import Flask, request, jsonify
from keras.models import load_model
from PIL import Image

NUMBER = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
LOWERCASE = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
            'v', 'w', 'x', 'y', 'z']
UPPERCASE = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
           'V', 'W', 'X', 'Y', 'Z']

CAPTCHA_CHARSET = NUMBER   # 验证码字符集
CAPTCHA_LEN = 4            # 验证码长度
CAPTCHA_HEIGHT = 60        # 验证码高度
CAPTCHA_WIDTH = 160        # 验证码宽度

# 10 个 Epochs 训练的模型
MODEL_FILE = './pre-trained/model/captcha_rmsprop_binary_crossentropy_bs_100_epochs_10.h5'

def vec2text(vector):
    if not isinstance(vector, np.ndarray):
        vector = np.asarray(vector)
    vector = np.reshape(vector, [CAPTCHA_LEN, -1])
    text = ''
    for item in vector:
        text += CAPTCHA_CHARSET[np.argmax(item)]
    return text

def rgb2gray(img):
    # Y' = 0.299 R + 0.587 G + 0.114 B 
    # https://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
    return np.dot(img[...,:3], [0.299, 0.587, 0.114])

app = Flask(__name__) # 创建 Flask 实例

# 测试 URL
@app.route('/ping', methods=['GET', 'POST'])
def hello_world():
    return 'pong'

# 验证码识别 URL
@app.route('/predict', methods=['POST'])
def predict():
    response = {'success': False, 'prediction': '', 'debug': 'error'}
    received_image= False
    if request.method == 'POST':
        if request.files.get('image'): # 图像文件
            image = request.files['image'].read()
            received_image = True
            response['debug'] = 'get image'
        elif request.get_json(): # base64 编码的图像文件
            encoded_image = request.get_json()['image']
            image = base64.b64decode(encoded_image)
            received_image = True
            response['debug'] = 'get json'
        if received_image:
            image = np.array(Image.open(BytesIO(image)))
            image = rgb2gray(image).reshape(1, 60, 160, 1).astype('float32') / 255
            with graph.as_default():
                pred = model.predict(image)
            response['prediction'] = response['prediction'] + vec2text(pred)
            response['success'] = True
            response['debug'] = 'predicted'
    else:
        response['debug'] = 'No Post'
    return jsonify(response)

model = load_model(MODEL_FILE) # 加载模型
graph = tf.get_default_graph() # 获取 TensorFlow 默认数据流图

if __name__ == "__main__":
    app.run()

3.使用Flask启动验证码识别服务

a.启动flask服务

实战七:手把手教你用TensorFlow进行验证码识别(下)
b.打开游览器访问测试URL(http://localhost:5000/ping)

实战七:手把手教你用TensorFlow进行验证码识别(下)
c.访问验证码识别服务

利用curl进行图像上传

curl -X POST -F [email protected] "http://localhost:5000/predict"

另开cmd终端,运行上述命令

实战七:手把手教你用TensorFlow进行验证码识别(下)

可以看出图片真实值为0044,预测结果为0244