Opensesame Freezing when Running using Pygaze
Hello hello :)
I am running into a similar issue as has been experienced before, namely that my opensesame experiment freezes once the Pygaze init is set to calibrate the tracker.
The calibration works fine but once the stimulus presentation starts, it freezes after the first trial. It gets stuck on the last image frame, and when I click on the screen it says "python has stopped responding".
I have attempted to add a short delay at the end to try to prevent freezing. Note that this stimulus type requires a loop of multiple images.
I have attached a code example of an inline script used to present stimuli:
import os
import time
# Function to present complex stimuli
def present_complex_stimulus(complex_stim_prefix):
# Construct the list of image filenames
image_filenames = [f'{complex_stim_prefix}_{i}.png' for i in range(1, 10)]
for img_filename in image_filenames:
# Construct the full image path
img_path = os.path.join(image_dir, img_filename)
# Clear the canvas before showing the new image
my_canvas.clear()
# Show the new image
my_canvas.image(img_path, center=True, x=0, y=0, scale=var.image_scale)
my_canvas.show()
time.sleep(0.4) # Display each image for 400 ms
var.complex_stim_prefix = selected_set["complex_stim_prefix"]
# Present the complex stimulus
present_complex_stimulus(var.complex_stim_prefix)
# Resetting variables
started_lookingaway = False
insp_dur = 0
disconnect_dur = 0
lookaway_dur = 0
# Initialize trial-specific variables
var.t0 = time.time()
var.insp_dur = 0
var.lookaway_dur = 0
var.disconnect_dur = 0
# Eye tracking loop
while True:
# Get the current time at the start of the iteration
current_time = time.time()
# Calculate elapsed time from the start of the loop
elapsed_time = current_time - var.t0
# Sample gaze coordinates
x, y = eyetracker.sample()
if elapsed_time >= 5:
break # Exit the loop if the maximum duration is reached
if eyetracker.connected():
if var.x_min < x < var.x_max and var.y_min < y < var.y_max:
# If on target, log inspection duration and reset looking away timer
var.insp_dur = elapsed_time
var.lookaway_dur = 0
else:
# If not on target, append new time to looking away timer
var.lookaway_dur += (current_time - var.t0)
# If looking away for 1.5 or more seconds, run grabber_loop2
if var.lookaway_dur >= 1.5:
run_grabber = True
break
else:
# Increment disconnect duration
var.disconnect_dur += (current_time - var.t0)
if var.disconnect_dur >= 1.5:
break
# Check if the trial should end based on the specified conditions
if elapsed_time >= 5 or var.lookaway_dur >= 1.5:
break
# Update var.t0 to the current time at the end of the iteration
var.t0 = current_time
# Add a small delay to prevent freezing
time.sleep(0.01)
# Clear the canvas
my_canvas.clear()
my_canvas.show()
Could you spot anything in my code that may be causing an infinite loop? Or is this problem related to hardware?
Comments
Quick update: the experiment no longer freezes when i remove my attention grabber script:
import os
import time
import pygame.mixer # Import the sound library
# Initialize the sound mixer
pygame.mixer.init()
# Define the directory containing the image files
image_dir = r"C:\Users\topuser\Desktop\HabTask-Complexity\stimuli"
# Define the path to the single image file
image_file = 'grabber1.png'
# Define the path to the sound file (MP3 format)
sound_file = r"C:\Users\topuser\Desktop\HabTask-Complexity\stimuli\calibration_target_audio.wav"
# Function to display the image
def display_image(image_file):
# Construct the full image path
img_path = os.path.join(image_dir, image_file)
# Clear the canvas before showing the new image
my_canvas.clear()
# Show the image
my_canvas.image(img_path, center=True, x=0, y=0, scale=var.image_scale)
my_canvas.show()
# Function to play the sound
def play_sound(sound_file):
# Load the sound file
sound = pygame.mixer.Sound(sound_file)
# Play the sound
sound.play()
# Add a small delay to prevent freezing
time.sleep(0.1)
# Function to monitor gaze
def monitor_gaze():
global run_grabber # Ensure run_grabber is recognized globally
# Resetting variables
started_lookingaway = False
lookaway_dur = 0
looking_at_grabber_dur = 0
disconnect_dur = 0 # Ensure this variable is defined
# Initialize t0 variable
var.t0 = time.time()
run_grabber = False
# Sample gaze coordinates
x, y = eyetracker.sample()
if eyetracker.connected():
if var.x_min < x < var.x_max and var.y_min < y < var.y_max:
# If on target, reset lookaway duration and start counting
lookaway_dur = 0
if run_grabber:
looking_at_grabber_dur += (time.time() - var.t0)
if looking_at_grabber_dur >= 0.5:
# Break the loop and continue to the next trial
return
else:
looking_at_grabber_dur = 0
else:
# If not on target, increase lookaway duration
lookaway_dur += (time.time() - var.t0)
# If looking away for more than 1.5 seconds, set run_grabber to True
if lookaway_dur >= 1.5:
run_grabber = True
return # Exit the function once the condition is met
else:
# Increment disconnect duration
disconnect_dur += (time.time() - var.t0)
if disconnect_dur >= 1.5:
return
# Display the image if run_grabber is True
if run_grabber:
# Display the image
display_image(image_file)
# Play the sound after displaying the image
play_sound(sound_file)