from __future__ import print_function
from keras.models import load_model
import cv2
import numpy as np
def reserveImg(img):
for i in range(img.shape[0]):
for j in range(img.shape[1]):
img[i, j] = 255 - img[i, j]
return img
# 图片可以是白底黑字的 白底红字的
img_rows, img_cols = 28, 28
img = cv2.imread('d:/test/test5.jpg',0)
retval, im_at_fixed = cv2.threshold(img, 130, 255, cv2.THRESH_TOZERO)
cv2.imshow('1',im_at_fixed)
img = reserveImg(im_at_fixed)
cv2.imshow('2',img)
cv2.waitKey()
img = cv2.resize(img, (img_rows, img_cols), interpolation=cv2.INTER_CUBIC)
img = np.array([img])
img = img.astype('float32')
img = img.reshape(img.shape[0], img_rows, img_cols, 1)
img = img/255
model = load_model('my_model.h5')
res = model.predict_classes(img)
print (res)
网友评论