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 2019

    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 2019

    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 2019

    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

    Buy Me A Coffee

  • 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

    Buy Me A Coffee

  • Hi,

    I have found a solution that works very well. You need two inline scripts. One at the beginning of the experiment, before the loop to be used. Here is the code:

    Then the code for recording the individual rows in the loop

    Important, the folder for the video files must be created in advance. In this example on the desktop.

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games