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