Search code examples
psychopy

Unexpected token error for cv2 library Pavlovia


I use psychopy to design a picture description experiment where I record the participants’ videos and voices and save them as my output. To record and save videos, I used a code that imports the cv2 library. I used code because the video component in the builder was crashing my experiment, and I read that code would run the video component faster. My experiment works locally but when I try to run it online, my experiment crashes while initializing the experiment. Is there a way to translate my python script to java? Or do you have any alternative suggestions? Here is my code: Begin Experiment:

import cv2  # this is the OpenCV library for computer vision
fourcc = cv2.VideoWriter_fourcc(*'XVID')
#out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,  480))
print(expInfo['participant'])
out = cv2.VideoWriter(expInfo['participant'] + '_output.avi', fourcc, 20.0, (640,  480))
webCam = cv2.VideoCapture(0)  # connect to the zeroth video camera

Each Frame:

returnVal, frame = webCam.read()
thisMovieFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)/255.0
out.write(frame)

I tried importing a set of routines maintained by Thomas Pronk, but it did not work.


Solution

  • cv2 is a Python library. There are other options for recording videos in JS

    BUT the latest versions of PsychoPy/JS support video recording and saving in their own classes, with a Builder Component included, so just use those instead! 💕 That also solves for you the issues around saving the data to Pavlovia (and in the local Python version it saves the data in a separate process which would be more efficient than your option of saving the frame within the render loop alongside stimulus rendering)