Ask Your Question

Revision history [back]

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 numpy as np
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('figureouthowtoincludevariables.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()

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 numpy as np
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('figureouthowtoincludevariables.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()

Solution: 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.

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 numpy as np
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('figureouthowtoincludevariables.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()

Solution: 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.

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 numpy as np
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('figureouthowtoincludevariables.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()

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('figureouthowtoincludevariables.avi', 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()