示例#1
0
#box = respbox.RespBox()

###########
# Stimuli #
###########
std_params = {'anchor': 'center', # for text display objects 
              'position': (xlim/2,ylim/2), 
              'on': False} 

rest = Text(text="Press space to begin.", **std_params)
fixation = Text(text="+", font_size=55, **std_params)
blank = Text(text="", **std_params)


trial_stims = [FaceStim(f) for f in (':-)', ':-(', ':-o')]
trials = [ExpTrial(s) for s in trial_stims]
random.shuffle(trials)
# A list of trials, using a super-pythonic list comprehension
# Trials are initialized with an 'events' arg which is a list of Event instances

vision_egg.set_stimuli([rest,fixation,blank]+trial_stims, trigger=K_SPACE)
# Give set_stimuli a list of stimulus objects as well as
# the pygame keypress which you want to begin the trials
stim_control = StimController(trials, vision_egg, pause_event=Event(rest,0,0))
# pause_event begins each block after a SPACE press.
# 'rest' is the name of my text stim

stim_control.run_trials(len(trials))
stim_control.writelog(stim_control.getOutputFilename(subject,'EXPERIMENT_TEMPLATE'))
# Change the experiment name and give it the subject at the beginning
class CameraWindow:
    '''A simple class to set up a pygame window
    
    A general design principle is that ALL openCV code should be encapsulated in
    classes such as the above.`'''

    writer = None

    def __init__(self, fname=None):
        '''Straightforward init

        fname :
            I think this needs to be *.avi
        '''
        # Set camera up first, as this is more likley to fail (I guess)
        self.cv_cam = CVCam()
        im = self.cv_cam.get_image()
        arr = self.cv_cam.conv2array(im)
        self.size = self.cv_cam.width, self.cv_cam.height
        if fname:
            self.writer = CVWriter(fname, self.size)

        # Then, we set up our graphical environment

        # SimpleVisionEgg is a custom set-up class I wrote to avoid boilerplate
        # code duplication. It's a little clunky, sadly.
        self.vision_egg = SimpleVisionEgg()
        screen_center = [x/2.0 for x in self.vision_egg.screen.size]
        tex_stim = TextureStimulus(mipmaps_enabled=False,
                                   texture=Texture(arr),
                                   size=self.size,
                                   # We shouldn't invoke OpenGL texture
                                   # filtering, as we take our texture size
                                   # directly from the camera.
                                   # the other option would be
                                   # GL_NEAREST
                                   texture_min_filter=GL.GL_LINEAR,
                                   position=screen_center,
                                   anchor='center')
        # This gives us programmatic access to the actual video memory (if
        # possible), so it should make things nice and fast. We still seem to be
        # down around 14 fps, though, on the Mac. We're not going to get much
        # faster on this front.
        self.tex_object = tex_stim.parameters.texture.get_texture_object()
        self.vision_egg.set_stimuli([tex_stim])


    def update_image(self, t):
        '''Grab an image from the camera and convert to a pygame.image
        
        I'm using my SimpleVisionEgg system, so I need to discard the time
        parameter `t` that gets passed in'''
        im = self.cv_cam.get_image()
        if self.writer:
            self.writer.write_im(im)
        arr = self.cv_cam.conv2array(im)
        # Note - we use .put_sub_image because we aren't using dimensions that
        # are a power of 2. There's a little more OpenGL magic going on somwhere
        # that I don't know about...
        self.tex_object.put_sub_image(arr)

        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                # For some reason, using VisionEgg we don't exit cleanly and
                # finalize our writer (thus, the movie is corrupted)
                del self.writer
                del self.cv_cam
                self.vision_egg.quit()

    def run(self):
        '''Run forever (until we get a QUIT event)'''
        self.vision_egg.set_functions(update=self.update_image)
        self.vision_egg.go()
              'on': False}

instruction = Text(text='ESTIMATE', position=(xlim/2, 7 * ylim/8),
                            **std_params)
description = []
for i in range(4):
    description.append(Text(text='Total U.S. population', 
                            position=(xlim/2, (11-i) * ylim/16), **std_params))

value = Text(text='300,000', position=(xlim/2, 3 * ylim/8), 
                      **std_params)

answer = Text(text='<>', position=(xlim/2, ylim/8),
                       **std_params)

vision_egg.set_stimuli([instruction, value, answer] + description)

recording = Recording()

class ReadResponse(Response):
    limit = ('return', 'enter', 'space')

class SurpriseResponse(Response):
    limit = ('1', '2', '3', '[1]', '[2]', '[3]')
    target = answer
    label = 'surprise'

    def __init__(self):
        '''Simply here to avoid calling the superclass __init__'''
        pass