[英]Display Images one after other in a loop using a user input prompt
I am writing a code that would display images one by one after the user sends input signal like enter.Following is my code 我正在编写一个代码,在用户发送输入信号(例如enter)后将一个接一个地显示图像。
import numpy as np
import matplotlib.pyplot as plt
from logistic_regression_class.utils_naseer import getData
import time
X,Y=getData(balances=False) // after getting X data and Y labels
# X and Y are retrieved using famous kaggle facial expression dataset
label_map=['Anger','Disgust','Fear','Happy','Sad','Surprise','Neutral']
plt.ion()
for i in range(len(Y)):
x=X[i]
plt.figure()
plt.imshow(x.reshape(48,48),cmap='gray')
plt.title(label_map[Y[i]])
plt.show()
_ = input("Press [enter] to continue.")
plt.close()
Output: 输出:
I am only getting blank images with no data and each time I presses enter I get a new blank image.But When I removed plt.close() then all the plots showed up in separate window but that will be too many windows popping up. 我只得到没有数据的空白图像,每次按Enter都会得到一个新的空白图像。但是当我删除plt.close()时,所有图都显示在单独的窗口中,但是会弹出太多窗口。 I also used suggestion from stackoverflow this link.
我也从计算器使用建议此链接。
What is the correct way to show images in a loop one after another using a use command input? 使用use命令输入一个接一个循环显示图像的正确方法是什么?
Screen Shots: 屏幕截图:
a) With plt.close() a)使用plt.close()
b) Without plt.close() b)没有plt.close()
I had a very similar problem, and placing plt.ion() within the for loop worked for me. 我有一个非常类似的问题,将plt.ion()放在for循环中对我来说很有效。 Like so:
像这样:
for i in range(len(Y)):
x=X[i]
plt.ion()
plt.figure()
plt.imshow(x.reshape(48,48),cmap='gray')
plt.title(label_map[Y[i]])
plt.show()
_ = input("Press [enter] to continue.")
plt.close()
I was trying to implement something similar for an image labeling program (ie present an image, take some user input and put the image in the correct folder). 我正在尝试为图像标签程序实现类似的功能(即显示图像,接受一些用户输入,然后将图像放入正确的文件夹中)。
I had some issues and the only way I could get it to work was by creating a thread (I know it's messy but it works!) that allows user input while the image is open and then closes it once there has been input. 我遇到了一些问题,使它正常工作的唯一方法是创建一个线程(我知道它很杂乱,但可以正常工作!),该线程允许用户在图像打开时进行输入,然后在输入后将其关闭。 I passed the
plt
object into the thread so I could close it from there. 我将
plt
对象传递给线程,以便可以从那里关闭它。 My solution looked something like this ... maybe you could use a similar approach! 我的解决方案看起来像这样...也许您可以使用类似的方法!
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import threading
from shutil import copyfile
import msvcrt
#define folders
CAR_DATA = 'car_data'
PASSENGER = 'passenger'
NO_PASSENGER = 'no_passenger'
UNSURE = 'unsure'
extensionsToCheck = ['jpg', '.png']
listing = os.listdir(CAR_DATA)
def userInput(plt, file_name):
print('1:No Passenger - 2:Passenger - Any Other:Unsure - e:exit')
user_input = msvcrt.getch()
user_input = bytes.decode(user_input)
if(user_input=='1'):
print("No Passenger")
copyfile(CAR_DATA+'/'+file_name, NO_PASSENGER+'/'+file_name)
elif(user_input=='2'):
print("Passenger")
copyfile(CAR_DATA+'/'+file_name, PASSENGER+'/'+file_name)
elif(user_input=="e"):
print("exit")
os._exit(0)
else:
print("unsure")
copyfile(CAR_DATA+'/'+file_name, UNSURE+'/'+file_name)
plt.close()
def main():
for file in listing:
if any(ext in file for ext in extensionsToCheck):
plt.figure(file)
img=mpimg.imread(CAR_DATA + '/' + file)
imgplot = plt.imshow(img)
plt.title(file)
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()
threading.Thread(target=userInput, args=(plt,file)).start()
plt.show()
if __name__ == '__main__':main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.