标注 Label 本篇将简要介绍几个用于Yolo标注的工具:Labelme、Labelimg、Label Studio 。 后续将介绍基于SAM2的半自动标注 以及全自动标注 的实现。
Labelme labelme的Github主页
安装Labelme 可以参考官方Guide 最简单使用pip安装(使用了PyQT5)
或者下载单独exe文件下载地址
使用Labelme 以pip安装的为例,在命令行中输入
就可以打开labelme,显示如下图 选择打开目录
,导入需要进行标注的图片所在文件夹。 根据需要,选择编辑-创建图形
以矩形为例,在图片上框选点选矩形的第一个点,然后在对顶角再点一下,就确定了一个矩形 在弹窗的框中给目标起一个名称(比如package) 然后对下一张图片进行标注。 此时会询问是否对标注进行保存,选择是-选择路径-保存为json文件。
将所有图片标注后,将得到全部的标注文件(JSON格式) 然后将所有json文件转为Yolo使用的txt格式标注文件(本文稍后介绍) 更详细的教程请参见:官方教程
Labelimg labelimg的Github主页
NOTE LabelImg is now part of the Label Studio community.
安装Labelimg 同样使用了PyQT5
更多安装请参考Github主页
使用Labelimg 首先需要建立classes.txt
文件,存储需要标注的类别(比如package) 其余与Labelme类似。
Label Studio Label Studio官网 Label StudioGithub项目页
安装Label Studio 使用pip进行安装,或在Github项目页查找其他方式安装
1 pip install label-studio
使用Label Studio 在命令行中输入:
即可以打开label studio sign up进行注册或者在有账号的情况下直接登陆 登陆后可以新建项目 输入项目名称,导入数据以及labeling setup 在labeling setup中,选择合适的标注方法 比如第一行第三个是物体框型检测,我们以此为例: 将labels中的airplane和car删除,新add一个package,然后save,我们就可以去导入的图片中进行框选了。 将图中所有物体进行框选后,标注就算完成了。 标注完成后可以选择export
NOTE 在项目的Setting-Danger Zone里面可以删除项目
这几个Labeling的项目都差不多,而且也已经开始支持各种AI。 但是相比较而已,我个人觉得对YOLO而言不是最好用的。 因为生成的JSON文件还需要转为txt。 后面将介绍我个人觉得更好用的工具。
JSON转TXT代码 下面是用于将JSON的标注文件转为TXT的python代码,供参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 import osimport jsondef convert_labelme_to_yolo (root_dir, classes ): for dirpath, _, filenames in os.walk(root_dir): for filename in filenames: if not filename.lower().endswith('.json' ): continue json_path = os.path.join(dirpath, filename) try : with open (json_path, 'r' ) as f: data = json.load(f) except json.JSONDecodeError as e: print (f"JSON解析失败 {json_path} : {str (e)} " ) continue try : img_w = data['imageWidth' ] img_h = data['imageHeight' ] shapes = data['shapes' ] except KeyError: print (f"缺少必要字段 {json_path} " ) continue yolo_lines = [] for shape in shapes: label = shape.get('label' , '' ) if label not in classes: continue points = shape.get('points' , []) shape_type = shape.get('shape_type' , '' ) if shape_type == 'rectangle' and len (points) == 2 : (x1, y1), (x2, y2) = points elif shape_type == 'polygon' and len (points) >= 2 : xs = [point[0 ] for point in points] ys = [point[1 ] for point in points] x1, x2 = min (xs), max (xs) y1, y2 = min (ys), max (ys) else : continue try : x_center = ((x1 + x2) / 2 ) / img_w y_center = ((y1 + y2) / 2 ) / img_h width = abs (x2 - x1) / img_w height = abs (y2 - y1) / img_h if not (0 <= x_center <= 1 and 0 <= y_center <= 1 and width > 0 and height > 0 ): continue yolo_lines.append(f"{classes.index(label)} {x_center:.6 f} {y_center:.6 f} {width:.6 f} {height:.6 f} " ) except (IndexError, ValueError) as e: print (f"坐标计算错误 {json_path} : {str (e)} " ) continue if yolo_lines: txt_path = os.path.join(dirpath, filename.replace('.json' , '.txt' )) with open (txt_path, 'w' ) as f: f.write('\n' .join(yolo_lines)) else : print (f"无有效标注 {json_path} " )if __name__ == '__main__' : ROOT_DIR = r"path\to\json" CLASSES = ["yourclass" ] convert_labelme_to_yolo(ROOT_DIR, CLASSES)