コード例 #1
0
ファイル: annota_dao.py プロジェクト: appstarbd/CvStudio
    def fetch_all(self, entity_id: int):
        anns = AnnotationEntity.alias()
        lbl = LabelEntity.alias()
        query = (
            anns.select(
                anns.id.alias("annot_id"),
                anns.entry.alias("annot_entry"),
                anns.kind.alias("annot_kind"),
                anns.points.alias("annot_points"),
                lbl.id.alias("label_id"),
                lbl.name.alias("label_name"),
                lbl.color.alias("label_color")
            )
                .join(lbl, on=(anns.label == lbl.id), join_type="LEFT")
                .where(anns.entry == entity_id)
        )
        cursor = query.dicts().execute()
        result = []
        for row in cursor:
            ann_vo = AnnotaVO()
            ann_vo.id = row["annot_id"]
            ann_vo.entry = row["annot_entry"]
            ann_vo.kind = row["annot_kind"]
            ann_vo.points = row["annot_points"]
            ann_vo.label = None
            if row["label_id"]:
                label = LabelVO()
                label.id = row["label_id"]
                label.name = row["label_name"]
                label.color = row["label_color"]
                ann_vo.label = label
            result.append(ann_vo)

        return result
コード例 #2
0
 def do_work():
     annotations = []
     for xml_file in files:
         tree = ET.parse(xml_file)
         root = tree.getroot()
         objects = root.findall('object')
         image_path = root.find('path').text
         image_vo = self._ds_dao.find_by_path(
             dataset_vo.id, image_path)
         if image_vo:
             for roi in objects:
                 label_name = roi.find('name').text
                 label_name = label_name.title()
                 label_vo = self._labels_dao.find_by_name(
                     dataset_vo.id, label_name)
                 if label_vo is None:
                     label_vo = LabelVO()
                     label_vo.name = label_name
                     label_vo.dataset = dataset_vo.id
                     label_vo.color = colors[random.randint(
                         0, len(colors))]
                     label_vo = self._labels_dao.save(
                         label_vo)
                 box = roi.find("bndbox")
                 if box:
                     x1 = int(box.find('xmin').text)
                     y1 = int(box.find('ymin').text)
                     x2 = int(box.find('xmax').text)
                     y2 = int(box.find('ymax').text)
                     box = AnnotaVO()
                     box.label = label_vo.id
                     box.entry = image_vo.id
                     box.kind = "box"
                     box.points = ",".join(
                         map(str, [x1, y1, x2, y2]))
                     annotations.append(box)
     if len(annotations) > 0:
         print(annotations)
         self._annot_dao.save(dataset_vo.id, annotations)
     return annotations, None
コード例 #3
0
    def save_annotations(self, done_work_callback):
        scene: QGraphicsScene = self.image_viewer.scene()
        annotations = []
        for item in scene.items():
            image_rect: QRectF = self.image_viewer.pixmap.sceneBoundingRect()
            image_offset = QPointF(image_rect.width() / 2,
                                   image_rect.height() / 2)
            if isinstance(item, EditableItem):
                a = AnnotaVO()
                a.label = item.label.id if item.label else None
                a.entry = self.tag.id
                a.kind = item.shape_type
                a.points = item.coordinates(image_offset)
                annotations.append(a)

        @work_exception
        def do_work():
            self._ann_dao.save(self.tag.id, annotations)
            return None, None

        worker = Worker(do_work)
        worker.signals.result.connect(done_work_callback)
        self._thread_pool.start(worker)
コード例 #4
0
ファイル: image_viewer.py プロジェクト: appstarbd/CvStudio
    def save_annotations(self):
        scene: QGraphicsScene = self.viewer.scene()
        self._annot_dao.delete(self.source.id)
        annot_list = []
        for item in scene.items():
            img_bbox: QRectF = self.viewer.pixmap.sceneBoundingRect()
            offset = QPointF(img_bbox.width() / 2, img_bbox.height() / 2)
            if isinstance(item, EditableBox):
                item_box: QRectF = item.sceneBoundingRect()
                x1 = math.floor(item_box.topLeft().x() + offset.x())
                y1 = math.floor(item_box.topRight().y() + offset.y())
                x2 = math.floor(item_box.bottomRight().x() + offset.x())
                y2 = math.floor(item_box.bottomRight().y() + offset.y())
                box = AnnotaVO()
                box.label = item.label.id if item.label else None
                box.entry = self.source.id
                box.kind = "box"
                box.points = ",".join(map(str, [x1, y1, x2, y2]))
                annot_list.append(box)
            elif isinstance(item, EditablePolygon):
                points = [[
                    math.floor(pt.x() + offset.x()),
                    math.floor(pt.y() + offset.y())
                ] for pt in item.points]
                points = np.asarray(points).flatten().tolist()
                poly = AnnotaVO()
                poly.label = item.label.id if item.label else None
                poly.entry = self.source.id
                poly.kind = "polygon"
                poly.points = ",".join(map(str, points))
                annot_list.append(poly)

        self._annot_dao.save(annot_list)