Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Accessing 'response' variables in coroutine to record webcam feed

I am using a coroutine in order to record webcam feed while displaying some text stimulus and taking in a keyboard response. The coroutine should end when the user presses a key. This is the function i have written and supplied to the "Generator function name":

def recordVidResponse():
	
	frame_width = int(var.cap.get(3))
	frame_height = int(var.cap.get(4))
	out = cv2.VideoWriter('/home/upamanyu/Desktop/outpy.avi', cv2.VideoWriter_fourcc('M','J','P','G'), 30, (frame_width,frame_height))
	
	yield
	
	while True:
		ret, frame = var.cap.read()
		
		if ret:
			print("dv")
			out.write(frame)
		
		if var.response_video_keyboard is not None:
			break
		
		yield
	
	out.release()

Although the documentation says that "response" and "response_[item_name]" variables should always be available i keep getting this error:

The variable 'response_video_keyboard' does not exist.

I have tried the above code with var.response instead of var.response_video_keyboard and i get the same error.

I have attached a picture of my coroutine structure. Please help out if you know what's going wrong.


Comments

  • Hi,

    This is a really nice use of coroutines. The issue is simply that var.response_video_keyboard is only defined when a key is pressed. Before then, it won't be None but simply doesn't exist. One possible solution would be to define the variable yourself during the initialization of the coroutines, i.e. before the first yield, like so: var.response_video_keyboard = None

    Does that make sense?

    Cheers!

    Sebastiaan

  • edited April 3

    Hey @sebastiaan,

    Thanks for the reply. I tried your solution before posting the query here but for some odd reason it wasn't working. It kept ending without any key being pressed. Although the if statement was:

    if var.response_video_keyboard is not None:
      print(var.response_video_keyboard)
      break
    

    it still kept entering the condition and printing 'None' for the print statement. It made no sense to me because the 'if' condition should prevent this. I haven't been able to figure out the reason for this.

    SOLUTION

    However, I figured another way around it. I used the 'End after item' in coroutines and set it to the keyboard_response. I set the keyboard_response 'End Time' to the same value as as the 'Duration' of the coroutine.

    Now, in the video recording inline script, I just yielded without a condition inside the loop and set the 'End Time' to the 'Duration' value of the coroutine. The coroutine will anyway end when the key is pressed because of the settings I mentioned in the previous paragraph.

    Now it is recording synchronised video per trial without any issue.

    Inline script to record and save video frames:

    # Note: var.cap is defined in an inline script right in the starting of the experiment. 
    # 
    # import cv2
    # var.cap = cv2.VideoCapture(0)
    # this is done to prevent the latency in trying to access the video camera before every trial
    
    def recordVidResponse():
            
            frame_width = int(var.cap.get(3))
          	frame_height = int(var.cap.get(4))
          	out = cv2.VideoWriter('path_to_save_your_video/' + str(var.Loop_NUmber) + '.avi', cv2.VideoWriter_fourcc('M','J','P','G'), 30, (frame_width,frame_height))
          
          	yield
          	
          	while True:
          		ret, frame = var.cap.read()		
          		if ret:
          			out.write(frame)		
          		yield
          	out.release()
    



    Hope this helps. I think a lot of people are running into trouble while trying to record the video simultaneously while displaying stimuli. This should do it.

  • Hi @u_ghose,

    I've just tried your solution out and it's really excellent.

    Thanks for posting it!

    Boo.

  • edited November 11

    Hello @u_ghose and all,

    thanks for the script. Unfortunately it doesn't work for me. I use Opensesame with Python 2.7. The code looks different in color. At the moment I can't explain what's wrong. Does anyone have a hint for me?



  • edited November 25

    Hi Mirko,

    not sure whether this will solve it, but the second yield must be on the same level as the if (it is indented too far currently).

    Does that help?

    Eduard

  • Hi Eduard,

    thank you for answering. I deleted booth of the yields. Now something is going happen but very slow and I didn't get any videofile.

    Mirko

  • Hi @eduard

    I found my mistake. I have now inserted all the code into an inline script at the beginning of the experiment and linked it to the generator in the Coroutine. The webcam also seems to be activated, but I don't get a file. I have path on the desktop. I think I haven't quite understood what I have to write into the Inlinescript within the Coroutine yet. Just used the same code (without the first 2 lines). Could you give me a hint? What do I have to write into the inlinescript of the coroutine and what at the beginning of the experiment?

  • Hi Mirko,


    The code should be in the part of the code that plays the videos. As far as I understand from previous discussions, all the code goes into the same inline script. Maybe @booradley or @u_ghose can help out?


    Eduard

Sign In or Register to comment.