示例#1
0
def userImageLabelAnalytics(image: Image, user1: User, label: Label):
    myPolygons = list(image.polygons.filter(created_by=user1, label=label))
    myShapePolygons = []
    for i in range(len(myPolygons)):
        myShapePolygons.append(ShapePolygon(json.loads(myPolygons[i].points)))
    users = User.objects.all()
    table = [[user.username for user in list(users)]]
    table[0].remove(user1.username)
    row = []
    for user2 in users:
        if (user1.id != user2.id):
            otherPolygons = list(image.polygons.filter(created_by=user2, label=label))
            otherShapePolygons = []
            for i in range(len(otherPolygons)):
                otherShapePolygons.append(ShapePolygon(json.loads(otherPolygons[i].points)))
            union = cascaded_union(myShapePolygons + otherShapePolygons)
            intersection = cascaded_union(
                [
                    ShapePolygon(json.loads(a.points)).intersection(ShapePolygon(json.loads(b.points)))
                    for a, b in list(itertools.product(myPolygons, otherPolygons))
                    # if a.name == b.name
                ]
            )
            if (union.area == 0 or intersection.area == 0):
                row.append(0)
            else:
                row.append(float("{0:.3f}".format(intersection.area / union.area)))
    table.append(row)
    return [{'user': table[0][i], 'accuracy': table[1][i]} for i in range(len(table[0]))]
示例#2
0
def imageLabelMatrix(users, imagePolygons):
    matrix = []
    for user1 in users:
        row = []
        myPolygons = [a for a in imagePolygons if (a.created_by_id == user1.id)]
        if (len(myPolygons) == 0):
            for user2 in users:
                if (user1.id != user2.id):
                    row.append(0)
                else:
                    row.append(1)
            matrix.append(row)
            continue
        myShapePolygons = []
        for i in range(len(myPolygons)):
            newshape = ShapePolygon(json.loads(myPolygons[i].points))
            if (not newshape.is_valid):
                newshape = newshape.exterior
            myShapePolygons.append(newshape)
        for user2 in users:
            if (user1.id != user2.id):
                otherPolygons = [a for a in imagePolygons if (a.created_by_id == user2.id)]
                if (len(otherPolygons) == 0):
                    row.append(0)
                    continue
                otherShapePolygons = []
                for i in range(len(otherPolygons)):
                    newshape = ShapePolygon(json.loads(otherPolygons[i].points))
                    if (not newshape.is_valid):
                        newshape = newshape.exterior
                    otherShapePolygons.append(newshape)
                union = cascaded_union(myShapePolygons + otherShapePolygons)
                intersection = cascaded_union(
                    [
                        ShapePolygon(json.loads(a.points)).intersection(ShapePolygon(json.loads(b.points)))
                        for a, b in list(itertools.product(myPolygons, otherPolygons))
                    ]
                )
                if (union.area == 0 or intersection.area == 0):
                    row.append(0)
                else:
                    row.append(intersection.area / union.area)
            else:
                row.append(1)
        matrix.append(row)
    # print(list(users))
    # for r in matrix:
    #     print(r)
    # print('\n')
    return matrix
 def convert_seg_map(self, labels, shrink_ratio, seg_size, ignore_difficult=True):
     # width, height = self.size
     # assert self.size[0] == seg_size[1]
     # assert self.size[1] == seg_size[0]
     height, width = seg_size[0], seg_size[1]
     seg_map = np.zeros((1, height, width), dtype=np.uint8)
     training_mask = np.ones((height, width), dtype=np.uint8)
     for poly, label in zip(self.polygons, labels):
         poly = poly.get_polygons()[0]
         poly = poly.reshape((-1, 2)).numpy()
         if ignore_difficult and label.item() == -1:
             cv2.fillPoly(training_mask, poly.astype(np.int32)[np.newaxis, :, :], 0)
             continue
         if poly.shape[0] < 4:
             continue
         p = ShapePolygon(poly)
         if p.length == 0:
             continue
         try:
             d = p.area * (1 - np.power(shrink_ratio, 2)) / p.length
         except:
             continue
         subj = [tuple(s) for s in poly]
         pco = pyclipper.PyclipperOffset()
         pco.AddPath(subj, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
         s = pco.Execute(-d)
         if s == []:
             cv2.fillPoly(training_mask, poly.astype(np.int32)[np.newaxis, :, :], 0)
             continue
         out = convert_2d_tuple(s[0])
         out = np.array(out).reshape(-1, 2)
         cv2.fillPoly(seg_map[0, :, :], [out.astype(np.int32)], 1)
     return seg_map, training_mask
 def rotate(self, angle, r_c, start_h, start_w):
     poly = self.polygons[0].numpy().reshape(-1, 2)
     poly[:, 0] += start_w
     poly[:, 1] += start_h
     polys = ShapePolygon(poly)
     r_polys = list(affinity.rotate(polys, angle, r_c).boundary.coords[:-1])
     p = []
     for r in r_polys:
         p += list(r)
     return Polygons([p], size=self.size, mode=self.mode)
 def rotate(self, angle, r_c, start_h, start_w):
     r_polys = []
     for poly in self.char_boxes:
         poly = poly.numpy()
         poly[0::2] += start_w
         poly[1::2] += start_h
         poly = ShapePolygon(np.array(poly).reshape(4, 2))
         r_poly = np.array(
             list(affinity.rotate(poly, angle, r_c).boundary.coords[:-1])
         ).reshape(-1, 8)
         r_polys.append(r_poly[0])
     return CharPolygons(
         r_polys,
         word=self.word,
         use_char_ann=self.use_char_ann,
         char_classes=self.char_classes,
         size=(r_c[0] * 2, r_c[1] * 2),
         mode=self.mode,
         char_num_classes=self.char_num_classes,
     )