Beispiel #1
0
def main():
    fmf_filename = sys.argv[1]

    fmf = fmf_mod.FlyMovie(fmf_filename)
    frame,timestamp = fmf.get_next_frame()
    frame = convert(frame,fmf.format)

    w = window.Window(visible=False, resizable=True)
    aii = ArrayInterfaceImage(frame)
    img = aii.texture
    w.width = img.width
    w.height = img.height
    w.set_visible()

    prev_n_frames = None
    while not w.has_exit:
        # TODO add some throttling here...
        w.dispatch_events()

        n_frames=fmf.compute_n_frames_from_file_size(only_full_frames=True)
        if prev_n_frames != n_frames:
            prev_n_frames = n_frames
            fmf.seek(n_frames-1)
            frame,timestamp = fmf.get_next_frame()
            frame = convert(frame,fmf.format)

            aii.view_new_array(frame)

            img.blit(0, 0, 0)
            w.flip()
Beispiel #2
0
def main():
    fmf_filename = []
    directory = './'
    if len(sys.argv) > 1:
        argin = sys.argv[1]
        if argin[-3:] == 'fmf':
            fmf_filename = argin
        else:
            directory = argin
    
    if fmf_filename == []:
        filenames = os.listdir(directory)
        mtimes = np.zeros(len(filenames))
        for f,filename in enumerate(filenames):
            if filename[-3:] == 'fmf':
                mtimes[f] = os.path.getmtime(os.path.join(directory,filename))
                
        inds = np.argsort(mtimes)
        fmf_filename = os.path.join(directory,filenames[inds[-1]])
    print directory
    print fmf_filename
    
    fmf = fmf_mod.FlyMovie(fmf_filename)
    frame,timestamp = fmf.get_next_frame()
    frame = convert(frame,fmf.format)

    w = window.Window(visible=False, resizable=True)
    aii = ArrayInterfaceImage(frame)
    img = aii.texture
    w.width = img.width
    w.height = img.height
    w.set_visible()

    prev_n_frames = None
    while not w.has_exit:
        # TODO add some throttling here...
        w.dispatch_events()

        n_frames=fmf.compute_n_frames_from_file_size(only_full_frames=True)
        if prev_n_frames != n_frames:
            prev_n_frames = n_frames
            fmf.seek(n_frames-1)
            frame,timestamp = fmf.get_next_frame()
            frame = convert(frame,fmf.format)

            aii.view_new_array(frame)

            img.blit(0, 0, 0)
            w.flip()
Beispiel #3
0
    def _new_child(self,id_val,image,sort_add=False):
        self.Hide()
        try:
            if self.child_kwargs is None:
                kws = {}
            else:
                kws = self.child_kwargs
            child = PointDisplayCanvas(self,-1,**kws)
        finally:
            self.Show()
        child.set_fullcanvas(True)
        pygim = ArrayInterfaceImage( image, allow_copy=False )
        child.new_image( pygim )
        child.set_rotate_180( self.rotate_180 )
        child.set_flip_LR( self.flip_lr )

        self.children[id_val] = child
        self.lbrt[id_val] = ()

        id_vals = self.children.keys()
        id_vals.sort()

        if sort_add:
            # maintain ordering
            self.box = wx.BoxSizer(wx.HORIZONTAL)
            self.SetSizer(self.box)
            for id_val in id_vals:
                child = self.children[id_val]
                self.box.Add( child, 1, wx.EXPAND|wx.ALL, border=1)
        else:
            self.box.Add( child, 1, wx.EXPAND|wx.ALL, border=1)
        self.Layout()
Beispiel #4
0
class Viewer(object):
    def __init__(self, height=84, width=84, resize_ratio=1):
        from pyglet.gl import glEnable, glBlendFunc
        from pyglet.gl import GL_BLEND, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
        from pyglet import window, image
        from pygarrayimage.arrayimage import ArrayInterfaceImage
        self.width = width
        self.height = height
        self.resize_ratio = resize_ratio
        self.window = window.Window(width=int(width * resize_ratio),
                                    height=int(height * resize_ratio),
                                    visible=False,
                                    resizable=True)
        self.arr = np.zeros(
            (int(height * resize_ratio), int(width * resize_ratio)),
            dtype=np.uint8)
        self.aii = ArrayInterfaceImage(self.arr)
        self.img = self.aii.texture

        checks = image.create(32, 32, image.CheckerImagePattern())
        self.background = image.TileableTexture.create_for_image(checks)
        self.window.set_visible()

        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    def render(self, new_arr):
        assert new_arr.shape == (
            self.height, self.width
        ), "the array shape {0} should be consistent with stipulated shape {1}!".format(
            new_arr.shape, (self.height, self.width))
        if self.resize_ratio != 1:
            new_arr = np.repeat(np.repeat(new_arr, int(self.resize_ratio), 0),
                                int(self.resize_ratio), 1)
        self.window.dispatch_events()
        self.background.blit_tiled(0, 0, 0, self.window.width,
                                   self.window.height)
        self.img.blit(0, 0, 0)
        self.window.flip()

        np.place(self.arr, self.arr >= -1, new_arr.astype('uint8')[::-1, :])
        self.aii.dirty(
        )  # dirty the ArrayInterfaceImage because the data changed

    def close(self):
        self.window.close()
Beispiel #5
0
    def __init__(self, height=84, width=84, resize_ratio=1):
        from pyglet.gl import glEnable, glBlendFunc
        from pyglet.gl import GL_BLEND, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
        from pyglet import window, image
        from pygarrayimage.arrayimage import ArrayInterfaceImage
        self.width = width
        self.height = height
        self.resize_ratio = resize_ratio
        self.window = window.Window(width=int(width * resize_ratio),
                                    height=int(height * resize_ratio),
                                    visible=False,
                                    resizable=True)
        self.arr = np.zeros(
            (int(height * resize_ratio), int(width * resize_ratio)),
            dtype=np.uint8)
        self.aii = ArrayInterfaceImage(self.arr)
        self.img = self.aii.texture

        checks = image.create(32, 32, image.CheckerImagePattern())
        self.background = image.TileableTexture.create_for_image(checks)
        self.window.set_visible()

        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
Beispiel #6
0
    def __init__(self):
        super().__init__()
        window = system.get_window()
        gl.glEnable(gl.GL_BLEND)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
        base_path = os.path.join('images', 'space_background_pack', 'layers')

        self.background = load(os.path.join(base_path, 'parallax-space-background.png'))
        self.background_sprite = Sprite(self.background)
        big_planet = load(os.path.join(base_path, 'parallax-space-big-planet.png'))
        self.big_planet_sprite = Sprite(big_planet)
        far_planet = load(os.path.join(base_path, 'parallax-space-far-planets.png'))
        self.far_planet_sprite = Sprite(far_planet)
        ring_planet = load(os.path.join(base_path, 'parallax-space-ring-planet.png'))
        self.ring_planet_sprite = Sprite(ring_planet)
        self.ring_planet_sprite.x = 100
        self.ring_planet_sprite.y = 100
        stars = load(os.path.join(base_path, 'parallax-space-stars.png'))
        self.stars_sprite = Sprite(stars)
        self.scheduled_functions = (
            (self.randomize_projectiles, 2),
            (self.check_direction, 1 / 120),
            (self.generate_powerup, 20)
        )

        if config.get_config('control') == 1:
            self.face_cascade = cv.CascadeClassifier(
                'venv/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml')
            self.facecam = Facecam(face_cascade=self.face_cascade).start()
            self.aii = ArrayInterfaceImage(self.facecam.read(), 'BGR')
            self.image = self.aii.texture

            @self.facecam.event('on_face_move')
            def follow_face(x, y, width, height):
                # Need to flip things first to work
                x = self.image.width - x
                y = self.image.height - y
                new_x = self.border.x + x - width // 2
                new_y = self.border.y + y - height // 2
                self.player.move(new_x, new_y)

            self.scheduled_functions = (
                (self.randomize_projectiles, 2),
                (self.generate_powerup, 20),
            )

            window.push_handlers(self.facecam)

        self.player = Player(src='images/ufo.png')
        self.border = Rectangle(
            width=(500 if not self.image else self.image.width) + 10,
            height=(500 if not self.image else self.image.height) + 10,
            color=(0, 0, 0, 127)
        )
        self.player.scale = 1.2
        self.batch = Batch()
        self.projectiles: List[Projectile] = []
        window.push_handlers(self.keys)

        def pause_game(symbol, modifiers):
            if symbol == key.ESCAPE:
                self.enemy.running = not self.enemy.running
                self.enemy.attack_pattern.running = not self.enemy.attack_pattern.running
                self.player.running = not self.player.running

        window.on_key_press = pause_game

        for func, interval in self.scheduled_functions:
            clock.schedule_interval(func, interval)
        self.fps_display = FPSDisplay(window)
        self.generate_enemy(0)
        self.resize()
Beispiel #7
0
class GameInterface(Interface):
    attack: AttackPattern = None
    aii: ArrayInterfaceImage = None
    background_sprite: Sprite = None
    batch: Batch = None
    border: Rectangle = None
    enemy: Enemy = None
    image = None
    facecam: Facecam = None
    face_cascade = None
    foreground_sprite: Sprite = None
    fps_display: FPSDisplay = None
    keys = key.KeyStateHandler()
    player: Player = None
    projectiles: List[Projectile] = []
    powerup: Powerup = None
    scheduled_functions = tuple()

    def __init__(self):
        super().__init__()
        window = system.get_window()
        gl.glEnable(gl.GL_BLEND)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
        base_path = os.path.join('images', 'space_background_pack', 'layers')

        self.background = load(os.path.join(base_path, 'parallax-space-background.png'))
        self.background_sprite = Sprite(self.background)
        big_planet = load(os.path.join(base_path, 'parallax-space-big-planet.png'))
        self.big_planet_sprite = Sprite(big_planet)
        far_planet = load(os.path.join(base_path, 'parallax-space-far-planets.png'))
        self.far_planet_sprite = Sprite(far_planet)
        ring_planet = load(os.path.join(base_path, 'parallax-space-ring-planet.png'))
        self.ring_planet_sprite = Sprite(ring_planet)
        self.ring_planet_sprite.x = 100
        self.ring_planet_sprite.y = 100
        stars = load(os.path.join(base_path, 'parallax-space-stars.png'))
        self.stars_sprite = Sprite(stars)
        self.scheduled_functions = (
            (self.randomize_projectiles, 2),
            (self.check_direction, 1 / 120),
            (self.generate_powerup, 20)
        )

        if config.get_config('control') == 1:
            self.face_cascade = cv.CascadeClassifier(
                'venv/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml')
            self.facecam = Facecam(face_cascade=self.face_cascade).start()
            self.aii = ArrayInterfaceImage(self.facecam.read(), 'BGR')
            self.image = self.aii.texture

            @self.facecam.event('on_face_move')
            def follow_face(x, y, width, height):
                # Need to flip things first to work
                x = self.image.width - x
                y = self.image.height - y
                new_x = self.border.x + x - width // 2
                new_y = self.border.y + y - height // 2
                self.player.move(new_x, new_y)

            self.scheduled_functions = (
                (self.randomize_projectiles, 2),
                (self.generate_powerup, 20),
            )

            window.push_handlers(self.facecam)

        self.player = Player(src='images/ufo.png')
        self.border = Rectangle(
            width=(500 if not self.image else self.image.width) + 10,
            height=(500 if not self.image else self.image.height) + 10,
            color=(0, 0, 0, 127)
        )
        self.player.scale = 1.2
        self.batch = Batch()
        self.projectiles: List[Projectile] = []
        window.push_handlers(self.keys)

        def pause_game(symbol, modifiers):
            if symbol == key.ESCAPE:
                self.enemy.running = not self.enemy.running
                self.enemy.attack_pattern.running = not self.enemy.attack_pattern.running
                self.player.running = not self.player.running

        window.on_key_press = pause_game

        for func, interval in self.scheduled_functions:
            clock.schedule_interval(func, interval)
        self.fps_display = FPSDisplay(window)
        self.generate_enemy(0)
        self.resize()

    def generate_enemy(self, dt):
        enemy_index = config.get_config('enemy')
        if enemy_index > len(enemies) - 1:
            return system.get_window().load_interface(WinInterface())

        window = system.get_window()
        self.enemy = enemies[enemy_index](self.batch, speed=100)
        self.enemy.move(window.width * 0.8, window.height / 2)
        self.enemy.next_x, self.enemy.next_y = self.enemy.x, self.enemy.y

        def on_die():
            config.set_config('enemy', config.get_config('enemy') + 1)
            clock.schedule_once(self.generate_enemy, 2)

        self.enemy.on_die = on_die

    def resize(self):
        window = system.get_window()
        scale = window.width / self.background.width
        self.background_sprite.scale = scale
        self.big_planet_sprite.scale = scale / 2
        self.far_planet_sprite.scale = scale / 2
        self.ring_planet_sprite.scale = scale / 2
        self.stars_sprite.scale = scale

        self.big_planet_sprite.x = window.width / 2
        self.big_planet_sprite.y = window.height / 2
        self.far_planet_sprite.x = window.width - self.far_planet_sprite.width
        self.far_planet_sprite.y = window.height - self.far_planet_sprite.height

        if self.image:
            self.image.x = (window.width - self.image.width) / 2
            self.image.y = (window.height - self.image.height) / 2
        self.border.x = window.width / 2 * 0.3
        self.border.y = (window.height - self.border.height) / 2

        self.generate_powerup(0)
        self.player.move(x=window.width / 2 * 0.3, y=window.height / 2)

    def on_draw(self):
        self.animate_background()
        self.background_sprite.draw()
        self.big_planet_sprite.draw()
        self.far_planet_sprite.draw()
        self.ring_planet_sprite.draw()
        self.stars_sprite.draw()
        self.border.draw()
        if self.facecam:
            img = self.facecam.read()
            img = cv.flip(img, -1)
            self.image.blit(self.border.x, self.border.y, 0)
            self.aii.view_new_array(img)
        # self.fps_display.draw()
        self.player.draw()
        self.batch.draw()
        # self.attack.draw()
        if self.enemy:
            self.enemy.draw()
        if self.powerup:
            self.powerup.forward()
            self.powerup.draw()
            if self.player.check_for_collision(self.powerup):
                self.powerup.on_collide(self.player)
                self.powerup.delete()
                self.powerup = None

    def animate_background(self):
        big_planet_sprite_depth = 3
        far_planet_sprite_depth = 10
        ring_planet_sprite_depth = 5
        stars_sprite_depth = 8

        window = system.get_window()
        self.big_planet_sprite.x = self.big_planet_sprite.x - 1 * (1 / big_planet_sprite_depth)
        if self.big_planet_sprite.x + self.big_planet_sprite.width <= 0:
            self.big_planet_sprite.x = window.width
        self.far_planet_sprite.x = self.far_planet_sprite.x - 1 * (1 / far_planet_sprite_depth)
        if self.far_planet_sprite.x + self.far_planet_sprite.width <= 0:
            self.far_planet_sprite.x = window.width
        self.ring_planet_sprite.x = self.ring_planet_sprite.x - 1 * (1 / ring_planet_sprite_depth)
        if self.ring_planet_sprite.x + self.ring_planet_sprite.width <= 0:
            self.ring_planet_sprite.x = window.width
        self.stars_sprite.x = self.stars_sprite.x - 1 * (1 / stars_sprite_depth)
        if self.stars_sprite.x + self.stars_sprite.width <= 0:
            self.stars_sprite.x = window.width

    def generate_powerup(self, dt):
        window = system.get_window()
        # roll = random.randint(1, 10)
        roll = 1
        if roll <= 3:  # 30% chance of getting a powerup
            powerup = random.choice(powerups)(batch=self.batch)
            x = random.randint(int(self.border.x), int(self.border.x + self.border.width))
            powerup.move(x=x, y=window.height)

            @powerup.movement
            def movement():
                powerup.move(powerup.x, powerup.y - powerup.calculate_speed())

            self.powerup = powerup

    def randomize_projectiles(self, dt):
        if self.enemy:
            self.enemy.attack_pattern.generate()

    def check_direction(self, dt):
        unit = 500 * dt
        if self.keys[key.DOWN]:
            if self.player.get_bot_bound() - unit >= self.border.y:
                self.player.move(self.player.x, self.player.y - unit)
        elif self.keys[key.UP]:
            if self.player.get_bot_bound() + self.player.height + unit <= self.border.y + self.border.height:
                self.player.move(self.player.x, self.player.y + unit)
                # self.player.y += unit
        if self.keys[key.LEFT]:
            if self.player.get_left_bound() - unit >= self.border.x:
                self.player.move(self.player.x - unit, self.player.y)
        elif self.keys[key.RIGHT]:
            if self.player.get_left_bound() + self.player.width + unit <= self.border.x + self.border.width:
                self.player.move(self.player.x + unit, self.player.y)
        # if self.keys[key.ESCAPE]:
        # fg_ratio = 0.4
        # bg_ratio = 0.1
        # self.foreground_sprite.x = (window.width * (1 + fg_ratio)) / 2 - self.player.x * fg_ratio
        # self.foreground_sprite.y = (window.height * (1 + fg_ratio)) / 2 - self.player.y * fg_ratio
        # self.background_sprite.x = (window.width * (1 + bg_ratio)) / 2 - self.player.x * bg_ratio
        # self.background_sprite.y = (window.height * (1 + bg_ratio)) / 2 - self.player.y * bg_ratio

    def clean(self):
        self.enemy.clean()
        if self.facecam:
            self.facecam.clean()
        print(f'Deleting {self}')
        super().clean()
Beispiel #8
0
def get_centers(filenames,showFrames=0,ROI=None,THRESH=1.5,ringR=.25, background=None):
    """find center of fly in fmf files, returns record array with x,y and time
    
    arguments:      
    filename        list of .fmf movie files or single .fmf movie file
    showFrames      0 [default] or 1
    ROI             4-tuple of region of interest (top, bottom, left, right) or None (whole frame)
    THRESH          threshold for number of std for pixel to be counted as foreground (default 1.5)
    ringR           inner radius for mask (default .25)
    background      background image (default None)
    
    example:
    centers = get_centers('/home/cardini/data/fly07/flyN20100317_185940.fmf',0,(9,15,12,12))
    """ 

    if isinstance(filenames,str):
        filenames = [filenames]
        
    if ROI is not None:
        top, bottom, left, right = ROI
    else:
        top, bottom, left, right = (1,0,0,1)
    
    COLOR = True
    ZOOM = 2
    FRAMESTEP = 100
    
    ended_early = False
    cents = []
    lenOutStr = 0
    for f,filename in enumerate(filenames):
        if filename[-3:] == 'fmf':
        
            fmf = FMF.FlyMovie(filename)

            nFrames = fmf.get_n_frames()
            
            frame,timestamp = fmf.get_frame(0)
            ROIFrame = frame[bottom:-top,left:-right]
            
            ## mask to eliminate points in corner -> 'ring' mask
            height,width = ROIFrame.shape
            R = numpy.floor(width/2)
            x0,y0 = R,R
            if numpy.mod(x0,2)==0:
                x0,y0 = x0-.5,y0-.5
            
            x = numpy.matrix(range(width))
            y = numpy.transpose(numpy.matrix(range(height)))

            xx = numpy.array((0*y+1)*x - y0)
            yy = numpy.array(y*(0*x+1) - x0)

            radius = numpy.sqrt(xx**2 + yy**2)
            ring = (radius<R) & (radius>R*ringR)
            ## end mask stuff
            
            if showFrames:
                
                dispFrame = convert(ROIFrame,fmf.format)
                if COLOR == True:
                    dispFrame = numpy.array([dispFrame,dispFrame,dispFrame])
                    dispFrame = numpy.swapaxes(dispFrame,0,2)
                    dispFrame = numpy.swapaxes(dispFrame,0,1)
                
                dispFrame = numpy.repeat(dispFrame,ZOOM,axis=0)
                dispFrame = numpy.repeat(dispFrame,ZOOM,axis=1)
                wnd = window.Window(visible=False, resizable=True)
                aii = ArrayInterfaceImage(dispFrame)
                img = aii.texture
                wnd.width = img.width
                wnd.height = img.height
                wnd.set_caption(filename[-23:])
                wnd.set_visible()

            for frameNumber in range(nFrames):
                frame,timestamp = fmf.get_frame(frameNumber)
                
                ROIFrame = frame[bottom:-top,left:-right]
                if background is not None:
                    ROIFrame = ROIFrame - background

                threshFrame = (ROIFrame < numpy.mean(ROIFrame) - THRESH*numpy.std(ROIFrame)) & (ring) #mask stuff

                h,w = threshFrame.shape
                X = numpy.arange(w)
                Y = numpy.arange(h)
                cX = numpy.sum(numpy.sum(threshFrame,0)*X)/numpy.sum(threshFrame)
                cY = numpy.sum(numpy.sum(threshFrame,1)*Y)/numpy.sum(threshFrame)
                
                if numpy.mod(frameNumber,FRAMESTEP) == 0:
                    outStr = str(frameNumber)+' of '+ str(nFrames)
                    sys.stdout.write("%s%s\r" % (outStr, " "*lenOutStr ))
                    lenOutStr = len(outStr)
                    sys.stdout.flush()
                    if showFrames:
                        if wnd.has_exit:
                            ended_early=True
                            break

                        wnd.dispatch_events()
                        #dispFrame = convert(ROIFrame,fmf.format)
                        #dispFrame = ROIFrame.astype(numpy.uint8)
                        #dispFrame[~ring]=dispFrame[~ring]/4
                        dispFrame = threshFrame.astype(numpy.uint8)*155 +100
                        if ~numpy.isnan(cY) and ~numpy.isnan(cY):
                            dispFrame[round(cY),round(cX)] = 0
                        if COLOR == True:
                            dispFrame = numpy.array([dispFrame,dispFrame,dispFrame])
                            dispFrame = numpy.swapaxes(dispFrame,0,2)
                            dispFrame = numpy.swapaxes(dispFrame,0,1)
                            dispFrame[ring,2]=255
                            if ~numpy.isnan(cY) and ~numpy.isnan(cY):
                                dispFrame[round(cY),round(cX),0] = 255

                        dispFrame = numpy.repeat(dispFrame,ZOOM,axis=0)
                        dispFrame = numpy.repeat(dispFrame,ZOOM,axis=1)
                        aii.view_new_array(dispFrame)
                        img.blit(0, 0, 0)
                        wnd.flip()

                cents.append((cX,cY,timestamp))
        if showFrames:
            wnd.close()
            
    centers = numpy.rec.fromarrays(numpy.transpose(cents), [('x',numpy.float),('y',numpy.float),('t',numpy.float)])
        
    if showFrames:
        pylab.figure()
        #pylab.scatter(centers.x,centers.y,s=1)
        pylab.plot(centers.x,centers.y)
        pylab.draw()
        
    if ended_early:
        centers = None
        
    return centers
Beispiel #9
0
        width, height= 320,240
        depth=4 # 1, 3, or 4
        arr = numpy.arange( width*height*depth, dtype=numpy.uint8)
        arr.shape = height,width,depth
        if depth==1 and 1:
            # test 2D array
            arr.shape = height,width
    else:
        # test constructor from PIL image
        import Image
        if Image.VERSION < '1.1.6':
            # new PIL necessary for __array_interface__
            raise ValueError("Need Image (PIL) version 1.1.6 ")
        arr = Image.open(filename)
        #arr = numpy.asarray( pil_image )
    aii = ArrayInterfaceImage(arr)

    img = aii.texture

    checks = image.create(32, 32, image.CheckerImagePattern())
    background = image.TileableTexture.create_for_image(checks)

    w.width = img.width
    w.height = img.height
    w.set_visible()

    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    i=0
    while not w.has_exit:
Beispiel #10
0
def play(frames,
         delay=0,
         magnify=1,
         background=None,
         blit_position=None,
         show_frame_number=True):

    w = window.Window(visible=False, resizable=True)

    arr = numpy.zeros([100, 100], dtype=numpy.uint8)
    aii = ArrayInterfaceImage(arr)
    img = aii.texture

    if background is None:
        checks = image.create(32, 32, image.CheckerImagePattern())
        background = image.TileableTexture.create_for_image(checks)
        background_tiled = True
    else:
        if background.dtype != np.uint8:
            'converting background to uint8'
            background = np.array(background, dtype=np.uint8) * 255
        background_aii = ArrayInterfaceImage(background)
        background = background_aii.texture
        background_tiled = False

    if background_tiled:
        w.width = img.width
        w.height = img.height
        w.set_visible()
    else:
        w.width = background.width
        w.height = background.height
        w.set_visible()

    LIBGL_ALWAYS_SOFTWARE = 1
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    for i, f in enumerate(frames):
        arr = f

        if arr is not None:
            if arr.dtype != np.uint8:
                'converting'
                arr = np.array(arr, dtype=np.uint8) * 255

            if magnify != 1:
                newshape = [arr.shape[0] * magnify, arr.shape[1] * magnify]
                arr = nim.rebin(arr, newshape)
            try:
                aii.view_new_array(arr)
            except:  # size changed
                print 'size changed!'
                #w.width = arr.shape[1]
                #w.height = arr.shape[0]
                aii = ArrayInterfaceImage(arr)

            img = aii.texture
            w.dispatch_events()
            w.clear()

            if background_tiled:
                background.blit_tiled(0, 0, 0, 100, 100)  #w.width, w.height)
            else:
                background.blit(0, 0, 0)

            # add some overlays:
            if show_frame_number:
                s = 'frame: ' + str(i)
                label = pyglet.text.Label(s,
                                          font_name='courier',
                                          font_size=36,
                                          x=10,
                                          y=10)
                label.draw()

            if blit_position is None:
                img.blit(0, 0, 0)
            else:
                img.blit(blit_position[i][1], blit_position[i][0], 0)

#frame_number_text.draw()
            if 0:
                r = arr.shape[0] / 2.
                body_axis = npmovie.kalmanobj.long_axis[f]
                wing_axis = npmovie.kalmanobj.wingaxisR[f]
                wing_center = npmovie.kalmanobj.wingcenterR[f]
                #print wing_center, wing_axis
                if mode == 'wingimgR':
                    print f, uframe.wingimgR.sum()
                pyglet.graphics.draw(
                    2, pyglet.gl.GL_LINES,
                    ('v2i',
                     (int(r), int(r), int(body_axis[1] * 10 * magnify) +
                      int(r), int(body_axis[0] * 10 * magnify) + int(r))))
                try:
                    pyglet.graphics.draw(
                        2, pyglet.gl.GL_LINES,
                        ('v2i', (int(r), int(r), int(wing_center[1] * magnify),
                                 int(wing_center[0] * magnify))))
                    pyglet.graphics.draw(1, pyglet.gl.GL_POINTS,
                                         ('v2i',
                                          (int(wing_center[1] * magnify),
                                           int(wing_center[0] * magnify))))
                    pass
                except:
                    pass

            w.flip()

            time.sleep(delay)  # slow down the playback

    w.close()
Beispiel #11
0
def play(frames, delay=0, magnify=1, background=None, blit_position=None, show_frame_number=True):

    w = window.Window(visible=False, resizable=True)
    
    arr = numpy.zeros([100,100], dtype=numpy.uint8)
    aii = ArrayInterfaceImage(arr)
    img = aii.texture

    if background is None:
        checks = image.create(32, 32, image.CheckerImagePattern())
        background = image.TileableTexture.create_for_image(checks)
        background_tiled = True
    else:
        if background.dtype != np.uint8:
            'converting background to uint8'
            background = np.array(background, dtype=np.uint8)*255
        background_aii = ArrayInterfaceImage(background)
        background = background_aii.texture
        background_tiled = False
        
    if background_tiled:
        w.width = img.width
        w.height = img.height
        w.set_visible()
    else:
        w.width = background.width
        w.height = background.height
        w.set_visible()
    
    LIBGL_ALWAYS_SOFTWARE = 1
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    
    
    for i, f in enumerate(frames):
        arr = f
                        
        if arr is not None:
            if arr.dtype != np.uint8:
                'converting'
                arr = np.array(arr, dtype=np.uint8)*255
                
            if magnify != 1:
                newshape = [arr.shape[0]*magnify, arr.shape[1]*magnify]
                arr = nim.rebin(arr, newshape)
            try:
                aii.view_new_array(arr)
            except: # size changed
                print 'size changed!'
                #w.width = arr.shape[1]
                #w.height = arr.shape[0]
                aii = ArrayInterfaceImage(arr)
                
            img = aii.texture
            w.dispatch_events()
            w.clear()
            
            
            if background_tiled:
                background.blit_tiled(0, 0, 0, 100, 100) #w.width, w.height)
            else:
                background.blit(0,0,0)
                
            # add some overlays:
            if show_frame_number:
                s = 'frame: ' + str(i)
                label = pyglet.text.Label(s,
                        font_name='courier',
                        font_size=36,
                        x=10, y=10)
                label.draw()

                
            if blit_position is None:
                img.blit(0, 0, 0)
            else:
                img.blit(blit_position[i][1], blit_position[i][0], 0)
                
            
			    
			    #frame_number_text.draw()
            if 0:
                r = arr.shape[0]/2.
                body_axis = npmovie.kalmanobj.long_axis[f]
                wing_axis = npmovie.kalmanobj.wingaxisR[f]
                wing_center = npmovie.kalmanobj.wingcenterR[f]
                #print wing_center, wing_axis
                if mode == 'wingimgR':
                    print f, uframe.wingimgR.sum() 
                pyglet.graphics.draw(2, pyglet.gl.GL_LINES,('v2i', (int(r), int(r), int(body_axis[1]*10*magnify)+int(r), int(body_axis[0]*10*magnify)+int(r))))
                try:        
                    pyglet.graphics.draw(2, pyglet.gl.GL_LINES,('v2i', (int(r), int(r), int(wing_center[1]*magnify), int(wing_center[0]*magnify))))
                    pyglet.graphics.draw(1, pyglet.gl.GL_POINTS,('v2i', (int(wing_center[1]*magnify), int(wing_center[0]*magnify))))
                    pass
                except:
                    pass
            
            
            w.flip()
            
            time.sleep(delay) # slow down the playback
    
    w.close()
        depth = 4  # 1, 3, or 4
        arr = numpy.arange(width * height * depth, dtype=numpy.uint8)
        arr.shape = height, width, depth
        if depth == 1 and 1:
            # test 2D array
            arr.shape = height, width
    else:
        # test constructor from PIL image
        import Image

        if Image.VERSION < "1.1.6":
            # new PIL necessary for __array_interface__
            raise ValueError("Need Image (PIL) version 1.1.6 ")
        arr = Image.open(filename)
        # arr = numpy.asarray( pil_image )
    aii = ArrayInterfaceImage(arr)

    img = aii.texture

    checks = image.create(32, 32, image.CheckerImagePattern())
    background = image.TileableTexture.create_for_image(checks)

    w.width = img.width
    w.height = img.height
    w.set_visible()

    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    i = 0
    while not w.has_exit:
Beispiel #13
0
from pyglet import gl
from pyglet.window import FPSDisplay
from pygarrayimage.arrayimage import ArrayInterfaceImage

gl.glEnable(gl.GL_BLEND)
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)

face_cascade = cv2.CascadeClassifier(
    'venv/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml')
# facecam = Facecam(face_cascade, src='ACM App.mp4').start()
img = cv2.imread('faces/four.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
aii = ArrayInterfaceImage(img, 'BGR')
soul = pyglet.image.load('chihiro.png')
soul_sprite = pyglet.sprite.Sprite(soul, x=0, y=0)
image = aii.texture
window = pyglet.window.Window(vsync=False)
window.maximize()
# window.height = image.height
# window.width = image.width
fps_display = FPSDisplay(window)
box = Rectangle(x=0, y=0, width=100, height=100)


@window.event
def on_draw():
    window.clear()
    image.blit(0, 0, 0)
Beispiel #14
0
def play_npmovie(npmovie, delay=0, magnify=1, mode="uimg", frames=None, framerange=None, print_frames=False):

    w = window.Window(visible=False, resizable=True)

    arr = numpy.zeros([60, 60], dtype=numpy.uint8)
    aii = ArrayInterfaceImage(arr)
    img = aii.texture
    w.width = img.width
    w.height = img.height
    w.set_visible()

    checks = image.create(32, 32, image.CheckerImagePattern())
    background = image.TileableTexture.create_for_image(checks)

    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    if frames is None:
        if framerange is not None:
            start = framerange[0]
            stop = framerange[1]
            if stop == -1:
                stop = len(npmovie.uframes)
            frames = np.arange(start, stop)
        else:
            frames = np.arange(1, len(npmovie.uframes))

    for f in frames:
        uframe = npmovie.uframes[f]
        if uframe.uimg is not None:
            try:
                if mode == "uimg":
                    arr = uframe.uimg
                if mode == "legs":
                    arr = uframe.legs
                if mode == "body":
                    arr = uframe.body
                if mode == "wings":
                    arr = uframe.wings
                if mode == "absdiff":
                    arr = uframe.absdiff
                if mode == "wingimg2":
                    arr = uframe.wingimg2
                if mode == "wingimgR":
                    arr = uframe.wingimgR
                if mode == "wingimgL":
                    arr = uframe.wingimgL
                if mode == "wingimg":
                    arr = uframe.wingimg
                if mode == "flysegs":
                    arr = uframe.flysegs
                if mode == "diffthresh":
                    arr = uframe.diffthresh
                if mode == "full":
                    arr = npmovie.background
                    arr[uframe.indices[0] : uframe.indices[1], uframe.indices[2] : uframe.indices[3]] = uframe.uimg
            except:
                arr = None

            if arr is not None:
                if arr.dtype != np.uint8:
                    "converting"
                    arr = np.array(arr, dtype=np.uint8) * 255

                if magnify != 1:
                    newshape = [arr.shape[0] * magnify, arr.shape[1] * magnify]
                    arr = nim.rebin(arr, newshape)
                try:
                    aii.view_new_array(arr)
                except:  # size changed
                    w.width = arr.shape[1]
                    w.height = arr.shape[0]
                    aii = ArrayInterfaceImage(arr)

                img = aii.texture

                w.dispatch_events()

                background.blit_tiled(0, 0, 0, w.width, w.height)
                img.blit(0, 0, 0)

                # add some overlays:
                r = arr.shape[0] / 2.0
                body_axis = npmovie.kalmanobj.long_axis[f]

                if 0:
                    wing_axis = npmovie.kalmanobj.wingaxisR[f]
                    wing_center = npmovie.kalmanobj.wingcenterR[f]
                    # print wing_center, wing_axis
                    if mode == "wingimgR":
                        print f, uframe.wingimgR.sum()
                    pyglet.graphics.draw(
                        2,
                        pyglet.gl.GL_LINES,
                        (
                            "v2i",
                            (
                                int(r),
                                int(r),
                                int(body_axis[1] * 10 * magnify) + int(r),
                                int(body_axis[0] * 10 * magnify) + int(r),
                            ),
                        ),
                    )
                    try:
                        pyglet.graphics.draw(
                            2,
                            pyglet.gl.GL_LINES,
                            ("v2i", (int(r), int(r), int(wing_center[1] * magnify), int(wing_center[0] * magnify))),
                        )
                        pyglet.graphics.draw(
                            1,
                            pyglet.gl.GL_POINTS,
                            ("v2i", (int(wing_center[1] * magnify), int(wing_center[0] * magnify))),
                        )
                        pass
                    except:
                        pass
                w.flip()

                time.sleep(delay)  # slow down the playback

    w.close()
Beispiel #15
0
 def __init__(self, **win_args):
     self.array = win_args.pop('array')
     self.aii = ArrayInterfaceImage(self.array)
     super(WindowArray, self).__init__(**win_args)
Beispiel #16
0
def get_pixel_stats(filenames,showFrames=0,ROI=None):
    """get pixel stats
    
    arguments:      
    filename        list of .fmf movie files or single .fmf movie file
    showFrames      0 [default] or 1
    ROI             4-tuple of region of interest (top, bottom, left, right) or None (whole frame)
    """ 

    if isinstance(filenames,str):
        filenames = [filenames]
        
    if ROI is not None:
        top, bottom, left, right = ROI
    else:
        top, bottom, left, right = (1,0,120,600)
        #top, bottom, left, right = (1,0,0,1)
    
    FRAMESTEP = 1000
    
    pixStats = []
    lenOutStr = 0
    for f,filename in enumerate(filenames):
        if filename[-3:] == 'fmf':
        
            fmf = FMF.FlyMovie(filename)

            nFrames = fmf.get_n_frames()
            
            frame,timestamp = fmf.get_frame(0)
            ROIFrame = frame[bottom:,left:right]
            
            ## mask to eliminate points in corner -> 'ring' mask
            height,width = ROIFrame.shape
            R = numpy.floor(height/2)
            x0,y0 = R,R
            if numpy.mod(x0,2)==0:
                x0,y0 = x0-.5,y0-.5
            
            x = numpy.matrix(range(width))
            y = numpy.transpose(numpy.matrix(range(height)))

            xx = numpy.array((0*y+1)*x - y0)
            yy = numpy.array(y*(0*x+1) - x0)

            radius = numpy.sqrt(xx**2 + yy**2)
            ring = (radius<R)& (radius>R/5)
            ## end mask stuff
            
            if showFrames:
                
                dispFrame = convert(ROIFrame,fmf.format)

                wnd = window.Window(visible=False, resizable=True)
                aii = ArrayInterfaceImage(dispFrame)
                img = aii.texture
                wnd.width = img.width
                wnd.height = img.height
                wnd.set_caption(filename[-23:])
                wnd.set_visible()

            for frameNumber in range(nFrames):
                frame,timestamp = fmf.get_frame(frameNumber)
                
                ROIFrame = frame[bottom:,left:right]
                #invertedFrame = 255 - ROIFrame
                #invertedFrame = invertedFrame - 120

                pixM = numpy.mean(ROIFrame[ring])
                pixS = numpy.std(ROIFrame[ring])
                
                if numpy.mod(frameNumber,FRAMESTEP) == 0:
                    outStr = str(frameNumber)+' of '+ str(nFrames)
                    sys.stdout.write("%s%s\r" % (outStr, " "*lenOutStr ))
                    lenOutStr = len(outStr)
                    sys.stdout.flush()
                    if showFrames:
                        if wnd.has_exit:
                            break

                        wnd.dispatch_events()
                        dispFrame = convert(ROIFrame,fmf.format)
                        dispFrame[~ring]=255

                        aii.view_new_array(dispFrame)
                        img.blit(0, 0, 0)
                        wnd.flip()

                pixStats.append((pixM,pixS,timestamp))
        if showFrames:
            wnd.close()

    pixelStats = numpy.rec.fromarrays(numpy.transpose(pixStats), [('m',numpy.float),('s',numpy.float),('t',numpy.float)])
    if showFrames:
        pylab.figure()
        pylab.plot(pixelStats.t,pixelStats.m)
        pylab.hold('on')
        pylab.plot(pixelStats.t,pixelStats.s)
        pylab.draw()

    return pixelStats