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=JOIN.LEFT_OUTER).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
def save(self, vo: LabelVO): label = LabelEntity.create( name=vo.name.title(), color=vo.color, dataset=vo.dataset ) vo.id = label.get_id() return vo
def fetch_all(self, ds_id): cursor = LabelEntity.select().where(LabelEntity.dataset == ds_id).dicts().execute() result = [] for ds in list(cursor): vo = LabelVO() result.append(vo) for k, v in ds.items(): setattr(vo, k, v) return result
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
def find_by_name(self, ds_id, label_name): query = ( LabelEntity .select() .where((LabelEntity.dataset == ds_id) & (LabelEntity.name == label_name))) # print(query) cursor = query.dicts().execute() result = list(cursor) vo = LabelVO() if len(result) > 0: row = result[0] for k, v in row.items(): setattr(vo, k, v) return vo return None
def __init__(self, *args, **kwargs): super(EditableItem, self).__init__(*args, **kwargs) self.setZValue(10) self.setAcceptHoverEvents(True) self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable, True) self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True) self.setFlag(QtWidgets.QGraphicsItem.ItemSendsGeometryChanges, True) self.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor)) self.setOpacity(0.5) self.signals = EditableItemSignals() self._labels_dao = LabelDao() self._label = LabelVO() self._tag = None self._shape_type = None app = QApplication.instance() color = app.palette().color(QPalette.Highlight) self._pen_color = color self._pen_width = 2 self._brush_color = color self.setPen(QtGui.QPen(self._pen_color, self._pen_width))
def result(self) -> LabelVO: vo = LabelVO() vo.name = self.nameLineEdit.text() vo.color = self.colorLineEdit.text() return vo