Ejemplo n.º 1
0
def intersection_over_union(target, list_of_bboxes, xp):
    print list_of_bboxes.shape
    target = LinearRing(get_vertex(target))
    list_of_bboxes = [LinearRing(get_vertex(i)) for i in list_of_bboxes]
    areas = xp.zeros(len(list_of_bboxes))
    for index, item in enumerate(list_of_bboxes):
        vertex = []
        if target.intersects(item):
            if isinstance(target.intersection(item),Point):
                continue
            for ver in target.intersection(item):
                if isinstance(ver,LineString):
                    u, v = ver.xy
                    vertex.append([u[0], v[0]])
                    vertex.append([u[1], v[1]])
                else:
                    vertex.append([ver.x, ver.y])
            for ver in target.coords[:4]:
                if Point(ver).within(Polygon(item)):
                    vertex.append(list(ver))
            for ver in item.coords[:4]:
                if Point(ver).within(Polygon(target)):
                    vertex.append(list(ver))
            areas[index] = Polygon(PolygonSort(vertex)).area
    return areas
Ejemplo n.º 2
0
def IoU(bbox_a,bbox_b):
    bbox_a = LinearRing(get_vertex(bbox_a))
    bbox_b = LinearRing(get_vertex(bbox_b))
    vertex = []
    if bbox_a.intersects(bbox_b):
        if isinstance(bbox_a.intersection(bbox_b),Point):
            return 0
        for ver in bbox_a.intersection(bbox_b):
            if isinstance(ver,LineString):
                u, v = ver.xy
                vertex.append([u[0], v[0]])
                vertex.append([u[1], v[1]])
            else:
                vertex.append([ver.x, ver.y])
        for ver in bbox_a.coords[:4]:
            if Point(ver).within(Polygon(bbox_b)):
                vertex.append(list(ver))
        for ver in bbox_b.coords[:4]:
            if Point(ver).within(Polygon(bbox_a)):
                vertex.append(list(ver))
        return Polygon(PolygonSort(vertex)).area
    return 0
Ejemplo n.º 3
0
def fig_intersects(fig1, fig2) -> float:
    lr1 = LinearRing(fig1)
    lr2 = LinearRing(fig2)
    return 1 if lr1.intersects(lr2) else 0
Ejemplo n.º 4
0
    def TakeAction(self, action):
        if not self.injail:
            proposal = Point((self.current_state.x + action[0],
                              self.current_state.y + action[1]))
            #print proposal.x, proposal.y
            path = LineString([self.current_state, proposal])

            if self.obstacles:
                for obstacle in self.obstacles:
                    if obstacle.intersects(path):
                        self.injail = True
                        self.current_state = Point((-1, -1))
                        return
            if self.muds:
                for mud in self.muds:
                    if mud.intersects(path):
                        #                         print 'we are here'
                        path_inmud = mud.intersection(path)
                        coords = [path.coords[0], path.coords[1]]
                        for loc in path_inmud.coords:
                            if loc not in coords:
                                coords.append(loc)
                        coords.sort(key=lambda tup: tup[1])
                        p_in_mud = proposal.intersects(mud)
                        s_in_mud = self.current_state.intersects(mud)
                        if p_in_mud and not s_in_mud:
                            #                             print 'current not in mud'
                            if coords.index((self.current_state.x,
                                             self.current_state.y)) == 0:
                                x = coords[1][0] - coords[0][0] + 0.5 * (
                                    coords[-1][0] - coords[1][0])
                                y = coords[1][1] - coords[0][1] + 0.5 * (
                                    coords[-1][1] - coords[1][1])
                                proposal = Point(
                                    (coords[0][0] + x, coords[0][1] + y))
                            else:
                                x = coords[1][0] - coords[-1][0] + 0.5 * (
                                    coords[0][0] - coords[1][0])
                                y = coords[1][1] - coords[-1][1] + 0.5 * (
                                    coords[0][1] - coords[1][1])
                                proposal = Point(
                                    (coords[-1][0] + x, coords[-1][1] + y))
                        elif s_in_mud and not p_in_mud:
                            #                             print 'proposal not in mud'
                            if coords.index((self.current_state.x,
                                             self.current_state.y)) == 0:
                                x = 0.5 * (coords[1][0] - coords[0][0]) + (
                                    coords[-1][0] - coords[1][0])
                                y = 0.5 * (coords[1][1] - coords[0][1]) + (
                                    coords[-1][1] - coords[1][1])
                                proposal = Point(
                                    (coords[0][0] + x, coords[0][1] + y))
                            else:
                                x = 0.5 * (coords[1][0] - coords[-1][0]) + (
                                    coords[0][0] - coords[1][0])
                                y = 0.5 * (coords[1][1] - coords[-1][1]) + (
                                    coords[0][1] - coords[1][1])
                                proposal = Point(
                                    (coords[-1][0] + x, coords[-1][1] + y))
                        else:
                            proposal = Point(
                                (self.current_state.x + action[0] * 0.5,
                                 self.current_state.y + action[1] * 0.5))

            path = LineString([self.current_state, proposal])
            bounds = LinearRing(self.maze.exterior.coords)
            if bounds.intersects(path):
                onedge = bounds.intersection(path)
                if type(onedge) is MultiPoint:
                    for point in onedge:
                        if not point.equals(self.current_state):
                            proposal = point
                elif type(onedge) is Point:
                    if not onedge.equals(self.current_state):
                        proposal = onedge
                    elif not self.maze.contains(proposal):
                        proposal = bounds.interpolate(bounds.project(proposal))

            self.current_state = proposal
        else:
            self.deadend_toleration = self.deadend_toleration - 1
        return self.GetCurrentState()