简体   繁体   English

使用用户输入提示循环显示一个接一个的图像

[英]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.

相关问题 在python提示后如何显示用户输入? - How to display user input after prompt in python? 输入(...)提示后显示%? - Display % after input(…) prompt? 创建一个 kivy 应用程序以在一个屏幕上获取用户的输入并在另一个屏幕上显示这些图像 - Create a kivy application to take input from user in one screen and display those many images in other screen 输入范围无效后如何重新提示循环用户输入 - How to re-prompt loop user input after invalid input range 用户输入后添加到输入提示上 - Adding onto an input prompt after the user inputs 使用带有用户输入的循环通过python下载图像列表 - Downloading images list through python using a loop with user input 如何创建一个接受命令并显示“ $”提示以指示用户可以使用while循环输入命令的shell程序? - How to create a shell program that accepts commands and prints a “$” prompt to indicate that a user can input a command using a while loop? 用户输入后重复for循环? - Repeat a for loop after user input? 在用户输入后使用 while 循环到 output 列表 - Using a while loop to output a list after user input 如何在 while 循环之前一劳永逸地提示用户输入? - How to prompt the user for input once and for all before a while loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM