Ejemplo n.º 1
0
def main():
    # define a controller, which moves the robot and provides the control commands to the localizer
    landmarks_x = [0.0, 100.0, 100.0, 0.0, 75.0]
    landmarks_y = [0.0, 0.0, 100.0, 100.0, 50.0]

    motion_cmd = [
        MotionCommand(20, 0.25 * pi),
        MotionCommand(20, 0.25 * pi),
        MotionCommand(20, 0.25 * pi),
        MotionCommand(20, 0.25 * pi),
        MotionCommand(20, 0.25 * pi),
        MotionCommand(20, 0.25 * pi),
        MotionCommand(20, 0.25 * pi)
    ]

    lm_map = LandmarkMap(landmarks_x, landmarks_y)
    simulator = Simulator(landmark_map=lm_map)

    localizer = Localizer()

    for cmd in motion_cmd:
        simulator.move(cmd.v, cmd.omega)
        localizer.odometry_callback(cmd.v, cmd.omega)

        r, phi = simulator.get_measurements()
        localizer.measurement_callback(r, phi)

    simulator.plot()
    localizer.plot()

    plt.show(block=True)
Ejemplo n.º 2
0
def random_maze(allow_loops: float, local: Localizer) -> List[List[int]]:
    """
    Creates a random maze with the specified number of nodes.
    """
    # Do NOT write maze = [[]] * node_count as this makes all list elements the same memory!
    node_count = local.node_count()
    maze = [[] for y in range(node_count)]

    # Remember all the nodes that connect to the origin. So far, just
    # contains the origin, which is zero by definition.
    accessible = {0}

    # First do a random walk until we hit the end. There may be loops,
    # but we don't worry about that. Just make sure there are no duplicate
    # edges. Also, create bidirectional edges as we go.
    edge_from = 0
    while edge_from != node_count:
        edge_to = local.random_step(edge_from, True)
        add_bidirectional_edges(maze, accessible, edge_from, edge_to,
                                allow_loops)
        edge_from = edge_to

    # We now have a working maze, but not a very interesting one, in that it
    # just has one random path from start to end. Add some blind alleys and
    # ensure that every node has at least one edge, which somehow connects to
    # the original random walk, hence the start (and the end)
    for i in range(node_count):
        if not (i in accessible):
            # random walk from i until we hit the accessible set
            new_path = {i}
            edge_from = i
            while not (edge_from in accessible):
                edge_to = local.random_step(edge_from, False)  # avoid the exit
                add_bidirectional_edges(maze, new_path, edge_from, edge_to,
                                        allow_loops)
                edge_from = edge_to

            # all these nodes are now accessible
            accessible.update(new_path)

    # We now have a maze with some blind alleys and all nodes are accessible.
    # Shuffle the edges in each node (we do not want the first edge to always
    # be the one that leads to the exit) and return it.
    for node in maze:
        shuffle(node)
    return maze
Ejemplo n.º 3
0
 def __init__(self, localizer=None, estimator=None):
     '''
     Parameters
     ----------
     pixels: tuple                    #coordinates instead?                                           
         (img_rows, img_cols)
     instrument: Instrument  
         Object resprenting the light-scattering instrument
     model_path: str
         path to model.h5 file
     '''
     if estimator is None:
         self.estimator = Estimator()
     else:
         self.estimator = estimator
     if localizer is None:
         self.localizer = Localizer()
     else:
         self.localizer = localizer
     self.instrument = estimator.instrument
Ejemplo n.º 4
0
    if saveImg:
        plt.savefig(saveImgPath, bbox_inches='tight')

    plt.show()


if __name__ == '__main__':
    eps = 4
    min_samples = 20
    theta = 5
    imgpath = "/home/tom/Data/IN-SITU/15/PanoramaSmallX_4Y_1.png"

    SEM = scipy.misc.imread(imgpath, flatten=True)

    L = Localizer(eps, min_samples, theta)
    centroids = L.predict(SEM)

    size = SEM.shape

    window_size = [250, 250]

    for i in range(len(centroids)):
        x1 = math.floor(centroids[i][0] - window_size[0] / 2)
        y1 = math.floor(centroids[i][1] - window_size[1] / 2)
        x2 = math.floor(centroids[i][0] + window_size[0] / 2)
        y2 = math.floor(centroids[i][1] + window_size[1] / 2)

        if y2 - y1 == 249:
            print y1, y2
            print centroids[i][1]
Ejemplo n.º 5
0
class EndtoEnd(object):
    '''
    Attributes
    __________
    localizer: Localizer
        Object resprenting the trained YOLO model
    estimator: Estimator
        Object representing the trained Keras model
    instrument: Instrument
        Object resprenting the light-scattering instrument
        
    Methods
    _______
    predict(img_names_path=None, img_list=[], save_predictions=False, predictions_path='predictions.json', save_crops=False, crop_dir='./cropped_img')
        loads img_names.txt from str 'img_names_path', imports images
        img_names.txt contains absolute paths of images, separated by line break
        or, just input images as a list
        predicts on list of images using self.model
        saves output to predictions_path if save_predictions = True
        saves cropped images to crop_dir if save_crops = True
    '''
    def __init__(self, localizer=None, estimator=None):
        '''
        Parameters
        ----------
        pixels: tuple                    #coordinates instead?                                           
            (img_rows, img_cols)
        instrument: Instrument  
            Object resprenting the light-scattering instrument
        model_path: str
            path to model.h5 file
        '''
        if estimator is None:
            self.estimator = Estimator()
        else:
            self.estimator = estimator
        if localizer is None:
            self.localizer = Localizer()
        else:
            self.localizer = localizer
        self.instrument = estimator.instrument

    @property
    def coordinates(self):
        return self._coordinates

    @coordinates.setter
    def coordinates(self, coordinates):
        self._coordinates = coordinates

    @property
    def instrument(self):
        return self._instrument

    @instrument.setter
    def instrument(self, instrument):
        self._instrument = instrument

    @property
    def estimator(self):
        return self._estimator

    @estimator.setter
    def estimator(self, estimator):
        self._estimator = estimator

    @property
    def localizer(self):
        return self._localizer

    @localizer.setter
    def localizer(self, localizer):
        self._localizer = localizer

    def predict(self, img_list=[], doubles_tol=0):
        '''
        output:
        predictions: list of features
        n images => n lists of features
        '''
        crop_px = self.estimator.pixels
        yolo_predictions = self.localizer.predict(img_list=img_list)
        yolo_predictions = nodoubles(yolo_predictions, tol=doubles_tol)
        (imcols, imrows, channels) = img_list[0].shape
        old_shape = (imrows, imcols)
        out_features = crop_feature(img_list=img_list,
                                    xy_preds=yolo_predictions,
                                    old_shape=old_shape,
                                    new_shape=crop_px)
        structure = list(map(len, out_features))
        flat_features = [item for sublist in out_features for item in sublist]
        imlist = [feat.data * 100 for feat in flat_features]
        char_predictions = self.estimator.predict(img_list=imlist)
        zpop = char_predictions['z_p']
        apop = char_predictions['a_p']
        npop = char_predictions['n_p']
        for framenum in range(len(structure)):
            listlen = structure[framenum]
            frame = out_features[framenum]
            index = 0
            while listlen > index:
                feature = frame[index]
                feature.model.particle.z_p = zpop.pop(0)
                feature.model.particle.a_p = apop.pop(0)
                feature.model.particle.n_p = npop.pop(0)
                feature.model.coordinates = feature.coordinates
                feature.model.instrument = self.instrument
                index += 1
        return out_features
Ejemplo n.º 6
0
        return out_features


if __name__ == '__main__':
    from lmfit import report_fit
    import cv2, json
    from matplotlib import pyplot as plt

    keras_head_path = 'keras_models/predict_stamp_auto'
    keras_model_path = keras_head_path + '.h5'
    keras_config_path = keras_head_path + '.json'
    with open(keras_config_path, 'r') as f:
        kconfig = json.load(f)
    estimator = Estimator(model_path=keras_model_path, config_file=kconfig)

    localizer = Localizer(configuration='holo')
    '''
    img_file = 'datasets/performance/eval/images/image0000.png'
    img_json = 'datasets/performance/eval/params/image0000.json'
    with open(img_json, 'r') as f:
        trueparams = json.load(f)
    print('Ground truth')
    print(trueparams)
    '''

    img_file = 'examples/test_image_large.png'
    img = cv2.imread(img_file)
    img_list = [img]

    e2e = EndtoEnd(estimator=estimator, localizer=localizer)
    features = e2e.predict(img_list=img_list)
Ejemplo n.º 7
0
        return out_features


if __name__ == '__main__':
    from lmfit import report_fit
    import cv2, json
    from matplotlib import pyplot as plt

    keras_head_path = 'keras_models/predict_stamp_newscale'
    keras_model_path = keras_head_path + '.h5'
    keras_config_path = keras_head_path + '.json'
    with open(keras_config_path, 'r') as f:
        kconfig = json.load(f)
    estimator = Estimator(model_path=keras_model_path, config_file=kconfig)

    localizer = Localizer(configuration='yolonew', weights='_100000')
    '''
    img_file = 'datasets/performance/eval/images/image0000.png'
    img_json = 'datasets/performance/eval/params/image0000.json'
    with open(img_json, 'r') as f:
        trueparams = json.load(f)
    print('Ground truth')
    print(trueparams)
    '''

    img_file = 'examples/test_image_large.png'
    img = cv2.imread(img_file)
    img_list = [img]

    e2e = EndtoEnd(estimator=estimator, localizer=localizer)
    features = e2e.predict(img_list=img_list)