本人为博客小白,这是我第一篇博客~
最近在用pytorch做mnist库的手写识别,由于平时编程有点“强迫症”,想按一下Enter就显示对应的手写图片和识别出来的数字,而网上找到的matplotlib教程都最多只能暂停指定的秒数,不能等待键盘输入,从而做不到想要的效果
尝试了半个晚上,终于成功了,参考了https://blog.csdn.net/xiaodongxiexie/article/details/78195860
废话不多说,先上代码
#Python 3.6.5
#Skimage Library
from skimage import io
import matplotlib.pyplot as plt
num = 0
def show():
global num
while(num<60000):
img=io.imread('data/test/'+str(num)+'.png')
io.imshow(img)
plt.ion()
plt.pause(0.01)
input("Press Enter to Continue")#之后改成识别输出
num+=1
if __name__ == '__main__':
show()
经过尝试,假如用plt.show的话会卡住这时一定要开另一个线程用plt.close关掉…
假如用plt.ion不会卡住,但一定要用plt.pause让窗口保留,然后用input等待键盘输入后才开始下一个循环
运行结果