1 Star 0 Fork 5

simonliu009/tflite_train

forked from WJG/tflite_train 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
final.py 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
WJG 提交于 2019-12-01 15:05 . 测试版0.5
# -*- coding:utf-8 -*-
import os
import numpy as np
import time
import io
from PIL import Image
import tensorflow as tf
test_image_dir = 'data/test'
model_path = "model_quantized.tflite"
chars='23456789abcdefghjkmpqrstuvwxy'
# 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()
print(str(input_details))
output_details = interpreter.get_output_details()
print(str(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
model_interpreter_time = 0
# 计时
start_time = time.time()
# 遍历文件
file_list = os.listdir(test_image_dir)
for file in file_list:
print('-----------------------------')
full_path = os.path.join(test_image_dir, file)
captcha_image = Image.open(full_path)
image_np_expanded=img2input(captcha_image,100,50)
# 填装数据
model_interpreter_start_time = time.time()
interpreter.set_tensor(input_details[0]['index'], image_np_expanded)
# 注意注意,我要调用模型了
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
model_interpreter_time += time.time() - model_interpreter_start_time
codes=''
for i in output_data[0]:
codes=codes+chars[i]
print('codes:{}'.format(codes))
used_time = time.time() - start_time
print('used_time:{}'.format(used_time))
print('model_interpreter_time:{}'.format(model_interpreter_time))
Loading...
马建仓 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