HSalman
HSalman

Reputation: 13

OpenCV Video capture using keyboard to start/stop

I have a running script capable of starting/stopping a video stream using 2 different keys;

However, when a video is captured it stores all the stream from the beginning of script execution.

    import cv2
    import numpy as np

    capture = cv2.VideoCapture(1)
    codec = cv2.VideoWriter_fourcc(*'XVID')
    output = cv2.VideoWriter('CAPTURE.avi', codec, 30, (640, 480))

    while True:
        ret, frame_temp = capture.read()
        cv2.imshow('FRAME', frame_temp)
        key=cv2.waitKey(1)
        if key%256 == 27:
            break
        elif key%256 == 32:
            output.write(frame_temp)

    capture.release()
    output.release()
    cv2.destroyAllWindows()

So, instead when the program runs I would like;

Any suggestions would be much appreciated.

Upvotes: 0

Views: 14534

Answers (2)

bfris
bfris

Reputation: 5805

You need an extra variable to figure out if you are recording. I made a variable called recording_flag

import cv2
import numpy as np

capture = cv2.VideoCapture(1)
codec = cv2.VideoWriter_fourcc(*'XVID')

recording_flag = False

while True:
    ret, frame_temp = capture.read()
    cv2.imshow('FRAME', frame_temp)
    key=cv2.waitKey(1)
    if key%256 == 27:
        break
    elif key%256 == 32:
        if recording_flag == False:
            # we are transitioning from not recording to recording
            output = cv2.VideoWriter('CAPTURE.avi', codec, 30, (640, 480))
            recording_flag = True
        else:
            # transitioning from recording to not recording
            recording_flag = False

    if recording_flag:
        output.write(frame_temp)

capture.release()
output.release()
cv2.destroyAllWindows()

Upvotes: 1

Source Code
Source Code

Reputation: 221

This line while True: isn't exactly ideal for what you're trying to do. I would recommend you define a function to do the recording and have it recursively call itself if the user stops recording via the space key. The top of your script would still start the same:

import cv2
import numpy as np

capture = cv2.VideoCapture(1)
codec = cv2.VideoWriter_fourcc(*'XVID')
output = cv2.VideoWriter('CAPTURE.avi', codec, 30, (640, 480))

Next you would define your function, it starts by waiting (forever) for a key to be pressed. Once a key is pressed if it is escape (27) it exits the function and cleans up, if it is space (32) it sets a boolean flag that starts recording (while loop). If neither escape or space is pressed it calls itself and starts the process over. Inside the while loop it checks if a key is pressed if so it checks if it is escape (27) or space (32). If either it hit it either exits the function (escape) or haults recording by calling itself which starts the process all over again (space).

def wait_then_capture():
    key = cv2.waitKey(0)
    if key%256 == 27:
        return
    elif key%256 == 32:
        record = True
    else:
        wait_then_capture()
    while record == True:
        ret, frame_temp = capture.read()
        cv2.imshow('FRAME', frame_temp)
        key = cv2.waitKey(1)
        if key%256 == 27:
            return
        elif key%256 == 32:
            break
        output.write(frame)
    wait_then_capture()

We then call our new function and end the script with the same cleanup you already have

wait_then_capture()

capture.release()
output.release()
cv2.destroyAllWindows()

Your finished script would look like this

import cv2
import numpy as np

capture = cv2.VideoCapture(1)
codec = cv2.VideoWriter_fourcc(*'XVID')
output = cv2.VideoWriter('CAPTURE.avi', codec, 30, (640, 480))

def wait_then_capture():
    key = cv2.waitKey(0)
    if key%256 == 27:
        return
    elif key%256 == 32:
        record = True
    else:
        wait_then_capture()
    while record == True:
        ret, frame_temp = capture.read()
        cv2.imshow('FRAME', frame_temp)
        key = cv2.waitKey(1)
        if key%256 == 27:
            return
        elif key%256 == 32:
            break
        output.write(frame)
    wait_then_capture()

wait_then_capture()

capture.release()
output.release()
cv2.destroyAllWindows()

Upvotes: 1

Related Questions