1 Star 0 Fork 5

simonliu009/tflite_train

forked from WJG/tflite_train 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
scf.py 1.97 KB
一键复制 编辑 原始数据 按行查看 历史
WJG 提交于 2020-04-22 13:28 . 添加Serverless云函数部署支持
# -*- coding:utf-8 -*-
import io
import json
import os
import time
import numpy as np
import tensorflow as tf
from PIL import Image
import base64
model_path = "model_quantized.tflite" #模型文件地址
chars = '23456789abcdefghjkmpqrstuvwxy' #验证码字符,顺序要与config.json里的一致
# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path=model_path)
interpreter.allocate_tensors()
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
#将验证码数据转换成模型输入格式
def img2input(img, width, height):
tmpe_array = []
for i in range(height):
for j in range(width):
pixel = img.getpixel((j, i))
tmpe_array.append((0.3*pixel[0]+0.6*pixel[1]+0.1*pixel[2])/255)
tmpe_array = np.array(tmpe_array).astype('float32')
input_array = np.expand_dims(tmpe_array, axis=0)
return input_array
#识别验证码
def predict(base64Image):
captcha_image = Image.open(io.BytesIO(base64.b64decode(base64Image)))
image_np_expanded = img2input(captcha_image, 100, 50)
interpreter.set_tensor(input_details[0]['index'], image_np_expanded)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
codes = ''
for i in output_data[0]:
codes += chars[i]
return codes
# api网关响应集成
def apiReply(reply, txt=False, content_type='application/json', code=200):
return {
"isBase64Encoded": False,
"statusCode": code,
"headers": {'Content-Type': content_type},
"body": json.dumps(reply, ensure_ascii=False) if not txt else str(reply)
}
#云函数入口
def main_handler(event, context):
return apiReply(
{
"ok": False if not 'base64Image' in event.keys() else True,
"message": "请求参数无效" if not 'base64Image' in event.keys() else predict(event['queryString']['base64Image'])
}
)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/simonliu009/tflite_train.git
git@gitee.com:simonliu009/tflite_train.git
simonliu009
tflite_train
tflite_train
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385