2014-04-04 3 views
0

나는 python opencv2 모듈을 사용하여 프로그램을 개발했다.파이썬 OpenCV : 키를 눌렀을 때 이미지 새로 고침

키를 누를 때마다 프로그램에서 이미지를 업로드합니다.

다음은 의사 코드입니다 :

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()를 사용하면 콘솔에 항상 포커스가 있음을 알 수 있습니다. 나는 이것에 만족하지 않았고, 키를 누름으로써 이미지 만 업로드하기를 원했지만, 콘솔은이 동작을 방해하고있었습니다.

감사합니다.

+0

또한 파이 게임 – vamosrafa

답변

3

먼저 getch()를 제거하십시오. 그것은 콘솔 창이 (또한 실제로 휴대용 ..) 초점을 가지고있는 동안에만 작동합니다. 대신

사용 waitKey() : 나는이 문제를 해결 한

import cv2 

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

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

    k = cv2.waitKey() 
    if k == 'w':      # toggle current image 
     img = cv2.imread(filedir + orange.jpg) 
    elif k == 'a': 
     img = cv2.imread(filedir + banana.jpg) 
    elif k == 27: 
     break 


cv2.destroyAllWindows() 
+0

을 사용하여 수행 할 수있는 방법이 있습니까? 그렇기 때문에 외부 장치를 연결하고 그 키보드를 읽는 경우에도 pyserial을 사용하여, 이것을 달성하기 위해 getch를 사용하지 않아야합니까? – vamosrafa

+0

무엇? 이모, 너는 너의 현재의 기술 이상으로 너무 멀리 목표로하고있어. 그것을 천천히 가져 가라. – berak

+0

내가 연결하는 장치가있다. 이 키는 과일에 연결되므로 과일을 만지면 그 과일의 이미지가 표시됩니다. 솔루션을 사용할 수 있습니다. 왜냐하면 장치를 읽을 수 있기 때문입니다. getch를 사용하면 정상적으로 작동합니다. 그러나 결함이있어 디자인에 만족하지 못했습니다. – vamosrafa

0

:

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

나는 모든 이미지 쇼,이 방법의 일관성을 위해 창을 파괴하고 그 모든 키 스트로크에 대응 신선한 이미지 업로드 유지 관리