[open] Help recording webcam video to file via OpenCV
Hello,
I am currently trying to implement a background video recording process that would run while a movie was played onscreen. Conceptually, everything is in place and ready, the only problem is when I attempt to save the video being recorded to a file, the file is saved without content. Some investigating shows that the main issue behind this error is codecs, or lack there of.
I was able to record the video to file successfully in PsychoPy, and upon investigation saw that there is a DLL folder holding all the binaries necessary for ffmpeg, is there an equivalent location for the compiled version of OpenSesame?
If that is the case with ffmpeg, what I would like to do is utilize ffmpeg binaries to save the video file to mpeg, but I can't determine how to do this without running OpenSesame from source, which at this point in development is not feasible.
Here is my (simplified) code for testing purposes:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC(*'MJPG')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
i=0
while(cap.isOpened()):
ret, frame = cap.read()
i += 1
if ret==True:
# write the frame
out.write(frame)
# show frame
cv2.imshow('frame',frame)
# escape after a few seconds of recording
if i > 200:
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
As a side note, when I run this script, it doesn't show the image being stored to the frame
variable. I assume this is related to the codec issue, but is to some degree irrelevant as we don't want to show the frame during the experiment.
Comments
Not really, but I imagine that just copying the .dll files from PsychoPy to the main OpenSesame folder (i.e. not a subfolder) should do the trick. Does this work for you?
The
cv2.imshow()
function shows the frame on the cv2 window, which you have to initialize separately. I guess it fails silently if you haven't done this. In any case, it will not show the image on the OpenSesame window, because cv2 knows nothing about that.Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi,
Maybe the solution here https://forum.cogsci.nl/index.php?p=/discussion/comment/16103 might come in handy. I could use it to successfully record webcam feed while displaying stimuli.