Exemplo n.º 1
0
    def get_training_tile_data(self, tile_idx):
        tile = self.train_tiles[tile_idx]
        rect = geom.Rectangle(tile.scale(tile_size),
                              tile.add(geom.Point(1, 1)).scale(tile_size))

        if tries < 3:
            search_rect_x = random.randint(
                window_size / 2,
                tile_size - window_size / 2 - self.search_rect_size)
            search_rect_y = random.randint(
                window_size / 2,
                tile_size - window_size / 2 - self.search_rect_size)
            search_rect = geom.Rectangle(
                rect.start.add(geom.Point(search_rect_x, search_rect_y)),
                rect.start.add(geom.Point(search_rect_x, search_rect_y)).add(
                    geom.Point(self.search_rect_size, self.search_rect_size)),
            )

        return {
            'region': tile.region,
            'rect': rect,
            'search_rect': search_rect,
            'cache': self.cache,
            'starting_locations': [],
            'gc': self.gcs[tile.region],
        }
Exemplo n.º 2
0
def get_tile_example(tile, tries=10):
    rect = get_tile_rect(tile)

    # pick origin: must be multiple of the output scale
    origin = geom.Point(
        random.randint(0,
                       rect.lengths().x / 4 - WINDOW_SIZE / 4),
        random.randint(0,
                       rect.lengths().y / 4 - WINDOW_SIZE / 4))
    origin = origin.scale(4)
    origin = origin.add(rect.start)

    tile_origin = origin.sub(rect.start)
    big_ims = tiles.cache.get_window(
        tile.region, rect,
        geom.Rectangle(tile_origin,
                       tile_origin.add(geom.Point(WINDOW_SIZE, WINDOW_SIZE))))
    input = big_ims['input'].astype('float32') / 255.0
    target = big_ims['angles'].astype('float32') / 255.0
    if numpy.count_nonzero(target.max(axis=2)) < 64 and tries > 0:
        return get_tile_example(tile, tries - 1)
    example = {
        'region': tile.region,
        'origin': origin,
        'input': input,
        'target': target,
    }
    if MASK_NEAR_ROADS:
        mask = target.max(axis=2) > 0
        mask = scipy.ndimage.morphology.binary_dilation(mask, iterations=9)
        example['mask'] = mask
    return example
Exemplo n.º 3
0
def load_rect(region, rect, load_func=load_tile, mode='all'):
	# special case for fast load: rect is single tile
	if rect.start.x % tile_size == 0 and rect.start.y % tile_size == 0 and rect.end.x % tile_size == 0 and rect.end.y % tile_size == 0 and rect.end.x - rect.start.x == tile_size and rect.end.y - rect.start.y == tile_size:
		return load_func(region, rect.start.x / tile_size, rect.start.y / tile_size, mode=mode)

	tile_rect = geom.Rectangle(
		geom.Point(rect.start.x / tile_size, rect.start.y / tile_size),
		geom.Point((rect.end.x - 1) / tile_size + 1, (rect.end.y - 1) / tile_size + 1)
	)
	full_rect = geom.Rectangle(
		tile_rect.start.scale(tile_size),
		tile_rect.end.scale(tile_size)
	)
	full_ims = {}

	for i in xrange(tile_rect.start.x, tile_rect.end.x):
		for j in xrange(tile_rect.start.y, tile_rect.end.y):
			p = geom.Point(i - tile_rect.start.x, j - tile_rect.start.y).scale(tile_size)
			tile_ims = load_func(region, i, j, mode=mode)
			for k, im in tile_ims.iteritems():
				scale = tile_size / im.shape[0]
				if k not in full_ims:
					full_ims[k] = numpy.zeros((full_rect.lengths().x / scale, full_rect.lengths().y / scale, im.shape[2]), dtype='uint8')
				full_ims[k][p.x/scale:(p.x+tile_size)/scale, p.y/scale:(p.y+tile_size)/scale, :] = im

	crop_rect = geom.Rectangle(
		rect.start.sub(full_rect.start),
		rect.end.sub(full_rect.start)
	)
	for k in full_ims:
		scale = (full_rect.end.x - full_rect.start.x) / full_ims[k].shape[0]
		full_ims[k] = full_ims[k][crop_rect.start.x/scale:crop_rect.end.x/scale, crop_rect.start.y/scale:crop_rect.end.y/scale, :]
	return full_ims
Exemplo n.º 4
0
def prepare_connection(sat, outim, inferred_idx, connection, size=320):
	path = [geom.Point(p[0], p[1]) for p in connection['path']]
	r = path[0].bounds()
	for p in path:
		r = r.extend(p)
	l = r.lengths()
	if l.x > 256 or l.y > 256:
		return
	s = geom.Point((size - l.x)/2, (size - l.y)/2)
	r = geom.Rectangle(r.start.sub(s), r.end.add(s))
	r = geom.Rectangle(geom.Point(0, 0), geom.Point(sat.shape[0], sat.shape[1])).clip_rect(r)
	l = r.lengths()
	im = numpy.zeros((size, size, 6), dtype='uint8')
	im[0:l.x, 0:l.y, 0:3] = sat[r.start.x:r.end.x, r.start.y:r.end.y, :]
	im[0:l.x, 0:l.y, 5] = outim[r.start.x:r.end.x, r.start.y:r.end.y]

	# draw graph
	for edge in inferred_idx.search(r.add_tol(32)):
		segment = edge.segment()
		start = segment.start.sub(r.start)
		end = segment.end.sub(r.start)
		for p in geom.draw_line(start, end, r.lengths()):
			im[p.x, p.y, 3] = 255

	# draw connection
	for i in xrange(len(path) - 1):
		start = path[i].sub(r.start)
		end = path[i + 1].sub(r.start)
		for p in geom.draw_line(start, end, r.lengths()):
			im[p.x, p.y, 4] = 255

	return im
Exemplo n.º 5
0
def vis_example(example, outputs=None):
	x = numpy.zeros((WINDOW_SIZE, WINDOW_SIZE, 3), dtype='uint8')
	x[:, :, :] = example['input'] * 255
	x[WINDOW_SIZE/2-2:WINDOW_SIZE/2+2, WINDOW_SIZE/2-2:WINDOW_SIZE/2+2, :] = 255

	gc = tiles.get_gc(example['region'])
	rect = geom.Rectangle(example['origin'], example['origin'].add(geom.Point(WINDOW_SIZE, WINDOW_SIZE)))
	for edge in gc.edge_index.search(rect):
		start = edge.src.point
		end = edge.dst.point
		for p in geom.draw_line(start.sub(example['origin']), end.sub(example['origin']), geom.Point(WINDOW_SIZE, WINDOW_SIZE)):
			x[p.x, p.y, 0:2] = 0
			x[p.x, p.y, 2] = 255

	for i in xrange(WINDOW_SIZE):
		for j in xrange(WINDOW_SIZE):
			di = i - WINDOW_SIZE/2
			dj = j - WINDOW_SIZE/2
			d = math.sqrt(di * di + dj * dj)
			a = int((math.atan2(dj, di) - math.atan2(0, 1) + math.pi) * NUM_BUCKETS / 2 / math.pi)
			if a >= NUM_BUCKETS:
				a = NUM_BUCKETS - 1
			elif a < 0:
				a = 0
			elif d > 100 and d <= 120 and example['target'] is not None:
				x[i, j, 0] = example['target'][WINDOW_SIZE/8, WINDOW_SIZE/8, a] * 255
				x[i, j, 1] = example['target'][WINDOW_SIZE/8, WINDOW_SIZE/8, a] * 255
				x[i, j, 2] = 0
			elif d > 70 and d <= 90 and outputs is not None:
				x[i, j, 0] = outputs[WINDOW_SIZE/8, WINDOW_SIZE/8, a] * 255
				x[i, j, 1] = outputs[WINDOW_SIZE/8, WINDOW_SIZE/8, a] * 255
				x[i, j, 2] = 0
	return x
Exemplo n.º 6
0
def get_tile_rect(tile):
	if RECT_OVERRIDE:
		return RECT_OVERRIDE
	p = geom.Point(tile.x, tile.y)
	return geom.Rectangle(
		p.scale(TILE_SIZE),
		p.add(geom.Point(1, 1)).scale(TILE_SIZE)
	)
Exemplo n.º 7
0
	def best_angle_to_pos(pos):
		angle_points = [get_next_point(extension_vertex.point, angle_bucket, segment_length) for angle_bucket in range(64)]
		distances = [angle_point.distance(pos.point()) for angle_point in angle_points]
		point_angle = numpy.argmin(distances) * math.pi * 2 / 64.0 - math.pi
		edge_angle = geom.Point(1, 0).signed_angle(pos.edge.segment().vector())
		avg_vector = vector_from_angle(point_angle).add(vector_from_angle(edge_angle))
		avg_angle = geom.Point(1, 0).signed_angle(avg_vector)
		return int((avg_angle + math.pi) * 64.0 / math.pi / 2)
Exemplo n.º 8
0
def get_shortest_path(im, src, opp, edge_im, g, vertex_distances):
    r = src.bounds().add_tol(MAX_STRAIGHT_DISTANCE)
    r = geom.Rectangle(geom.Point(0, 0), geom.Point(im.shape[0],
                                                    im.shape[1])).clip_rect(r)
    seen_points = set()
    distances = {}
    prev = {}
    dst_edge = None
    dst_point = None

    distances[src] = 0
    while len(distances) > 0:
        closest_point = None
        closest_distance = None
        for point, distance in distances.items():
            if closest_point is None or distance < closest_distance:
                closest_point = point
                closest_distance = distance

        del distances[closest_point]
        seen_points.add(closest_point)
        if edge_im[closest_point.x, closest_point.y] >= 0:
            edge = g.edges[edge_im[closest_point.x, closest_point.y]]
            src_distance = vertex_distances.get(edge.src, MIN_GRAPH_DISTANCE)
            dst_distance = vertex_distances.get(edge.dst, MIN_GRAPH_DISTANCE)
            if src_distance + closest_point.distance(
                    edge.src.point
            ) >= MIN_GRAPH_DISTANCE and dst_distance + closest_point.distance(
                    edge.dst.point) >= MIN_GRAPH_DISTANCE:
                dst_edge = edge
                dst_point = closest_point
                break

        for offset in [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1),
                       (1, -1), (1, 1)]:
            adj_point = closest_point.add(geom.Point(offset[0], offset[1]))
            if r.contains(adj_point
                          ) and adj_point not in seen_points and src.distance(
                              adj_point) < opp.distance(adj_point):
                distance = closest_distance + 1 + (
                    1 - im[adj_point.x, adj_point.y])
                if adj_point not in distances or distance < distances[
                        adj_point]:
                    distances[adj_point] = distance
                    prev[adj_point] = closest_point

    if dst_edge is None:
        return None, None

    path = []
    point = dst_point
    while point != src:
        path.append(point)
        point = prev[point]
    path.append(src)
    path.reverse()

    return dst_edge, path
Exemplo n.º 9
0
def count_adjacent(skeleton, point):
    r = geom.Rectangle(geom.Point(0, 0),
                       geom.Point(skeleton.shape[0], skeleton.shape[1]))
    count = 0
    for offset in [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1),
                   (1, -1), (1, 1)]:
        adj_point = point.add(geom.Point(offset[0], offset[1]))
        if skeleton[adj_point.x, adj_point.y] > 0:
            count += 1
    return count
Exemplo n.º 10
0
 def best_angle_to_pos(pos):
     angle_points = [
         model_utils.get_next_point(point, angle_bucket, SEGMENT_LENGTH)
         for angle_bucket in xrange(64)
     ]
     distances = [
         angle_point.distance(pos.point()) for angle_point in angle_points
     ]
     point_angle = numpy.argmin(distances) * math.pi * 2 / 64.0 - math.pi
     edge_angle = geom.Point(1, 0).signed_angle(pos.edge.segment().vector())
     avg_vector = geom.vector_from_angle(point_angle, 50).add(
         geom.vector_from_angle(edge_angle, 50))
     avg_angle = geom.Point(1, 0).signed_angle(avg_vector)
     return int((avg_angle + math.pi) * 64.0 / math.pi / 2)
Exemplo n.º 11
0
def extract_features(im, connection):
	path = [geom.Point(p[0], p[1]) for p in connection['path']]
	samples = sample_points(path)
	softmax_scores = []
	distance_nonroad = []
	for point in samples:
		softmax_scores.append(im[point.x, point.y])
Exemplo n.º 12
0
def points_from_poly_str(s):
    parts = s.split(' ')
    points = []
    for part in parts:
        x, y = part.split(',')
        points.append(geom.Point(float(x), float(y)))
    return points
Exemplo n.º 13
0
	def draw(rs, distance, remaining, is_explored=False):
		start_edge_idx = rs.distance_to_edge(distance, return_idx=True)
		for edge_idx in xrange(start_edge_idx, len(rs.edges)):
			edge = rs.edges[edge_idx]
			edge_distance = distance - rs.edge_distances[edge.id]

			start_edge_pos = graph.EdgePos(edge, edge_distance)
			start = start_edge_pos.point()

			if edge_distance + remaining < edge.segment().length():
				end_edge_pos = graph.EdgePos(edge, edge_distance + remaining)
				end = end_edge_pos.point()
			else:
				end = edge.dst.point

			if not is_explored:
				for p in geom.draw_line(start.sub(origin), end.sub(origin), geom.Point(window_size, window_size)):
					p_small = p.scale(128.0 / window_size)
					tile[p_small.x, p_small.y] = 1.0

			remaining -= edge.segment().length() - edge_distance
			distance = rs.edge_distances[edge.id] + edge.segment().length() + 0.001
			if remaining <= 0:
				return

		for next_rs in rs.out_rs(path.gc.edge_to_rs):
			if rs == next_rs or next_rs.is_opposite(rs):
				continue
			draw(next_rs, 0, remaining, is_explored=path.is_explored(next_rs.edges[0]))
Exemplo n.º 14
0
def process(region):
    print region
    city = region.split('_')[0]
    offset_x, offset_y = int(region.split('_')[1]), int(region.split('_')[2])

    sat = scipy.ndimage.imread('{}/{}_sat.png'.format(sat_path, region))
    sat = sat.swapaxes(0, 1)
    im = scipy.ndimage.imread('{}/{}.png'.format(
        out_im_path, region)).astype('float32') / 255.0
    im = im.swapaxes(0, 1)
    g = graph.read_graph('{}/{}.graph'.format(out_graph_path, region))
    g_idx = g.edgeIndex()

    gt = graph.read_graph('{}/{}.graph'.format(graph_path, city))
    offset = geom.Point(offset_x * 4096, offset_y * 4096)
    for vertex in gt.vertices:
        vertex.point = vertex.point.sub(offset)

    gt_idx = gt.edgeIndex()

    connections = get_connections.get_connections(g, im, limit=512)
    good, bad = label_gt.label_connections(gt, g, connections)
    for i, connection in enumerate(good):
        label_gt.write_connection(sat, im, g_idx, connection,
                                  '{}/good/{}_{}'.format(dst_path, region, i))
    for i, connection in enumerate(bad):
        label_gt.write_connection(sat, im, g_idx, connection,
                                  '{}/bad/{}_{}'.format(dst_path, region, i))
Exemplo n.º 15
0
def get_starting_locations(gcs, segment_length, region=None):
    all_starting_locations = {}
    with open(startlocs_path, 'r') as f:
        # top-level is dict from tile to starting location lists
        for tile, locs in json.load(f).items():
            tile_region = tile.split('_')[0]

            if region is not None and tile_region != region:
                continue
            elif tile_region not in gcs:
                continue

            starting_locations = []
            # each loc is a dict with keys 'x', 'y', 'edge_id'
            for loc in locs:
                point = geom.Point(int(loc['x']), int(loc['y']))
                edge_pos = gcs[tile_region].graph.edges[int(
                    loc['edge_id'])].closest_pos(point)
                next_positions = graph.follow_graph(edge_pos, segment_length)

                if not next_positions:
                    continue

                starting_locations.append([{
                    'point': point,
                    'edge_pos': edge_pos,
                }, {
                    'point': next_positions[0].point(),
                    'edge_pos': next_positions[0],
                }])
            all_starting_locations[tile] = starting_locations
    return all_starting_locations
Exemplo n.º 16
0
 def sample_disjoint(origin):
     while True:
         i = random.randint(0, 4096 - SIZE)
         j = random.randint(0, 4096 - SIZE)
         if i >= origin.x and i < origin.x + SIZE and j >= origin.y and j < origin.y + SIZE:
             continue
         return geom.Point(i, j)
Exemplo n.º 17
0
def load_rect(region, rect, load_func=load_tile, mode='all'):
    # special case for fast load: rect is single tile
    if rect.start.x % tile_size == 0 and rect.start.y % tile_size == 0 and rect.end.x % tile_size == 0 and rect.end.y % tile_size == 0 and rect.end.x - rect.start.x == tile_size and rect.end.y - rect.start.y == tile_size:
        tile = load_rect(region,
                         int(rect.start.x / tile_size),
                         int(rect.start.y / tile_size),
                         mode=mode)
        return tile

    tile_rect = geom.Rectangle(
        geom.Point(rect.start.x / tile_size, rect.start.y / tile_size),
        geom.Point((rect.end.x - 1) / tile_size + 1,
                   (rect.end.y - 1) / tile_size + 1))
    full_rect = geom.Rectangle(tile_rect.start.scale(tile_size),
                               tile_rect.end.scale(tile_size))
    full_ims = {}

    for i in range(tile_rect.start.x, tile_rect.end.x):
        for j in range(tile_rect.start.y, tile_rect.end.y):
            p = geom.Point(i - tile_rect.start.x,
                           j - tile_rect.start.y).scale(tile_size)
            tile_ims = load_func(region, i, j, mode=mode)
            for k, im in tile_ims.items():
                print('tile_size, im shape', tile_size, im.shape[0])
                scale = tile_size / im.shape[0]
                if k not in full_ims:
                    full_ims[k] = numpy.zeros(
                        (int(full_rect.lengths().x / scale),
                         int(full_rect.lengths().y / scale), im.shape[2]),
                        dtype='uint8')
                full_ims[k][int(p.x / scale):int((p.x + tile_size) / scale),
                            int(p.y / scale):int((p.y + tile_size) /
                                                 scale), :] = im
                print('scale: {},fullims: {}'.format(scale,
                                                     numpy.shape(full_ims[k])))

    crop_rect = geom.Rectangle(rect.start.sub(full_rect.start),
                               rect.end.sub(full_rect.start))
    print('crop_rect', crop_rect.start, crop_rect.end)
    for k in full_ims:
        scale = (full_rect.end.x - full_rect.start.x) / full_ims[k].shape[0]
        full_ims[k] = full_ims[k][int(crop_rect.start.x /
                                      scale):int(crop_rect.end.x / scale),
                                  int(crop_rect.start.y /
                                      scale):int(crop_rect.end.y / scale), :]
    return full_ims
Exemplo n.º 18
0
def get_tile_list():
    tiles = []
    with open(pytiles_path, 'r') as f:
        for json_tile in json.load(f):
            tile = geom.Point(int(json_tile['x']), int(json_tile['y']))
            tile.region = json_tile['region']
            tiles.append(tile)
    return tiles
Exemplo n.º 19
0
def vis_example(example, outputs=None):
    info, input, angle_targets, detect_targets = example
    x = numpy.zeros((WINDOW_SIZE, WINDOW_SIZE, 3), dtype='uint8')
    x[:, :, :] = input * 255
    x[WINDOW_SIZE / 2 - 2:WINDOW_SIZE / 2 + 2,
      WINDOW_SIZE / 2 - 2:WINDOW_SIZE / 2 + 2, :] = 255

    gc = tiles.get_gc(info['region'])
    rect = geom.Rectangle(
        info['origin'], info['origin'].add(geom.Point(WINDOW_SIZE,
                                                      WINDOW_SIZE)))
    for edge in gc.edge_index.search(rect):
        start = edge.src.point
        end = edge.dst.point
        for p in geom.draw_line(start.sub(info['origin']),
                                end.sub(info['origin']),
                                geom.Point(WINDOW_SIZE, WINDOW_SIZE)):
            x[p.x, p.y, 0:2] = 0
            x[p.x, p.y, 2] = 255

    if info['closest_pos'] is not None:
        p = info['closest_pos'].point().sub(info['origin'])
        x[p.x - 2:p.x + 2, p.y - 2:p.y + 2, 0] = 255
        x[p.x - 2:p.x + 2, p.y - 2:p.y + 2, 1:3] = 0

    for i in xrange(WINDOW_SIZE):
        for j in xrange(WINDOW_SIZE):
            di = i - WINDOW_SIZE / 2
            dj = j - WINDOW_SIZE / 2
            d = math.sqrt(di * di + dj * dj)
            a = int((math.atan2(dj, di) - math.atan2(0, 1) + math.pi) * 64 /
                    2 / math.pi)
            if a >= 64:
                a = 63
            elif a < 0:
                a = 0
            elif d > 100 and d <= 120 and angle_targets is not None:
                x[i, j, 0] = angle_targets[a] * 255
                x[i, j, 1] = angle_targets[a] * 255
                x[i, j, 2] = 0
            elif d > 70 and d <= 90 and outputs is not None:
                x[i, j, 0] = outputs[a] * 255
                x[i, j, 1] = outputs[a] * 255
                x[i, j, 2] = 0
    return x
Exemplo n.º 20
0
def get_reachable_points(im, point, iterations):
    points = set()
    search = set()
    r = geom.Rectangle(geom.Point(0, 0),
                       geom.Point(im.shape[0] - 1, im.shape[1] - 1))
    search.add(point)
    for _ in xrange(iterations):
        next_search = set()
        for point in search:
            for offset in [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1),
                           (1, -1), (1, 1)]:
                adj_point = point.add(geom.Point(offset[0], offset[1]))
                if r.contains(adj_point) and adj_point not in points and im[
                        adj_point.x, adj_point.y] > 0:
                    points.add(adj_point)
                    next_search.add(adj_point)
        search = next_search
    return points
Exemplo n.º 21
0
	def get_test_tile_data(self):
		if 'chicago' not in REGIONS:
			return None

		rect = geom.Rectangle(
			geom.Point(1024, -8192),
			geom.Point(4096, -5376),
		)

		starting_locations = self.all_starting_locations['chicago_0_-2']
		starting_locations = [loc for loc in starting_locations if rect.add_tol(-window_size).contains(loc[0]['point'])]
		return {
			'region': 'chicago',
			'rect': rect,
			'search_rect': rect.add_tol(-window_size/2),
			'cache': self.cache,
			'starting_locations': starting_locations,
			'gc': self.gcs['chicago'],
		}
Exemplo n.º 22
0
		def tile_filter(tile):
			if tile.region not in REGIONS:
				return False
			rect = geom.Rectangle(
				tile.scale(tile_size),
				tile.add(geom.Point(1, 1)).scale(tile_size)
			)
			starting_locations = self.all_starting_locations['{}_{}_{}'.format(tile.region, tile.x, tile.y)]
			starting_locations = [loc for loc in starting_locations if rect.add_tol(-window_size).contains(loc[0]['point'])]
			return len(starting_locations) > 0
Exemplo n.º 23
0
def visualize_connection(sat, gt_idx, inferred_idx, connection, fname, good=True):
	path = [geom.Point(p[0], p[1]) for p in connection['path']]
	r = path[0].bounds()
	for p in path:
		r = r.extend(p)
	r = r.add_tol(128)
	r = geom.Rectangle(geom.Point(0, 0), geom.Point(sat.shape[0], sat.shape[1])).clip_rect(r)
	im = numpy.copy(sat[r.start.x:r.end.x, r.start.y:r.end.y])
	im_rect = geom.Rectangle(geom.Point(0, 0), geom.Point(im.shape[0], im.shape[1]))

	def color_point(p, color, tol=1):
		s = im_rect.clip(p.sub(geom.Point(tol, tol)))
		e = im_rect.clip(p.add(geom.Point(tol, tol)))
		im[s.x:e.x+1, s.y:e.y+1, :] = color

	# draw graph yellow
	for edge in inferred_idx.search(r.add_tol(32)):
		segment = edge.segment()
		start = segment.start.sub(r.start)
		end = segment.end.sub(r.start)
		for p in geom.draw_line(start, end, r.lengths()):
			color_point(p, [255, 255, 0])

	# draw connection red or green
	for i in xrange(len(path) - 1):
		start = path[i].sub(r.start)
		end = path[i + 1].sub(r.start)
		for p in geom.draw_line(start, end, r.lengths()):
			if good:
				color_point(p, [0, 255, 0])
			else:
				color_point(p, [255, 0, 0])

	# draw gt graph blue
	for edge in gt_idx.search(r.add_tol(32)):
		segment = edge.segment()
		start = segment.start.sub(r.start)
		end = segment.end.sub(r.start)
		for p in geom.draw_line(start, end, r.lengths()):
			color_point(p, [0, 0, 255], tol=0)

	Image.fromarray(im.swapaxes(0, 1)).save(fname)
Exemplo n.º 24
0
def get_compact_path_input(path, extension_vertex, window_size=512):
	origin = extension_vertex.point.sub(geom.Point(window_size/2, window_size/2))
	rect = origin.bounds().extend(origin.add(geom.Point(window_size, window_size)))

	path_segments = []
	for edge_id in path.edge_rtree.intersection((rect.start.x, rect.start.y, rect.end.x, rect.end.y)):
		path_segments.append(path.graph.edges[edge_id].segment())

	gt_segments = []
	for edge in path.gc.edge_index.search(rect):
		gt_segments.append(edge.segment())

	return {
		'window_size': window_size,
		'cache': path.tile_data['cache'],
		'region': path.tile_data['region'],
		'big_rect': path.tile_data['rect'],
		'origin': origin,
		'path_segments': path_segments,
		'gt_segments': gt_segments,
	}
Exemplo n.º 25
0
def get_shortest_path(im, src, max_distance):
    r = geom.Rectangle(geom.Point(0, 0), geom.Point(im.shape[0], im.shape[1]))
    in_r = r.add_tol(-1)
    seen_points = set()
    distances = {}
    prev = {}
    dst = None

    distances[src] = 0
    while len(distances) > 0:
        closest_point = None
        closest_distance = None
        for point, distance in distances.items():
            if closest_point is None or distance < closest_distance:
                closest_point = point
                closest_distance = distance

        del distances[closest_point]
        seen_points.add(closest_point)
        if closest_distance > max_distance:
            break
        elif not in_r.contains(closest_point):
            dst = closest_point
            break

        for offset in [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1),
                       (1, -1), (1, 1)]:
            adj_point = closest_point.add(geom.Point(offset[0], offset[1]))
            if r.contains(adj_point) and adj_point not in seen_points:
                distance = closest_distance + distance_from_value(
                    im[adj_point.x, adj_point.y])
                if adj_point not in distances or distance < distances[
                        adj_point]:
                    distances[adj_point] = distance
                    prev[adj_point] = closest_point

    if dst is None:
        return

    return dst
Exemplo n.º 26
0
    def set_by_positions(positions):
        # get existing angle buckets, don't use any that are within 3 buckets
        bad_buckets = set()
        for edge in extension_vertex.out_edges:
            edge_angle = geom.Point(1, 0).signed_angle(edge.segment().vector())
            edge_bucket = int((edge_angle + math.pi) * 64.0 / math.pi / 2)
            for offset in xrange(3):
                clockwise_bucket = (edge_bucket + offset) % 64
                counterclockwise_bucket = (edge_bucket + 64 - offset) % 64
                bad_buckets.add(clockwise_bucket)
                bad_buckets.add(counterclockwise_bucket)

        for pos in positions:
            best_angle_bucket = best_angle_to_pos(pos)
            if best_angle_bucket in bad_buckets:
                continue
            set_angle_bucket_soft(best_angle_bucket)
Exemplo n.º 27
0
def get_tile_list():
    tiles = []
    with open(pytiles_path, 'r') as f:
        for json_tile in json.load(f):
            tile = geom.Point(int(json_tile['x']), int(json_tile['y']))
            tile.region = json_tile['region']
            tiles.append(tile)
    downloaded = set([
        fname.split('_sat.png')[0] for fname in os.listdir(get_tile_dirs()[0])
        if '_sat.png' in fname
    ])
    dl_tiles = [
        tile for tile in tiles
        if '{}_{}_{}'.format(tile.region, tile.x, tile.y) in downloaded
    ]
    print 'found {} total tiles, using {} downloaded tiles'.format(
        len(tiles), len(dl_tiles))
    return dl_tiles
Exemplo n.º 28
0
def get_tile_list():
    tiles = []
    for fname in os.listdir(get_tile_dirs()[0]):
        if not fname.endswith('_sat.jpg'):
            continue
        parts = fname.split('_sat.jpg')[0].split('_')
        region = parts[0]
        x, y = int(parts[1]), int(parts[2])
        tile = geom.Point(x, y)
        tile.region = region
        tiles.append(tile)
    if angles_dir:
        # filter tiles for only those that are in angles_dir
        angle_keys = set(os.listdir(angles_dir))
        tiles = [
            tile for tile in tiles
            if '{}_{}_{}.bin'.format(tile.region, tile.x, tile.y) in angle_keys
        ]
    return tiles
Exemplo n.º 29
0
def insert_connections(g, connections):
    split_edges = {
    }  # map from edge to (split pos, new edge before pos, new edge after pos)
    for idx, connection in enumerate(connections):
        # figure out which current edge the connection intersects
        edge = g.edges[connection['edge']]
        path = [geom.Point(p[0], p[1]) for p in connection['path']]
        intersection_point = path[-1]
        while edge in split_edges:
            our_pos = edge.closest_pos(intersection_point).distance
            if our_pos < split_edges[edge]['pos']:
                edge = split_edges[edge]['before']
            else:
                edge = split_edges[edge]['after']

        # add path vertices
        prev_vertex = g.vertices[connection['src']]
        for point in path[1:]:
            vertex = g.add_vertex(point)
            edge1, edge2 = g.add_bidirectional_edge(prev_vertex, vertex)
            edge1.phantom = True
            edge1.connection_idx = idx
            edge2.phantom = True
            edge2.connection_idx = idx
            prev_vertex = vertex

        # split the edge
        new_vertex = prev_vertex
        for edge in [edge, edge.get_opposite_edge()]:
            split_pos = edge.closest_pos(intersection_point).distance
            split_edges[edge] = {
                'pos': split_pos,
                'before': g.add_edge(edge.src, new_vertex),
                'after': g.add_edge(new_vertex, edge.dst),
            }

    # remove extraneous edges
    filter_edges = set([edge for edge in split_edges.keys()])
    g = g.filter_edges(filter_edges)
    return g
Exemplo n.º 30
0
	def get_training_tile_data_normal(self, tile, tries=0):
		rect = geom.Rectangle(
			tile.scale(tile_size),
			tile.add(geom.Point(1, 1)).scale(tile_size)
		)

		if tries < 3:
			search_rect_x = random.randint(window_size/2, tile_size - window_size/2 - self.search_rect_size)
			search_rect_y = random.randint(window_size/2, tile_size - window_size/2 - self.search_rect_size)
			search_rect = geom.Rectangle(
				rect.start.add(geom.Point(search_rect_x, search_rect_y)),
				rect.start.add(geom.Point(search_rect_x, search_rect_y)).add(geom.Point(self.search_rect_size, self.search_rect_size)),
			)
			starting_locations = self.all_starting_locations['{}_{}_{}'.format(tile.region, tile.x, tile.y)]
			starting_locations = [loc for loc in starting_locations if search_rect.add_tol(-window_size/4).contains(loc[0]['point'])]
		else:
			starting_locations = self.all_starting_locations['{}_{}_{}'.format(tile.region, tile.x, tile.y)]
			starting_locations = [loc for loc in starting_locations if rect.add_tol(-window_size).contains(loc[0]['point'])]
			starting_location = random.choice(starting_locations)
			search_rect_min = starting_location[0]['point'].sub(geom.Point(self.search_rect_size / 2, self.search_rect_size / 2)).sub(rect.start)

			if search_rect_min.x < window_size/2:
				search_rect_min.x = window_size/2
			elif search_rect_min.x > tile_size - window_size/2 - self.search_rect_size:
				search_rect_min.x = tile_size - window_size/2 - self.search_rect_size

			if search_rect_min.y < window_size/2:
				search_rect_min.y = window_size/2
			elif search_rect_min.y > tile_size - window_size/2 - self.search_rect_size:
				search_rect_min.y = tile_size - window_size/2 - self.search_rect_size

			search_rect = geom.Rectangle(
				rect.start.add(search_rect_min),
				rect.start.add(search_rect_min).add(geom.Point(self.search_rect_size, self.search_rect_size)),
			)
			starting_locations = [starting_location]

		if not starting_locations:
			return self.get_training_tile_data_normal(tile, tries + 1)
		return {
			'region': tile.region,
			'rect': rect,
			'search_rect': search_rect,
			'cache': self.cache,
			'starting_locations': starting_locations,
			'gc': self.gcs[tile.region],
		}