import tensorflow as tf
from PIL import Image
import numpy as np
new_model = tf.keras.models.load_model('saved_model/my_model')
imgs = ['9.jpg']
for path in imgs:
#读取图片
img = Image.open(path)
img = img.resize((28, 28), Image.ANTIALIAS)
img_arr = np.array(img.convert('L'))
for i in range(28):
for j in range(28):
if img_arr[i][j] < 150:
img_arr[i][j] = 255
else:
img_arr[i][j] = 0
# 归一化
img_arr = img_arr / 255.0
# 添加一个维度
x_predict = img_arr.reshape(1, 28, 28, 1)
# 识别
result = new_model.predict(x_predict)
pred = tf.argmax(result[0])
print('正在识别:{} -> {}'.format(path, pred))