labelme【ValueError: x1 must be greater than or equal to x0】】

问题描述

之前用 labelme 标注了一些图片,现在需要用 labelme2coco.py 将标注文件转换为 coco 格式的数据集,在执行的时候遇到下面的问题:

(labelme) PS D:\scripts> python .\labelme2coco.py --labels ..\datasets\version280\labels.txt
 ..\datasets\version280\labels\ .\data\coco
Creating dataset: .\data\coco
Generating dataset from: ..\datasets\version280\labels\00000.x0y0r0.024645.json
Traceback (most recent call last):
  File "D:\scripts\labelme2coco.py", line 203, in <module>
    main()
  File "D:\scripts\labelme2coco.py", line 121, in main
    mask = labelme.utils.shape_to_mask(img.shape[:2], points, shape_type)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\miniconda3\envs\labelme\Lib\site-packages\labelme\utils\shape.py", line 33, in shape_to_mask
    draw.rectangle(xy, outline=1, fill=1)
  File "C:\Users\miniconda3\envs\labelme\Lib\site-packages\PIL\ImageDraw.py", line 316, in rectangle
    self.draw.draw_rectangle(xy, fill, 1)
ValueError: x1 must be greater than or equal to x0

问题原因

查看出问题的 json 文件可知,问题出在矩形框的坐标上。

原因是:labelme 的这个脚本默认标注员在标注矩形框的时候,是先点击框的左上角,再点击框的右下角,此时满足:

point[0][0] <= point[1][0] # 即 x0 <= x1
point[1][0] <= point[1][1] # 即 y0 <= y1

解决方式

在执行第 121 行前,对有问题的坐标进行调整。例如:
将(在文件的 118 行到 121 行)

for shape in label_file.shapes:
    points = shape["points"]
    label = shape["label"]
    group_id = shape.get("group_id")
    shape_type = shape.get("shape_type", "polygon")
    mask = labelme.utils.shape_to_mask(img.shape[:2], points, shape_type)

改成:

for shape in label_file.shapes:
    points = shape["points"]
    if points[0][0] > points[1][0]:
        points[0][0], points[1][0] = points[1][0], points[0][0]
    if points[0][1] > points[1][1]:
        points[0][1], points[1][1] = points[1][1], points[0][1]
        
    label = shape["label"]
    group_id = shape.get("group_id")
    shape_type = shape.get("shape_type", "polygon")
    mask = labelme.utils.shape_to_mask(img.shape[:2], points, shape_type)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值