Home > Net >  how to end a while loop by pressing a key
how to end a while loop by pressing a key

Time:12-06

I'm trying to write a code so that a webcam will take a picture every hour for an undetermined amount of time (2 weeks for now). Here's what I have so far (which works) :

t0=time.perf_counter()
time_max=3600*24*14
time_step= 3600

while(True):
    tc=time.perf_counter()
    crit = tc-t0
    if (crit>time_max) : 
        cap.release()
        break
    cap.open(num_cam,api)
    cap.set(cv2.CAP_PROP_FOCUS,70)
    ret=cap.grab() #takes a picture
    tst,frm=cap.retrieve()
        cv2.imwrite('test1h_' str(np.round(crit,2)) '_f70.png',frm)
        cap.release()
        time.sleep(time_step)
pass

I would like for this loop to stop if I press 'q' for example, and for the webcam to take a picture if I press 'p', how could I implement that ? I read you can you can use cv2.waitKey but I don't know where to put that in the code. I also read that you can use the nodule "keyboard" but that it requires a root in linux and I work on windows.

CodePudding user response:

You can just use this:

if cv.waitKey(20) % 0xFF == ord("d"):
    break

So for example if you want to display a video while not pressing the "d" key, this should work:

while True:
    isTrue, frame = capture.read()

    cv.imshow("Video", frame)

    if cv.waitKey(20) % 0xFF == ord("d"):
        break

capture.realease()
cv.destroyAllWindows()

CodePudding user response:

Edit: Had mixed up 'char' and 'key', fixed it now.

If you're okay with not using cv2, I would suggest this using pynput:

from pynput import keyboard

def on_press(key):
    print(key)

listener = keyboard.Listener(on_press=on_press)
listener.start()

In your case, you could implement it like this:

import time
from pynput import keyboard

t0=time.perf_counter()
time_max=3600*24*14
time_step= 3600

def on_press(key):
    if key.char=='p':
        #Take picture
        cap.open(num_cam,api)
        cap.set(cv2.CAP_PROP_FOCUS,70)
        ret=cap.grab()
        tst,frm=cap.retrieve()
            cv2.imwrite('test1h_' str(np.round(crit,2)) '_f70.png',frm)
            cap.release()

    elif key.char=='q':
        #Interrupt
        exit()

listener = keyboard.Listener(on_press=on_press)
listener.start()

while(True):
    tc=time.perf_counter()
    crit = tc-t0
    if (crit>time_max) :
        cap.release()
        break
    cap.open(num_cam,api)
    cap.set(cv2.CAP_PROP_FOCUS,70)
    ret=cap.grab() #takes a picture
    tst,frm=cap.retrieve()
        cv2.imwrite('test1h_' str(np.round(crit,2)) '_f70.png',frm)
        cap.release()
        time.sleep(time_step)
pass
  • Related