Continue video capture in background until new routine
Hi, I'm trying to capture video from an external camera (a webcam for now) during an experiment. This means that within my experiment, I need to start recording without the video feed showing, continue the experiment, and then end the recording at a given point in the experiment.
You can see what I'm doing at the moment below. Now, due to the way the experimental software (opensesame, inline scripts) works, I need to split this routine into one which starts the video recording (but doesn't show the recording) and a separate one, which is called later, which stops and safes the recording.
So, basically, I have two questions: 1) How can I record from the webcam without showing the frame to my particpants? and 2) How can I split this routine into two separate ones: one start routine and one stop and safe routine?
import cv2 as cv
cap = cv.VideoCapture(0)
if not cap.isOpened():
print("Cannot open camera")
exit()
# Define the codec and create VideoWriter object
fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('video.avi', fourcc, 20.0, (640, 480))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
frame = cv.flip(frame, 1)
# write the flipped frame
out.write(frame)
cv.imshow('frame', frame)
if cv.waitKey(1) == ord('q'):
break
# Release everything if job is finished
cap.release()
out.release()
cv.destroyAllWindows()
maybe you can make the imshow() / waitKey() part depending on a flag, and toggle it when 'r' is pressed ? (you don't need 2 routines for this)
(seriously, this is about your lack of general programming skills, not really about opencv)