Hi everyone,
I have the following recognition of emotion experiment design: I will show a sequence of faces, and the participant will click on one of the images he thinks is correct. When he selects on of the faces, the sequence will be interrupted and will be displayed a question (What is the emotion of the face? Select the right label) with five options for him to choose from. I need to register which is the face that he selected. How can I do that? Does someone have some ideas? Thanks a lot.
If you are using ImageStim to post the pictures, you can run a while loop to cycle through the images and see if there is a mouseclick in any. In the following code, outline[i] is a list of rectangles and you can put your image objects for a given trial in the list.
which=-2
sw1=timer1.getTime()
while which <0 :
select=[]
for i in range(len(outline)):
select.append( mouser.isPressedIn(outline[i] ))
#print("waiting for mouse click", str(select) )
core.wait(0.0001)
if True in select:
which=select.index(True)
RT= timer1.getTime()-sw1
The variable ‘which’ returns the index of the clicked item in the list called outline. One nice tweak on this is to use the name= parameter in each image object to provide a unique name or code for each image. These names/codes can refer back to the original filename or can code properties (e.g.,
outline[0]=visual.ImageStim(win, image="foo000.png", ... name="MaleHappyBrownhairRoundface" )
when loading your faces files.
Now you can add a line below the which=select.index(True)
in my code above
facechosen=outline[which].name
The documentation (psychopy.event.Mouse) is a bit dubious on this with images:
isPressedIn(shape, buttons=(0, 1, 2))[source]
Returns True if the mouse is currently inside the shape and one of the mouse buttons is pressed. The default is that any of the 3 buttons can indicate a click; for only a left-click, specify buttons=[0]:
if mouse.isPressedIn(shape):
if mouse.isPressedIn(shape, buttons=[0]): # left-clicks only
Ideally, shape can be anything that has a .contains() method, like ShapeStim or Polygon. **Not tested with ImageStim.**
I hope this gets you started.
… just tested with png images (125x83 )and it seems to work ok
import sys
import numpy
import psychopy
from psychopy import visual, event, core
black= ( -1.0, -1.0, -1.0 )
gray= ( 0.1, 0.1, 0.1 )
white= ( +1.0, +1.0, +1.0 )
XSIZE=1200
YSIZE=1200
the_screen = visual.Window([XSIZE,YSIZE], monitor='ovo', color=gray, winType='pyglet', units='pix',
allowGUI=False, waitBlanking=True ,fullscr=False, colorSpace='rgb', checkTiming=False)
mouser=psychopy.event.Mouse(win=the_screen, visible=True )
mouser.setPos ( newPos= (0,0) )
flist=[ ["face_F1.png","MaleHappyBrownhair" ] ,
["face_F2.png","MaleGrumpyBrownhair" ] ,
["face_F3.png","MaleAngryBrownhair" ] ,
["face_F4.png","FemaleHappyBrownhair" ] ,
["face_F5.png","FemaleGrumpyBrownhair" ] ,
["face_F6.png","FemaleAngryBrownhair" ]
]
allfaces=[]
for i in range(len(flist)):
allfaces.append( visual.ImageStim(the_screen, image=flist[i][0], name=flist[i][1] ))
print("loading image: %s type: %s\n"%( flist[i][0], flist[i][1] ) )
fix=visual.TextStim(the_screen, text='+', color=white, units='pix', height=96,pos=(0,0),colorSpace='rgb' )
radius=256
ticks=numpy.linspace(0,2*numpy.pi, 6, endpoint=False)
imaginarycircle=[]
for theta in range(len(ticks)):
x=int((radius)*numpy.cos(ticks[theta]) )
y=int((radius)*numpy.sin(ticks[theta]) )
imaginarycircle.append( (x,y) )
core.wait(1.01)
for trials in range(6):
fix.draw()
the_screen.flip(clearBuffer=True)
core.wait(.2)
the_screen.flip(clearBuffer=True)
numpy.random.shuffle(allfaces)
for f in range(len(allfaces)):
allfaces[f].pos = imaginarycircle[f]
allfaces[f].draw()
#print("drawing %s at %s\n"%(allfaces[f].name,str(imaginarycircle[f] ) ) )
the_screen.flip(clearBuffer=True) # w posted
which=-2
while which <0 :
select=[]
for i in range(len(allfaces)):
select.append( mouser.isPressedIn(allfaces[i] ))
core.wait(0.0001)
if True in select:
which=select.index(True)
core.wait(0.200)
the_screen.flip(clearBuffer=True) # w posted
print( "You pressed location %02d face= (%s)"%(which, allfaces[which].name) )
# ccw from -
# 2 1
# 3 0
# 4 5
sys.exit()