Exemple #1
0
    class camera_tester(object):
        """docstring for merged_gm"""
        def __init__(self,
                     prior,
                     detection_model,
                     trajectory,
                     num_std=1,
                     bounds=None):
            self.fig = plt.figure(figsize=(16, 8))
            self.gm = prior
            self.detection_model = detection_model
            self.trajectory = itertools.cycle(trajectory)
            self.vb = VariationalBayes()
            self.num_std = num_std
            if bounds is None:
                self.bounds = [-5, -5, 5, 5]
            else:
                self.bounds = bounds

        def update(self, i=0):
            self.camera_pose = next(self.trajectory)
            logging.info('Moving to pose {}.'.format(self.camera_pose))
            self.detection_model.move(self.camera_pose)

            # Do a VBIS update
            mu, sigma, beta = self.vb.update(measurement='No Detection',
                                             likelihood=detection_model,
                                             prior=self.gm,
                                             use_LWIS=True,
                                             poly=detection_model.poly,
                                             num_std=self.num_std)
            self.gm = GaussianMixture(weights=beta,
                                      means=mu,
                                      covariances=sigma)
            # Log what's going on
            logging.info(self.gm)
            logging.info('Weight sum: {}'.format(beta.sum()))

            self.remove()
            self.plot()

        def plot(self):
            levels_res = 50
            self.levels = np.linspace(0, np.max(self.gm.pdf(self.pos)),
                                      levels_res)
            self.contourf = self.ax.contourf(self.xx,
                                             self.yy,
                                             self.gm.pdf(self.pos),
                                             levels=self.levels,
                                             cmap=plt.get_cmap('jet'))
            # Plot camera
            self.cam_patch = PolygonPatch(self.detection_model.poly,
                                          facecolor='none',
                                          linewidth=2,
                                          edgecolor='white')
            self.ax.add_patch(self.cam_patch)

            # Plot ellipses
            self.ellipse_patches = self.gm.plot_ellipses(
                poly=self.detection_model.poly)

        def plot_setup(self):
            # Define gridded space for graphing
            min_x, max_x = self.bounds[0], self.bounds[2]
            min_y, max_y = self.bounds[1], self.bounds[3]
            res = 30
            self.xx, self.yy = np.mgrid[min_x:max_x:1 / res,
                                        min_y:max_y:1 / res]
            pos = np.empty(self.xx.shape + (2, ))
            pos[:, :, 0] = self.xx
            pos[:, :, 1] = self.yy
            self.pos = pos

            # Plot setup
            self.ax = self.fig.add_subplot(111)

            self.ax.set_title('VBIS with camera detection test')
            plt.axis('scaled')
            self.ax.set_xlim([min_x, max_x])
            self.ax.set_ylim([min_y, max_y])

            levels_res = 50
            self.levels = np.linspace(0, np.max(self.gm.pdf(self.pos)),
                                      levels_res)
            cax = self.contourf = self.ax.contourf(self.xx,
                                                   self.yy,
                                                   self.gm.pdf(self.pos),
                                                   levels=self.levels,
                                                   cmap=plt.get_cmap('jet'))
            self.fig.colorbar(cax)

        def remove(self):
            if hasattr(self, 'cam_patch'):
                self.cam_patch.remove()
                del self.cam_patch

            if hasattr(self, 'ellipse_patches'):
                for patch in self.ellipse_patches:
                    patch.remove()
                del self.ellipse_patches

            if hasattr(self, 'contourf'):
                for collection in self.contourf.collections:
                    collection.remove()
                del self.contourf