• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

labelme和coco数据集

武飞扬头像
epic_Lin
帮助1

目录

1、labelme(Json to Dataset)

2、json转txt原理

2.1. 转txt(不是coco)

2.2. json转coco


1、labelme(Json to Dataset)

labelme标注完了之后生成.json文件,需要用脚本把它转换成coco或者其他txt格式的数据集来使用。本文总结了labelme产生的json文件转化成coco,txt和yolo三种方法。

下面是labelme安装教程,这里作者给出了json_to_dataset.py方法,但是不是转化成coco格式的,下面是原文章:

https://blog.csdn.net/qq_39435411/article/details/120776118?ops_request_misc=%7B%22request%5Fid%22%3A%22164103107516780366532500%22%2C%22scm%22%3A%2220140713.130102334..%22%7D&request_id=164103107516780366532500&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-120776118.first_rank_v2_pc_rank_v29&utm_term=labelme转coco&spm=1018.2226.3001.4187

2、json转txt原理

2.1. 转txt(不是coco)

目的:将labelme标注的json文件中的坐标和label信息提取到txt文件中

注意:labelme标注时使用“polygon”即画点标注方式,不是“rectangle”和“circle”标注方式,每个点坐标包括x和y,所以总共输出8个坐标值和1个label值

json格式

学新通

  1.  
    {
  2.  
    "version": "4.6.0",
  3.  
    "flags": {},
  4.  
    "shapes": [
  5.  
    {
  6.  
    "label": "B4",
  7.  
    "points": [
  8.  
    [
  9.  
    157.25806451612902,
  10.  
    639.516129032258
  11.  
    ]
  12.  
    ],
  13.  
    "group_id": null,
  14.  
    "shape_type": "point",
  15.  
    "flags": {}
  16.  
    },
  17.  
    {
  18.  
    "label": "B4",
  19.  
    "points": [
  20.  
    [
  21.  
    156.0483870967742,
  22.  
    689.1129032258065
  23.  
    ]
  24.  
    ],
  25.  
    "group_id": null,
  26.  
    "shape_type": "point",
  27.  
    "flags": {}
  28.  
    },
  29.  
    {
  30.  
    "label": "B4",
  31.  
    "points": [
  32.  
    [
  33.  
    278.6290322580645,
  34.  
    683.0645161290323
  35.  
    ]
  36.  
    ],
  37.  
    "group_id": null,
  38.  
    "shape_type": "point",
  39.  
    "flags": {}
  40.  
    },
  41.  
    {
  42.  
    "label": "B4",
  43.  
    "points": [
  44.  
    [
  45.  
    275.80645161290323,
  46.  
    634.6774193548387
  47.  
    ]
  48.  
    ],
  49.  
    "group_id": null,
  50.  
    "shape_type": "point",
  51.  
    "flags": {}
  52.  
    }
  53.  
    ],
  54.  
    "imagePath": "4496.jpg",
  55.  
    "imageData": "/9j/4AAQSkZJRgABAQAAAQABAAD",
  56.  
    "imageHeight": 1024,
  57.  
    "imageWidth": 1280
  58.  
    }
学新通

批量转换

dir_json为json文件夹

dir_txt为txt文件夹

同级目录下创建json2txt.py文件,文件内容复制如下

  1.  
    # coding:utf-8
  2.  
     
  3.  
    import os
  4.  
    import json
  5.  
    import numpy as np
  6.  
     
  7.  
    def json2txt(path_json,path_txt):
  8.  
    with open(path_json,'r', encoding='gb18030') as path_json:
  9.  
    jsonx=json.load(path_json)
  10.  
    with open(path_txt,'w ') as ftxt:
  11.  
    for shape in jsonx['shapes']: # shapes里面放的是标签的数据,比如四个点的类别和坐标
  12.  
    xy=np.array(shape['points'])# shapes里面的points是点的x,y坐标
  13.  
    label=str(shape['label']) # 类别信息
  14.  
    strxy = ''
  15.  
    for m,n in xy:
  16.  
    strxy =str(m) ',' str(n) ','
  17.  
    strxy =label
  18.  
    ftxt.writelines(strxy "\n")
  19.  
     
  20.  
    dir_json = 'json/'
  21.  
    dir_txt = 'txt/'
  22.  
    if not os.path.exists(dir_txt):
  23.  
    os.makedirs(dir_txt)
  24.  
    list_json = os.listdir(dir_json)
  25.  
    for cnt,json_name in enumerate(list_json):
  26.  
    print('cnt=%d,name=%s'%(cnt,json_name))
  27.  
    path_json = dir_json json_name
  28.  
    path_txt = dir_txt json_name.replace('.json','.txt')
  29.  
    # print(path_json, path_txt)
  30.  
    json2txt(path_json, path_txt)
学新通

txt文件输出示例

学新通

学新通

这个格式不是coco格式,coco格式如下:

学新通

2.2. json转coco

  1.  
    # 命令行执行: python labelme2coco.py --input_dir images --output_dir coco --labels labels.txt
  2.  
    # 输出文件夹必须为空文件夹
  3.  
     
  4.  
    import argparse
  5.  
    import collections
  6.  
    import datetime
  7.  
    import glob
  8.  
    import json
  9.  
    import os
  10.  
    import os.path as osp
  11.  
    import sys
  12.  
    import uuid
  13.  
    import imgviz
  14.  
    import numpy as np
  15.  
    import labelme
  16.  
    from sklearn.model_selection import train_test_split
  17.  
     
  18.  
    try:
  19.  
    import pycocotools.mask
  20.  
    except ImportError:
  21.  
    print("Please install pycocotools:\n\n pip install pycocotools\n")
  22.  
    sys.exit(1)
  23.  
     
  24.  
     
  25.  
    def to_coco(args,label_files,train):
  26.  
     
  27.  
    # 创建 总标签data
  28.  
    now = datetime.datetime.now()
  29.  
    data = dict(
  30.  
    info=dict(
  31.  
    description=None,
  32.  
    url=None,
  33.  
    version=None,
  34.  
    year=now.year,
  35.  
    contributor=None,
  36.  
    date_created=now.strftime("%Y-%m-%d %H:%M:%S.%f"),
  37.  
    ),
  38.  
    licenses=[dict(url=None, id=0, name=None,)],
  39.  
    images=[
  40.  
    # license, url, file_name, height, width, date_captured, id
  41.  
    ],
  42.  
    type="instances",
  43.  
    annotations=[
  44.  
    # segmentation, area, iscrowd, image_id, bbox, category_id, id
  45.  
    ],
  46.  
    categories=[
  47.  
    # supercategory, id, name
  48.  
    ],
  49.  
    )
  50.  
     
  51.  
    # 创建一个 {类名 : id} 的字典,并保存到 总标签data 字典中。
  52.  
    class_name_to_id = {}
  53.  
    for i, line in enumerate(open(args.labels).readlines()):
  54.  
    class_id = i - 1 # starts with -1
  55.  
    class_name = line.strip() # strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
  56.  
    if class_id == -1:
  57.  
    assert class_name == "__ignore__" # background:0, class1:1, ,,
  58.  
    continue
  59.  
    class_name_to_id[class_name] = class_id
  60.  
    data["categories"].append(
  61.  
    dict(supercategory=None, id=class_id, name=class_name,)
  62.  
    )
  63.  
     
  64.  
     
  65.  
    if train:
  66.  
    out_ann_file = osp.join(args.output_dir, "annotations","instances_train2017.json")
  67.  
    else:
  68.  
    out_ann_file = osp.join(args.output_dir, "annotations","instances_val2017.json")
  69.  
     
  70.  
     
  71.  
    for image_id, filename in enumerate(label_files):
  72.  
     
  73.  
    label_file = labelme.LabelFile(filename=filename)
  74.  
    base = osp.splitext(osp.basename(filename))[0] # 文件名不带后缀
  75.  
    if train:
  76.  
    out_img_file = osp.join(args.output_dir, "train2017", base ".jpg")
  77.  
    else:
  78.  
    out_img_file = osp.join(args.output_dir, "val2017", base ".jpg")
  79.  
     
  80.  
    print("| ",out_img_file)
  81.  
     
  82.  
    # ************************** 对图片的处理开始 *******************************************
  83.  
    # 将标签文件对应的图片进行保存到对应的 文件夹。train保存到 train2017/ test保存到 val2017/
  84.  
    img = labelme.utils.img_data_to_arr(label_file.imageData) # .json文件中包含图像,用函数提出来
  85.  
    imgviz.io.imsave(out_img_file, img) # 将图像保存到输出路径
  86.  
     
  87.  
    # ************************** 对图片的处理结束 *******************************************
  88.  
     
  89.  
    # ************************** 对标签的处理开始 *******************************************
  90.  
    data["images"].append(
  91.  
    dict(
  92.  
    license=0,
  93.  
    url=None,
  94.  
    file_name=osp.relpath(out_img_file, osp.dirname(out_ann_file)),
  95.  
    # out_img_file = "/coco/train2017/1.jpg"
  96.  
    # out_ann_file = "/coco/annotations/annotations_train2017.json"
  97.  
    # osp.dirname(out_ann_file) = "/coco/annotations"
  98.  
    # file_name = ..\train2017\1.jpg out_ann_file文件所在目录下 找 out_img_file 的相对路径
  99.  
    height=img.shape[0],
  100.  
    width=img.shape[1],
  101.  
    date_captured=None,
  102.  
    id=image_id,
  103.  
    )
  104.  
    )
  105.  
     
  106.  
    masks = {} # for area
  107.  
    segmentations = collections.defaultdict(list) # for segmentation
  108.  
    for shape in label_file.shapes:
  109.  
    points = shape["points"]
  110.  
    label = shape["label"]
  111.  
    group_id = shape.get("group_id")
  112.  
    shape_type = shape.get("shape_type", "polygon")
  113.  
    mask = labelme.utils.shape_to_mask(
  114.  
    img.shape[:2], points, shape_type
  115.  
    )
  116.  
     
  117.  
    if group_id is None:
  118.  
    group_id = uuid.uuid1()
  119.  
     
  120.  
    instance = (label, group_id)
  121.  
     
  122.  
    if instance in masks:
  123.  
    masks[instance] = masks[instance] | mask
  124.  
    else:
  125.  
    masks[instance] = mask
  126.  
     
  127.  
    if shape_type == "rectangle":
  128.  
    (x1, y1), (x2, y2) = points
  129.  
    x1, x2 = sorted([x1, x2])
  130.  
    y1, y2 = sorted([y1, y2])
  131.  
    points = [x1, y1, x2, y1, x2, y2, x1, y2]
  132.  
    else:
  133.  
    points = np.asarray(points).flatten().tolist()
  134.  
     
  135.  
    segmentations[instance].append(points)
  136.  
    segmentations = dict(segmentations)
  137.  
     
  138.  
    for instance, mask in masks.items():
  139.  
    cls_name, group_id = instance
  140.  
    if cls_name not in class_name_to_id:
  141.  
    continue
  142.  
    cls_id = class_name_to_id[cls_name]
  143.  
     
  144.  
    mask = np.asfortranarray(mask.astype(np.uint8))
  145.  
    mask = pycocotools.mask.encode(mask)
  146.  
    area = float(pycocotools.mask.area(mask))
  147.  
    bbox = pycocotools.mask.toBbox(mask).flatten().tolist()
  148.  
     
  149.  
    data["annotations"].append(
  150.  
    dict(
  151.  
    id=len(data["annotations"]),
  152.  
    image_id=image_id,
  153.  
    category_id=cls_id,
  154.  
    segmentation=segmentations[instance],
  155.  
    area=area,
  156.  
    bbox=bbox,
  157.  
    iscrowd=0,
  158.  
    )
  159.  
    )
  160.  
    # ************************** 对标签的处理结束 *******************************************
  161.  
     
  162.  
    # ************************** 可视化的处理开始 *******************************************
  163.  
    if not args.noviz:
  164.  
    labels, captions, masks = zip(
  165.  
    *[
  166.  
    (class_name_to_id[cnm], cnm, msk)
  167.  
    for (cnm, gid), msk in masks.items()
  168.  
    if cnm in class_name_to_id
  169.  
    ]
  170.  
    )
  171.  
    viz = imgviz.instances2rgb(
  172.  
    image=img,
  173.  
    labels=labels,
  174.  
    masks=masks,
  175.  
    captions=captions,
  176.  
    font_size=15,
  177.  
    line_width=2,
  178.  
    )
  179.  
    out_viz_file = osp.join(
  180.  
    args.output_dir, "visualization", base ".jpg"
  181.  
    )
  182.  
    imgviz.io.imsave(out_viz_file, viz)
  183.  
    # ************************** 可视化的处理结束 *******************************************
  184.  
     
  185.  
    with open(out_ann_file, "w") as f: # 将每个标签文件汇总成data后,保存总标签data文件
  186.  
    json.dump(data, f)
  187.  
     
  188.  
     
  189.  
    # 主程序执行
  190.  
    def main():
  191.  
    parser = argparse.ArgumentParser(
  192.  
    formatter_class=argparse.ArgumentDefaultsHelpFormatter
  193.  
    )
  194.  
    parser.add_argument("--input_dir", help="input annotated directory")
  195.  
    parser.add_argument("--output_dir", help="output dataset directory")
  196.  
    parser.add_argument("--labels", help="labels file", required=True)
  197.  
    parser.add_argument("--noviz", help="no visualization", action="store_true")
  198.  
    args = parser.parse_args()
  199.  
     
  200.  
    if osp.exists(args.output_dir):
  201.  
    print("Output directory already exists:", args.output_dir)
  202.  
    sys.exit(1)
  203.  
    os.makedirs(args.output_dir)
  204.  
    print("| Creating dataset dir:", args.output_dir)
  205.  
    if not args.noviz:
  206.  
    os.makedirs(osp.join(args.output_dir, "visualization"))
  207.  
     
  208.  
    # 创建保存的文件夹
  209.  
    if not os.path.exists(osp.join(args.output_dir, "annotations")):
  210.  
    os.makedirs(osp.join(args.output_dir, "annotations"))
  211.  
    if not os.path.exists(osp.join(args.output_dir, "train2017")):
  212.  
    os.makedirs(osp.join(args.output_dir, "train2017"))
  213.  
    if not os.path.exists(osp.join(args.output_dir, "val2017")):
  214.  
    os.makedirs(osp.join(args.output_dir, "val2017"))
  215.  
     
  216.  
    # 获取目录下所有的.jpg文件列表
  217.  
    feature_files = glob.glob(osp.join(args.input_dir, "*.jpg"))
  218.  
    print('| Image number: ', len(feature_files))
  219.  
     
  220.  
    # 获取目录下所有的joson文件列表
  221.  
    label_files = glob.glob(osp.join(args.input_dir, "*.json"))
  222.  
    print('| Json number: ', len(label_files))
  223.  
     
  224.  
     
  225.  
    # feature_files:待划分的样本特征集合 label_files:待划分的样本标签集合 test_size:测试集所占比例
  226.  
    # x_train:划分出的训练集特征 x_test:划分出的测试集特征 y_train:划分出的训练集标签 y_test:划分出的测试集标签
  227.  
    x_train, x_test, y_train, y_test = train_test_split(feature_files, label_files, test_size=0.3)
  228.  
    print("| Train number:", len(y_train), '\t Value number:', len(y_test))
  229.  
     
  230.  
    # 把训练集标签转化为COCO的格式,并将标签对应的图片保存到目录 /train2017/
  231.  
    print("—"*50)
  232.  
    print("| Train images:")
  233.  
    to_coco(args,y_train,train=True)
  234.  
     
  235.  
    # 把测试集标签转化为COCO的格式,并将标签对应的图片保存到目录 /val2017/
  236.  
    print("—"*50)
  237.  
    print("| Test images:")
  238.  
    to_coco(args,y_test,train=False)
  239.  
     
  240.  
     
  241.  
    if __name__ == "__main__":
  242.  
    print("—"*50)
  243.  
    main()
  244.  
    print("—"*50)
学新通

 CRT的labels.txt:

  1.  
    __ignore__
  2.  
    _background_
  3.  
    B1
  4.  
    B2
  5.  
    B3
  6.  
    B4
  7.  
    B5
  8.  
    BO
  9.  
    Bs
  10.  
    Bb
  11.  
    R1
  12.  
    R2
  13.  
    R3
  14.  
    R4
  15.  
    R5
  16.  
    RO
  17.  
    Rs
  18.  
    Rb
  19.  
    Drone
学新通

在labelme2coco.py文件的目录下,打开命令行执行:

python labelme2coco.py --input_dir data --output_dir coco --labels labels.txt

3、 json转yolo(附代码)

3.1. labelme分割格式(polygon)转yolo代码

  1.  
    # coding:utf-8
  2.  
     
  3.  
    import os
  4.  
    import cv2 as cv2
  5.  
    import json
  6.  
    import matplotlib.pyplot as plt
  7.  
     
  8.  
    """
  9.  
    1. One row per object
  10.  
    2. Each row is class x_center y_center width height format.
  11.  
    3. Box coordinates must be in normalized xywh format (from 0 - 1).
  12.  
    If your boxes are in pixels, divide x_center and width by image width, and y_center and height by image height.
  13.  
    4. Class numbers are zero-indexed (start from 0).
  14.  
    """
  15.  
     
  16.  
    # labelme 中预设的类别名和类别 id 的对应关系
  17.  
    label_idx_map = {"B1": 0, "B2": 1, "B3": 2, "B4": 3, "B5": 4, "BO": 5, "Bs": 6, "Bb": 7,"R1": 8, "R2": 9, "R3": 10, "R4": 11, "R5": 12, "RO": 13, "Rs": 14, "Rb": 15}
  18.  
    color_list = [[200, 0, 0], [0, 200, 0], [0, 0, 200], [200, 200, 0], [0, 200, 200], [200, 0, 200], [0, 0, 0],
  19.  
    [128, 128, 0],[200, 0, 0], [70, 20, 0], [100, 0, 200], [200, 200, 200], [0, 20, 200], [200, 0, 200], [0, 0, 0],
  20.  
    [128, 128, 0]]
  21.  
     
  22.  
     
  23.  
    def labelme_to_yolo(img_dir, json_dir, save_dir,save_jpg_dir):
  24.  
    name_list = os.listdir(json_dir) # 可以和图片是同一个文件夹
  25.  
    for name in name_list:
  26.  
    if name.endswith('.json'):
  27.  
     
  28.  
    save_path = os.path.join(save_dir, name.replace(".json", ".txt")) # 创建txt路径
  29.  
    im_path = os.path.join(img_dir, name.replace(".json", ".jpg")) # 拼装图片路径
  30.  
    json_path = os.path.join(json_dir, name) # 拼装json路径
  31.  
     
  32.  
    if(os.path.exists(im_path)):
  33.  
    im = cv2.imread(im_path) # 读取图片
  34.  
    name_jpg = os.path.join(save_jpg_dir, name.replace(".json", ".jpg")) # 保存图片的路径
  35.  
     
  36.  
    else:
  37.  
    im_path = os.path.join(img_dir, name.replace(".json", ".jpeg")) # 如果找不到jpg,就去找jpeg
  38.  
    im = cv2.imread(im_path) # 读取图片
  39.  
    name_jpg = os.path.join(save_jpg_dir, name.replace(".json", ".jpeg")) # 创建保存图片的路径
  40.  
    label_dict = json.load(open(json_path, 'r')) # 读取json文件
  41.  
    height = label_dict["imageHeight"]
  42.  
    width = label_dict["imageWidth"]
  43.  
    loc_info_list = label_dict["shapes"]
  44.  
    label_info_list = list()
  45.  
    for loc_info in loc_info_list:
  46.  
    obj_name = loc_info.get("label")
  47.  
    label_id = label_idx_map.get(obj_name)
  48.  
    # print(label_id)
  49.  
    loc = loc_info.get("points")
  50.  
    x0, y0 = loc[0] # 左上角点
  51.  
    x1, y1 = loc[1] # 左下角点
  52.  
    x2, y2 = loc[2] # 右下角点
  53.  
    x3, y3 = loc[3] # 右上角点
  54.  
    if x2<=x0 or y2<=y0:
  55.  
    print("error:",name)
  56.  
     
  57.  
    x_max = max(x0,x1,x2,x3)
  58.  
    x_min = min(x0,x1,x2,x3)
  59.  
    y_max = max(y0,y1,y2,y3)
  60.  
    y_min = min(y0,y1,y2,y3)
  61.  
     
  62.  
    cv2.rectangle(im, (int(x_max), int(y_max)), (int(x_min), int(y_min)), color_list[label_id], 2)
  63.  
    cv2.imwrite(name_jpg, im)
  64.  
     
  65.  
    x_center = (x_max x_min) / 2 / width
  66.  
    y_center = (y_max y_min ) / 2 / height
  67.  
    box_w = (abs(x_max - x_min)) / width
  68.  
    box_h = (abs(y_max - y_min)) / height
  69.  
    x0 = x0 / width
  70.  
    x1 = x1 / width
  71.  
    x2 = x2 / width
  72.  
    x3 = x3 / width
  73.  
     
  74.  
    y0 = y0 / height
  75.  
    y1 = y1 / height
  76.  
    y2 = y2 / height
  77.  
    y3 = y3 / height
  78.  
     
  79.  
    label_info_list.append([str(label_id), str(x_center), str(y_center), str(box_w), str(box_h),str(x0),str(y0),str(x1),str(y1),str(x2),str(y2),str(x3),str(y3)])
  80.  
     
  81.  
    with open(save_path, 'w') as f:
  82.  
    for label_info in label_info_list:
  83.  
    label_str = ' '.join(label_info)
  84.  
    f.write(label_str)
  85.  
    f.write('\n')
  86.  
     
  87.  
    # debug
  88.  
    # plt.figure(0)
  89.  
    # plt.imshow(im)
  90.  
    # plt.show()
  91.  
    # print("xxx")
  92.  
     
  93.  
     
  94.  
    if __name__ == "__main__":
  95.  
    # 图像文件夹
  96.  
    image_dir = "4_labeled/4"
  97.  
    # labelme 的标注结果
  98.  
    json_dir = "4_labeled/4"
  99.  
    # yolo 使用的 txt 结果
  100.  
    save_dir = "output_txt"
  101.  
    # yolo 使用的 jpg 结果
  102.  
    save_jpg_dir = "output_jpg"
  103.  
    labelme_to_yolo(image_dir, json_dir, save_dir,save_jpg_dir)
  104.  
     
学新通

3.2. labelme分割(polygon)和点(points)两种模式区别

对于polygon格式标出的json文件(标签信息主要在"shapes"中),shapes对应一个列表,其中每一个目标是一个字典(比如两个装甲板对应两个字典)。

字典中有labels points group_id shape_type flags

如"labels" : "R3"

points对应一个列表,有这个目标所有标注点的信息,每个标注点以[x,y]的形式保存

"points" : [ [x1, y1], [x2, y2], [x3, y3], [x4, y4] ]

"shape_type" : "polygon"

  1.  
    {
  2.  
    "version": "4.6.0",
  3.  
    "flags": {},
  4.  
    "shapes": [
  5.  
    {
  6.  
    "label": "R3",
  7.  
    "points": [
  8.  
    [
  9.  
    527.3333333333334,
  10.  
    732.2222222222222
  11.  
    ],
  12.  
    [
  13.  
    514.0,
  14.  
    814.4444444444445
  15.  
    ],
  16.  
    [
  17.  
    716.8888888888889,
  18.  
    817.3333333333334
  19.  
    ],
  20.  
    [
  21.  
    725.7777777777778,
  22.  
    725.1111111111111
  23.  
    ]
  24.  
    ],
  25.  
    "group_id": null,
  26.  
    "shape_type": "polygon",
  27.  
    "flags": {}
  28.  
    },
  29.  
    {
  30.  
    "label": "B5",
  31.  
    "points": [
  32.  
    [
  33.  
    300.0,
  34.  
    727.7777777777778
  35.  
    ],
  36.  
    [
  37.  
    294.22222222222223,
  38.  
    802.6666666666666
  39.  
    ],
  40.  
    [
  41.  
    470.8888888888889,
  42.  
    800.2222222222222
  43.  
    ],
  44.  
    [
  45.  
    471.55555555555554,
  46.  
    714.6666666666666
  47.  
    ]
  48.  
    ],
  49.  
    "group_id": null,
  50.  
    "shape_type": "polygon",
  51.  
    "flags": {}
  52.  
    }
  53.  
    ],
  54.  
    "imagePath": "Pic_2020_10_24_173228_28.jpeg",
  55.  
    "imageData": "/9j/4AAQSkZJRgABAQAAAQABAAD",
  56.  
    "imageHeight": 1024,
  57.  
    "imageWidth": 1280
  58.  
    }
学新通

point格式标注:

shapes中格式和上面一样,points中只存放一个点,几个点shapes中就有几个字典

labels points group_id shape_type flags

"shape_type" : point

  1.  
    {
  2.  
    "version": "4.6.0",
  3.  
    "flags": {},
  4.  
    "shapes": [
  5.  
    {
  6.  
    "label": "R4",
  7.  
    "points": [
  8.  
    [
  9.  
    637.8737541528238,
  10.  
    644.8504983388704
  11.  
    ]
  12.  
    ],
  13.  
    "group_id": null,
  14.  
    "shape_type": "point",
  15.  
    "flags": {}
  16.  
    },
  17.  
    {
  18.  
    "label": "R4",
  19.  
    "points": [
  20.  
    [
  21.  
    641.1960132890365,
  22.  
    688.704318936877
  23.  
    ]
  24.  
    ],
  25.  
    "group_id": null,
  26.  
    "shape_type": "point",
  27.  
    "flags": {}
  28.  
    },
  29.  
    {
  30.  
    "label": "R4",
  31.  
    "points": [
  32.  
    [
  33.  
    740.531561461794,
  34.  
    687.3754152823919
  35.  
    ]
  36.  
    ],
  37.  
    "group_id": null,
  38.  
    "shape_type": "point",
  39.  
    "flags": {}
  40.  
    },
  41.  
    {
  42.  
    "label": "R4",
  43.  
    "points": [
  44.  
    [
  45.  
    737.8737541528238,
  46.  
    642.8571428571428
  47.  
    ]
  48.  
    ],
  49.  
    "group_id": null,
  50.  
    "shape_type": "point",
  51.  
    "flags": {}
  52.  
    }
  53.  
    ],
  54.  
    "imagePath": "1779.jpg",
  55.  
    "imageData": "/9j/4AAQSkZJRgABAQAAAQABAAD/Ha",
  56.  
    "imageHeight": 1024,
  57.  
    "imageWidth": 1280
  58.  
    }
学新通

3.3. labelme_point_2yolo代码

  1.  
    # coding:utf-8
  2.  
    # labeme中point格式标注,转化成yolo格式
  3.  
    import os
  4.  
    import cv2 as cv2
  5.  
    import json
  6.  
    import matplotlib.pyplot as plt
  7.  
     
  8.  
    """
  9.  
    1. One row per object
  10.  
    2. Each row is class x_center y_center width height format.
  11.  
    3. Box coordinates must be in normalized xywh format (from 0 - 1).
  12.  
    If your boxes are in pixels, divide x_center and width by image width, and y_center and height by image height.
  13.  
    4. Class numbers are zero-indexed (start from 0).
  14.  
    """
  15.  
     
  16.  
    # labelme 中预设的类别名和类别 id 的对应关系
  17.  
    # 在这个point模式转换的代码中因为有RS,BS,Rs,Bs哨兵大小写标错,所以对应字典中大小写都有
  18.  
    label_idx_map = {"B1": 0, "B2": 1, "B3": 2, "B4": 3, "B5": 4, "BO": 5, "Bs": 6, "Bb": 7,"R1": 8, "R2": 9, "R3": 10, "R4": 11, "R5": 12, "RO": 13, "Rs": 14, "Rb": 15, "RS" : 14, "BS" : 6}
  19.  
    color_list = [[200, 0, 0], [0, 200, 0], [0, 0, 200], [200, 200, 0], [0, 200, 200], [200, 0, 200], [0, 0, 0],
  20.  
    [128, 128, 0],[200, 0, 0], [70, 20, 0], [100, 0, 200], [200, 200, 200], [0, 20, 200], [200, 0, 200], [0, 0, 0],
  21.  
    [128, 128, 0]]
  22.  
     
  23.  
     
  24.  
     
  25.  
    def labelme_to_yolo(img_dir, json_dir, save_dir,save_jpg_dir):
  26.  
    name_list = os.listdir(json_dir) # 可以和图片是同一个文件夹
  27.  
    count = 0
  28.  
    for name in name_list:
  29.  
    if name.endswith('.json'):
  30.  
     
  31.  
    save_path = os.path.join(save_dir, name.replace(".json", ".txt")) # 创建txt路径
  32.  
    im_path = os.path.join(img_dir, name.replace(".json", ".jpg")) # 拼装图片路径
  33.  
    json_path = os.path.join(json_dir, name) # 拼装json路径
  34.  
     
  35.  
    if(os.path.exists(im_path)):
  36.  
    im = cv2.imread(im_path) # 读取图片
  37.  
    name_jpg = os.path.join(save_jpg_dir, name.replace(".json", ".jpg")) # 保存图片的路径
  38.  
     
  39.  
    else:
  40.  
    im_path = os.path.join(img_dir, name.replace(".json", ".jpeg")) # 如果找不到jpg,就去找jpeg
  41.  
    im = cv2.imread(im_path) # 读取图片
  42.  
    name_jpg = os.path.join(save_jpg_dir, name.replace(".json", ".jpeg")) # 创建保存图片的路径
  43.  
    label_dict = json.load(open(json_path, 'r')) # 读取json文件
  44.  
    height = label_dict["imageHeight"]
  45.  
    width = label_dict["imageWidth"]
  46.  
    loc_info_list = label_dict["shapes"] # shapes列表中point格式一个点是一个字典
  47.  
    label_info_list = list()
  48.  
    count_point = 0
  49.  
    point = []
  50.  
     
  51.  
    for loc_info in loc_info_list: # 取出每个点
  52.  
    count_point = 1
  53.  
    obj_name = loc_info.get("label") # obj_name = 标签
  54.  
    label_id = label_idx_map.get(obj_name)# 检索标签对应的数字
  55.  
    # print(label_id)
  56.  
    loc = loc_info.get("points")
  57.  
    x0, y0 = loc[0] # 取出一个点
  58.  
    point.append([x0,y0])
  59.  
    # x1, y1 = loc[1] # 左下角点
  60.  
    # x2, y2 = loc[2] # 右下角点
  61.  
    # x3, y3 = loc[3] # 右上角点
  62.  
    # if x2<=x0 or y2<=y0:
  63.  
    # print("error:",name)
  64.  
    if count_point == 4:
  65.  
    count_point = 0
  66.  
    x_max = max(point[0][0],point[1][0],point[2][0],point[3][0])
  67.  
    x_min = min(point[0][0],point[1][0],point[2][0],point[3][0])
  68.  
    y_max = max(point[0][1],point[1][1],point[2][1],point[3][1])
  69.  
    y_min = min(point[0][1],point[1][1],point[2][1],point[3][1])
  70.  
     
  71.  
    # cv2.rectangle(im, (int(x_max), int(y_max)), (int(x_min), int(y_min)), color_list[label_id], 2)
  72.  
    cv2.imwrite(name_jpg, im)
  73.  
     
  74.  
    x_center = (x_max x_min) / 2 / width
  75.  
    y_center = (y_max y_min ) / 2 / height
  76.  
    box_w = (abs(x_max - x_min)) / width
  77.  
    box_h = (abs(y_max - y_min)) / height
  78.  
    x0 = point[0][0] / width
  79.  
    x1 = point[1][0] / width
  80.  
    x2 = point[2][0] / width
  81.  
    x3 = point[3][0] / width
  82.  
     
  83.  
    y0 = point[0][1] / height
  84.  
    y1 = point[1][1] / height
  85.  
    y2 = point[2][1] / height
  86.  
    y3 = point[3][1] / height
  87.  
     
  88.  
    label_info_list.append([str(label_id), str(x_center), str(y_center), str(box_w), str(box_h),str(x0),str(y0),str(x1),str(y1),str(x2),str(y2),str(x3),str(y3)])
  89.  
    print(save_path)
  90.  
    count = 1
  91.  
    print(count)
  92.  
    with open(save_path, 'w') as f: # 每四次就进来写一下,如果两个标签,第二个四次重写文件
  93.  
    for label_info in label_info_list:
  94.  
    label_str = ' '.join(label_info)
  95.  
    f.write(label_str)
  96.  
    f.write('\n')
  97.  
     
  98.  
    # debug
  99.  
    # plt.figure(0)
  100.  
    # plt.imshow(im)
  101.  
    # plt.show()
  102.  
    # print("xxx")
  103.  
     
  104.  
     
  105.  
    if __name__ == "__main__":
  106.  
    # 图像文件夹
  107.  
    image_dir = "1 (1)"
  108.  
    # labelme 的标注结果
  109.  
    json_dir = "1 (1)"
  110.  
    # yolo 使用的 txt 结果
  111.  
    save_dir = "1_output_txt"
  112.  
    # yolo 使用的 jpg 结果
  113.  
    save_jpg_dir = "1_output_jpg"
  114.  
    labelme_to_yolo(image_dir, json_dir, save_dir,save_jpg_dir)
  115.  
     
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbfckf
系列文章
更多 icon
同类精品
更多 icon
继续加载