代码拉取完成,页面将自动刷新
# -*- 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))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。