【labelme】改造labelme

这篇博客介绍了如何修改Labelme的源代码,以实现删除形状时不显示确认对话框,自动保存标签信息,不存储imageData以减小JSON大小,并按资源管理器顺序显示图像。通过编辑`app.py`和`label_file.py`文件,用户可以自定义Labelme的行为以提高标注效率。
摘要由CSDN通过智能技术生成
  1. 如何查看自己labelme安装的位置
    例如通过pip3.8安装
pip3.8 install labelme

可以通过查看pip3.8版本寻找对应的python安装位置

pip3.8 -V

然后就可以在对应版本的python下修改相应的文件

  1. delete 删除标签时,不再弹出对话框

    找到./python/site-packages/labelme/app.py

def deleteSelectedShape(self):
        self.remLabels(self.canvas.deleteSelected())
        self.setDirty()
        if self.noShapes():
            for action in self.actions.onShapesPresent:
                action.setEnabled(False)
        #yes, no = QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No
        #msg = self.tr(
        #    "You are about to permanently delete {} polygons, "
        #    "proceed anyway?"
        #).format(len(self.canvas.selectedShapes))
        #if yes == QtWidgets.QMessageBox.warning(
        #    self, self.tr("Attention"), msg, yes | no, yes
        #):
            #self.remLabels(self.canvas.deleteSelected())
            #self.setDirty()
            #if self.noShapes():
            #    for action in self.actions.onShapesPresent:
            #        action.setEnabled(False)
  1. 选择其他图像,自动保存当前标签信息,不再弹出对话框
    找到./python/site-packages/labelme/app.py
def mayContinue(self):
        if not self.dirty:
            return True
        self.saveFile()
        return True
        #mb = QtWidgets.QMessageBox
        #msg = self.tr('Save annotations to "{}" before closing?').format(
        #    self.filename
        #)
        #answer = mb.question(
        #    self,
        #    self.tr("Save annotations?"),
        #    msg,
        #    mb.Save | mb.Discard | mb.Cancel,
        #    mb.Save,
        #)
        #if answer == mb.Discard:
        #    return True
        #elif answer == mb.Save:
        #    self.saveFile()
        #    return True
        #else:  # answer == mb.Cancel
        #    return False
  1. imageData 不再存储 image的信息,减少json存储大小
    找到./python/site-packages/labelme/label_file.py
    imageData=imageData 改为 imageData=None
def save(
        self,
        filename,
        shapes,
        imagePath,
        imageHeight,
        imageWidth,
        imageData=None,
        otherData=None,
        flags=None,
    ):
        if imageData is not None:
            imageData = base64.b64encode(imageData).decode("utf-8")
            imageHeight, imageWidth = self._check_image_height_and_width(
                imageData, imageHeight, imageWidth
            )
        if otherData is None:
            otherData = {}
        if flags is None:
            flags = {}
        data = dict(
            version=__version__,
            flags=flags,
            shapes=shapes,
            imagePath=imagePath,
            # imageData=imageData,
            imageData=None,
            imageHeight=imageHeight,
            imageWidth=imageWidth,
        )
        for key, value in otherData.items():
            assert key not in data
            data[key] = value
        try:
            with open(filename, "w") as f:
                json.dump(data, f, ensure_ascii=False, indent=2)
            self.filename = filename
        except Exception as e:
            raise LabelFileError(e)
  1. image list 列表按照资源管理器进行排序,顺序显示图像
    找到./python/site-packages/labelme/app.py
    首先 import natsort,如果没有安装,可以pip install natsort安装
def scanAllImages(self, folderPath):
        extensions = [
            ".%s" % fmt.data().decode().lower()
            for fmt in QtGui.QImageReader.supportedImageFormats()
        ]

        images = []
        for root, dirs, files in os.walk(folderPath):
           for file in files:
               if file.lower().endswith(tuple(extensions)):
                   relativePath = osp.join(root, file)
                   images.append(relativePath)
        # images.sort(key=lambda x: x.lower())
        images = natsort.natsorted(images)
        return images
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二十四桥下一句

您的鼓励是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值