Ask Your Question
-1

Continue video capture in background until new routine

asked 2020-09-04 09:58:10 -0600

updated 2020-09-15 15:45:48 -0600

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()
edit retag flag offensive close merge delete

Comments

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)

berak gravatar imageberak ( 2020-09-05 10:29:14 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
-1

answered 2020-09-15 15:43:57 -0600

If anyone has the same problem. It can be solved by using opencv inside psychopy rather than opensesame.

Psychopy allows for inline_scripts to be split into different phases of a routine, thereby making the while-loop avoidable and solving both problems in one go. The code can then be separated into code that needs to be run at the start of each trial, the code inside the loop, which is run once per frame, and the code that needs to be run at the end of each trial.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-09-04 09:58:10 -0600

Seen: 1,754 times

Last updated: Sep 15 '20