Exemple #1
0
 def __init__(self, bbox, configuration=None):
     self.configuration = Configuration(
     ) if configuration is None else configuration
     self.bbox = bbox
     self.tile = None
     self.streets = []
     self.convnet = None
     self.square_image_length = 50
     self.max_distance = self._calculate_max_distance(
         self.configuration.DETECTION.zoomlevel, self.square_image_length)
     self.image_api = OtherApi(
         self.configuration.DETECTION.zoomlevel
     ) if self.configuration.DETECTION.orthophoto is 'other' else self._get_image_api(
         self.configuration.DETECTION.orthophoto)
Exemple #2
0
def read_config(args):
    config_file = args.config
    config = configparser.ConfigParser()
    if not os.path.isfile(config_file):
        raise Exception("The config file does not exist! " + config_file)
    config.read(config_file)
    configuration = Configuration()
    if not args.standalone:
        configuration.check_redis_fields(config)
    configuration.set_from_config_parser(config)

    if args.role is 'manager':
        configuration.check_manager_config(config)
    return configuration
Exemple #3
0
def test_manager_standalone(store_path):
    small_bbox = Bbox(left=8.83848086,
                      bottom=47.2218996495,
                      right=8.8388215005,
                      top=47.2220713398)
    manager = Manager(bbox=small_bbox,
                      standalone=True,
                      configuration=Configuration(
                          dict(bbox_size=50, compare=False)))
    manager.run()
    with open(store_path, 'r') as f:
        data = json.load(f)
    assert len(data['nodes']) == 1
def read_config(args):
    config_file = args.config
    config = configparser.ConfigParser()
    if not os.path.isfile(config_file):
        raise Exception("The config file does not exist! " + config_file)
    config.read(config_file)
    configuration = Configuration()
    if not args.standalone:
        configuration.check_redis_fields(config)
    configuration.set_from_config_parser(config)

    if args.role is 'manager':
        configuration.check_manager_config(config)
    return configuration
Exemple #5
0
def main_func():
    set_logger()
    parser = argparse.ArgumentParser(description='Detect crosswalks.', )
    parser.add_argument(
        '-c',
        '--config',
        action='store',
        dest='config',
        required=True,
        help='The path to the configuration file.'
    )

    parser.add_argument(
        '-s',
        '--standalone',
        dest='standalone',
        action='store_true',
        help='If chosen the detection will run standalone and save the results in "crosswalks.json".')
    parser.set_defaults(standalone=False)

    subparsers = parser.add_subparsers(
        title='worker roles',
        description='',
        dest='role',
        help='Select the role of this process'
    )

    subparsers.required = True

    p_manager = subparsers.add_parser(
        'manager',
        help='Splits up the given bounding box (WGS84, minlon/minlat/maxlon/maxlat) into small pieces and puts them into the redis queue to be consumed by the jobworkers.')
    p_manager.add_argument(
        'bb_left',
        type=float,
        action='store',
        help='left float value of the bounding box (WGS84, minlon)')
    p_manager.add_argument(
        'bb_bottom',
        type=float,
        action='store',
        help='bottom float value of the bounding box (WGS84, minlat)')
    p_manager.add_argument(
        'bb_right',
        type=float,
        action='store',
        help='right float value of the bounding box (WGS84, maxlon)')
    p_manager.add_argument(
        'bb_top',
        type=float,
        action='store',
        help='top float value of the bounding box (WGS84, maxlat)')
    p_manager.set_defaults(func=manager)

    p_jobworker = subparsers.add_parser(
        'jobworker',
        help='Detect crosswalks on element from the redis queue.')
    p_jobworker.set_defaults(func=job_worker)

    p_resultworker = subparsers.add_parser(
        'resultworker',
        help='Consolidate and write out results.')
    p_resultworker.set_defaults(func=result_worker)

    args = parser.parse_args()
    configuration = Configuration(args.config)
    args.func(args, configuration)
def configuration():
    return Configuration()
def test_parameter():
    port = 1991
    config = Configuration(dict(port=port))
    assert config.port == port
def configuration():
    return Configuration(
        os.path.dirname(os.path.realpath(__file__)) + '/../test_config.ini')
Exemple #9
0
def configuration_no_compare():
    parameters = dict(compare=False)
    return Configuration(parameters=parameters)
Exemple #10
0
def configuration_no_compare():
    configuration = Configuration(os.path.dirname(os.path.realpath(__file__)) + '/test_config.ini')
    configuration.DETECTION.compare = 'false'
    return configuration
Exemple #11
0
class BoxWalker:
    def __init__(self, bbox, configuration=None):
        self.configuration = Configuration(
        ) if configuration is None else configuration
        self.bbox = bbox
        self.tile = None
        self.streets = []
        self.convnet = None
        self.square_image_length = 50
        self.max_distance = self._calculate_max_distance(
            self.configuration.DETECTION.zoomlevel, self.square_image_length)
        self.image_api = OtherApi(
            self.configuration.DETECTION.zoomlevel
        ) if self.configuration.DETECTION.orthophoto is 'other' else self._get_image_api(
            self.configuration.DETECTION.orthophoto)

    @staticmethod
    def _get_image_api(image_api):
        source = 'src.data.orthofoto.' + image_api + '.' + image_api + '_api'
        module = import_module(source)
        class_ = getattr(module, image_api.title() + 'Api')
        return class_()

    def load_convnet(self):
        self.convnet = Detector(
            labels_file=self.configuration.DETECTION.labels,
            graph_file=self.configuration.DETECTION.network)
        if not self.configuration.DETECTION.word in self.convnet.labels:
            error_message = self.configuration.DETECTION.word + " is not in label file."
            logger.error(error_message)
            raise Exception(error_message)

    def load_tiles(self):
        self._printer("Start image loading.")
        loader = TileLoader(bbox=self.bbox, image_api=self.image_api)
        loader.load_tile()
        self.tile = loader.tile
        self._printer("Stop image loading.")

    def load_streets(self):
        self._printer("Start street loading.")
        street_loader = StreetLoader()
        self.streets = street_loader.load_data(self.bbox)
        self._printer(str(len(self.streets)) + " streets to walk.")
        self._printer("Stop street loading.")

    def walk(self):
        ready_for_walk = (not self.tile is None) and (not self.convnet is None)
        if not ready_for_walk:
            error_message = "Not ready for walk. Load tiles and convnet first"
            logger.error(error_message)
            raise Exception(error_message)

        self._printer("Start detection.")

        follow_streets = self.configuration.to_bool(
            self.configuration.DETECTION.followstreets)

        if follow_streets:
            tiles = self._get_tiles_of_box_with_streets(
                self.streets, self.tile)
        else:
            tiles = self._get_tiles_of_box(self.tile)

        self._printer("{0} images to analyze.".format(str(len(tiles))))

        images = [tile.image for tile in tiles]
        predictions = self.convnet.detect(images)
        detected_nodes = []
        for i, _ in enumerate(tiles):
            prediction = predictions[i]
            if self.hit(prediction):
                detected_nodes.append(tiles[i].get_centre_node())
        self._printer("Stop detection.")
        merged_nodes = self._merge_near_nodes(detected_nodes)

        compare = self.configuration.to_bool(
            self.configuration.DETECTION.compare)
        print('Compare with OpenStreetMap entries: {0}'.format(compare))
        if compare:
            return self._compare_with_osm(merged_nodes)
        return merged_nodes

    def _get_tiles_of_box(self, tile):
        tile_walker = TileWalker(
            tile=tile,
            square_image_length=self.square_image_length,
            zoom_level=self.configuration.DETECTION.zoomlevel,
            step_width=self.configuration.DETECTION.stepwidth)
        tiles = tile_walker.get_tiles()
        return tiles

    def _get_tiles_of_box_with_streets(self, streets, tile):
        street_walker = StreetWalker(
            tile=tile,
            square_image_length=self.square_image_length,
            zoom_level=self.configuration.DETECTION.zoomlevel,
            step_width=self.configuration.DETECTION.stepwidth)
        tiles = []
        for street in streets:
            street_tiles = street_walker.get_tiles(street)
            tiles += street_tiles
        return tiles

    def _merge_near_nodes(self, node_list):
        merger = NodeMerger(node_list, self.max_distance)
        return merger.reduce()

    def _compare_with_osm(self, detected_nodes):
        comparator = OsmComparator(max_distance=self.max_distance)
        tag = Tag(key=self.configuration.DETECTION.key,
                  value=self.configuration.DETECTION.value)
        return comparator.compare(detected_nodes=detected_nodes,
                                  tag=tag,
                                  bbox=self.bbox)

    def hit(self, prediction):
        return prediction[self.configuration.DETECTION.word] > float(
            self.configuration.DETECTION.barrier)

    @staticmethod
    def _printer(message):
        print("{0}: {1}".format(str(datetime.datetime.now()), message))

    @staticmethod
    def _calculate_max_distance(zoom_level, square_image_length):
        global_mercator = GlobalMercator()
        resolution = global_mercator.Resolution(zoom_level)
        return resolution * (square_image_length / 2)
Exemple #12
0
 def _configuration(configuration):
     return Configuration() if configuration is None else configuration