Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python OpenCV:按键时刷新图像_Python_Python 2.7_Opencv_Numpy_Pygtk - Fatal编程技术网

Python OpenCV:按键时刷新图像

Python OpenCV:按键时刷新图像,python,python-2.7,opencv,numpy,pygtk,Python,Python 2.7,Opencv,Numpy,Pygtk,我用python opencv2模块开发了一个程序 只要按下一个键,程序就会上传一个图像 以下是伪代码: import cv2 from msvcrt import getch while True: k = getch() if k == 'w': img = cv2.imread(filedir + orange.jpg) cv2.namedWindow cv2.imshow(img)

我用python opencv2模块开发了一个程序

只要按下一个键,程序就会上传一个图像

以下是伪代码:

 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
这是不言自明的,因为当按下“w”时,我正试图上传一个“orange.jpg”文件

我真正的问题是:如何以这样的方式设计程序,用户不必按两次键,一次按键关闭图像文件,另一次按键打开文件。这使设计失败,因为我希望在一次击键中进行处理。即使用户按下“w”键,并且“orange.jpg”已上载,也应该刷新该文件,而不是关闭该文件。类似地,当用户按下“a”键,并且“orange.jpg”打开时,“orange.jpg”文件应该关闭,而banana.jpg应该自动打开,这应该是一次性操作。到目前为止,我必须按两次键才能执行此任务

我已经实现了代码,所以即使有人建议我去pygtk并使用该键上传图像,我也没有问题。我唯一的目标是在不受用户干扰的情况下销毁上传的图像,即处理过程应该是自主的

正如beark所说,在程序中使用getch意味着关注点将始终放在控制台上。我对此并不满意,只想通过按键上传图像,但控制台阻碍了这一操作


谢谢。

首先,摆脱格奇。它只在控制台窗口有焦点时才工作,因为焦点不是真正可移植的

改用waitKey:

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()

我已经解决了这个问题:

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()

我正在破坏每个图像显示的窗口,这样,每个按键对应于新图像上传的一致性得到保持

Ok,所以即使我连接了一个外部设备,并使用pyserial读取该设备的键盘,我也不能使用getch来实现这一点?什么?你的目标太高了,超出了你目前的能力。慢慢来……我连接了一个设备,它有钥匙。此键将连接到水果,因此当我触摸水果时,将显示该水果的图像。您的解决方案将起作用,因为我能够读取设备,并且使用getch,它工作正常。但是,我不满意这个设计,因为它有缺陷。还有,有没有什么方法可以用pygame来实现呢