2

I am writing a code that would display images one by one after the user sends input signal like enter.Following is my code

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. 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?

Screen Shots:

a) With plt.close()

enter image description here

b) Without plt.close()

enter image description here

Naseer
  • 4,041
  • 9
  • 36
  • 72

2 Answers2

1

I had a very similar problem, and placing plt.ion() within the for loop worked for me. 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()
Chinntimes
  • 185
  • 1
  • 1
  • 8
-1

I was trying to implement something similar for an image labeling program (i.e. 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. 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()