Image chooser from a grid of images

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): 3.0.5
Standard Standalone? (y/n) y

I’m trying to display a couple of images(based on a stimulus sequence) in a grid and I’d like the participant to “select” a bunch(constant) of images and record the selections and compare against the valid answers.

similar to I’m not a robot check.

Would that be possible to build it with builder view? I’m ok with coding too but would be great if it can be executed to the builder application.

1 Like

@Chintham_Tharun, yes it is possible. There are several things to consider when making your image identification Captcha-style trials. Below is a guide for how to do this.

How to position images

In Builder, you can define your stimuli in an Excel conditions file. Each column represents a variable, and each row represents a trial. If you want eight images, then you need eight columns for each image (e.g., image1, image2, etc). For now, the order does not matter. In your routine, you need eight image components, each with their position set in the grid. These are placeholders for your images, and get updated on every trial. They want names like loc1, loc2, loc3 etc - (loc for location). You can call them anything you want, but I call them locations as they set a location for the images on screen.

Are image orders randomised?

For image order randomisation, you need a code component to randomise which image goes to which image component placeholder. Something like:

# Start Routine tab
images = [image1, image2, image3, ...]  # etc. Add all your image variables to a list
placeHolders = [loc1, loc2, loc3, ...]  # etc
shuffle(images)  # randomize the order of the images

# Now you have to loop through the list and set the images to their place holder
for index, image in enumerate(images):
    placeHolders[index].setImage(image)  # For each placeholder, set the image

How are correct and incorrect responses recorded?

Assuming you have 3 correct answers on every trial, you need to define your correct answers in the conditions file. Add three columns, called corrAns1, corrAns2, and corrAns3. In each, put the correct answer - the filename of the correct image. We refer to these at the end of each trial when we decide whether or not a response was correct.

Add a mouse component to your trial. Set the mouse to never end routine on press, save mouse state on click, and add your placeholder names as clickable stimuli e.g., loc1, loc2 etc. Now, only placeholders (images) can be clicked.

In the every frame tab of the code component, you want to check whether an image has been clicked:

# Every Frame
for loc in placeHolders:
    if mouse.isPressedIn(loc):
        loc.setOpacity(.5)  # fade image to show it was clicked

if len(mouse.leftButton) == 3:
    continueRoutine = False   # End trial after 3 responses

How to check whether the responses were correct

We can use the code component at the end of a routine to check whether all responses stored in the mouse are correct:

# End Routine
for clicked in mouse.clicked_name:  # Check the list of clicked images - will be stored as loc1, loc2 etc
    if clicked.image in [corrAns1, corrAns2, corrAns3]:  # is the clicked image name in the list of correct answers
        response = 1  # 1 for correct
    else:
        response = 0  # for incorrect

thisExp.addData("responseCorr", response)  # store the correct answer in your datafile

Thank you for your response.

I need few clarifications to proceed through.

Is there a function which would place the image components in the grid? or should I just initialise them with [x,y] in the position tab of the builder view.

What is the data type of the placeholders? Would loc1 mean the [x,y] positions of the images in the builder view?

Thanks a lot though, It gave me a way to proceeed :slight_smile:

In the first bit of code under the position header, the code placeHolders[index].setImage(image) sets the image to be displayed. When you first add the image components to the routine, you can set your image size and positions in the image component dialog box (also remember to set your image to update “on every repeat” - which is every trial or presentation of that routine). You may need to keep trying different combinations or size and position, but once they are set they will not need to change.

The place holders are image components, or objects - see the docs https://www.psychopy.org/builder/components/image.html. Loc1 would be the name of the image component, that holds your image. Loc1 is an object with lots of attributes, including size, position, onset time, duration etc.

To get familiar with Builder, I recommend you do Jon Peirce’s Stroop task. It will teach you important PsychoPy concepts, such as components, using loops, how to use conditions files, updating stimuli every trial, etc. Have a go with that, and if you have anymore questions, just ask :slight_smile:

I was looking at the loc1 as position attributes until now. But now I totally get what you said. Thanks a ton :slight_smile: