Question

I have developed a program using python opencv2 module.

The program uploads an image whenever a key is pressed.

Here's the Pseudo code:

 import cv2
    from msvcrt import getch


    while True:

    k = getch()

    if k == 'w':

          img = cv2.imread(filedir + orange.jpg)
          cv2.namedWindow
          cv2.imshow(img)
          waitkey()
          destroyAllWindows

    elif k == 'a'

          img = cv2.imread(filedir + banana.jpg)
          cv2.namedWindow
          cv2.imshow(img)
          waitkey()
          destroyAllWindows

This is self explanatory, as i am trying to upload an 'orange.jpg' file, when 'w' is pressed.

My real question is: How to design the program in such a manner, that user doesn't have to press the key twice, one key press closes the image file, and other key press opens the file. This fails the design, as I want processing to happen in one single keystroke. Even if user presses 'w' and 'orange.jpg' is already uploaded, instead of closing this file, the file should get refreshed. Similarly, when user presses 'a', and the 'orange.jpg' is open, then the 'orange.jpg' file should gets closed and banana.jpg should get open automatically, and this should be one time operation. As of now, I have to press the keys two times, to perform this task.

I have the code implemented, so even if someone suggests me to go to pygtk and upload image by pressing key using that, I have no issues. My only goal is to destroy the images uploaded without much of user interference, i.e. the processing should appear autonomous.

As beark has said, that using getch() in the program means that focus will be always on the console. I was not satisfied with this, and wanted only the images to upload by pressing keys, but console was hindering this action.

Thanks.

Was it helpful?

Solution

First, get rid of the getch(). It will only work while the console window has the focus, which is not really portable.

Use waitKey() instead:

import cv2 

cv2.namedWindow("lala")
img = cv2.imread(filedir + orange.jpg) # load initial image

while True:
    cv2.imshow("lala", img)

    # The function waitKey waits for a key event infinitely (when delay<=0)
    k = chr(cv2.waitKey(100)) 
    if k == 'w':                       # toggle current image
        img = cv2.imread(filedir + orange.jpg)
    elif k == 'a':
        img = cv2.imread(filedir + banana.jpg)
    elif k == 27:  #escape key 
        break


cv2.destroyAllWindows()

OTHER TIPS

I have solved the issue:

import sys
import cv2
import os

def main():

    File_Lst =[]

    plat = sys.platform
    #print plat

    if plat == 'win32': #for windows operating system

        File_dir = "C:\\Users\\user\\Desktop\\fruit\\"


    elif plat == 'linux2': # for linux

        File_dir = "/host/Users/user/Desktop/fruit/"

    for file in os.listdir(File_dir):

        File_Lst.append(file)

    print File_Lst


    welcome_index = File_Lst.index('welcome.jpg')           
    welcome_str = File_Lst[welcome_index]

    orange_index = File_Lst.index('orange.jpg')         
    orange_str = File_Lst[orange_index]


    apple_index = File_Lst.index('apple.jpg')           
    apple_str = File_Lst[apple_index]

    banana_index = File_Lst.index('banana.jpg')         
    banana_str = File_Lst[banana_index]

    doughnuts_index = File_Lst.index('doughnuts.jpg')           
    doughnuts_str = File_Lst[doughnuts_index]

    img = cv2.imread(File_dir + welcome_str )
    cv2.destroyAllWindows()         
    cv2.imshow("Press KEYS to know which food is good or bad", img)

    while True:

        k = cv2.waitKey(0)

        if k == ord('w'): # wait for 'w' key to upload orange nutrition information

            img = cv2.imread(File_dir + orange_str) 
            newx,newy = img.shape[1]/2,img.shape[0]/2 #new size (w,h)
            img = cv2.resize(img,(newx,newy))
            cv2.destroyAllWindows()     
            cv2.imshow("Orange Nutritional Information", img)

        elif k == ord('a'): # wait for 'w' key to upload apple nutrition information

            img = cv2.imread(File_dir + apple_str)  
            newx,newy = img.shape[1]/2,img.shape[0]/2 #new size (w,h)
            img = cv2.resize(img,(newx,newy))
            cv2.destroyAllWindows()     
            cv2.imshow("Apple Nutritional Information", img)

        elif k == ord('s'): # wait for 'w' key to upload apple nutrition information

            img = cv2.imread(File_dir + banana_str) 
            newx,newy = img.shape[1]/2,img.shape[0]/2 #new size (w,h)
            img = cv2.resize(img,(newx,newy))
            cv2.destroyAllWindows()     
            cv2.imshow("Banana Nutritional Information", img)


        elif k == 32:

            break
            cv2.destroyAllWindows()

        else:

            img = cv2.imread(File_dir + doughnuts_str)
            cv2.destroyAllWindows()
            cv2.imshow("Bad, Have good eating habits CHUMP", img)
            continue    







main()

I am destroying the window for every image show, this way, the consistency that every key strokes corresponds to fresh image upload gets maintained

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top