ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.88k stars 16.38k forks source link

How to transfer *.json(labelme labeled segment) to yolo *.txt (the new segment) #10161

Closed Snailgoo closed 1 year ago

Snailgoo commented 2 years ago

Search before asking

Question

I noticed that the instance segmentation module has been added, but how to convert the instance segmentation dataset labeled with labelme into a txt format that YOLO can train? Are there any related scripts or instructions?

thank you!

Additional

No response

github-actions[bot] commented 2 years ago

👋 Hello @Snailgoo, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email support@ultralytics.com.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

YOLOv5 CI

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 2 years ago

@Snailgoo YOLOv5 segmentation label examples can be seen in coco128-seg.yaml. They are the same as detection labels, using txt files with one object per line but instead of class, xywh they are class xy1, xy2, xy3... etc.

WongVi commented 1 year ago

@glenn-jocher could you please share the file so that I can convert labelme generate data to segmentation format? @Snailgoo have you solve the problem of segmentation training? could you please share idea ?

sdimantsd commented 1 year ago

@WongVi @Snailgoo

This is me code doing this:


import os
import json

js_path = 'train.json'
out_labels_path = 'labels/'
main_dir = '[path_to_root_dataset_dir]'

all_images = list()
js_path = os.path.join(main_dir, js_path)
out_labels_path = os.path.join(main_dir, out_labels_path)

if not os.path.exists(out_labels_path):
    os.makedirs(out_labels_path)

js = json.load(open(js_path, 'r'))

annotations_by_image_id = {}
annotations_by_id = {}
for ann in js['annotations']:
    ann_id = ann['id']
    assert (ann_id not in annotations_by_id), 'error ,annotation id already in annotations_by_id'
    annotations_by_id[ann_id] = ann
    image_id = ann['image_id']
    if image_id not in annotations_by_image_id:
        annotations_by_image_id[image_id] = list()
    annotations_by_image_id[image_id].append(ann_id)

for im in js['images']:
    im_id = im['id']
    fname = os.path.basename(im['file_name']).split('.')[0]
    all_images.append(im['file_name'])
    w = im['width']
    h = im['height']
    label_name = fname + '.txt'
    with open(os.path.join(out_labels_path, label_name), 'w') as out_labels:
        if im_id in annotations_by_image_id:
            for ann_id in annotations_by_image_id[im_id]:
                ann = annotations_by_id[ann_id]
                category_id = ann['category_id'] - 1  # coco category id start with 1, and yolo category id start with 0
                out_seg = []
                for seg in ann['segmentation']:
                    for i in range(int(len(seg)/2)):
                        x = seg[i*2] / w
                        y = seg[i*2 + 1] / h
                        out_seg.append(x)
                        out_seg.append(y)
                line = str(category_id)
                for coord in out_seg:
                    line += ' ' + str(coord)
                out_labels.write(line)
                out_labels.write('\n')

with open(os.path.join(main_dir, js_path.split('.')[0] + '.txt'), 'w') as f:
    for im_name in all_images:
        f.write(im_name)
        f.write('\n')
JNaranjo-Alcazar commented 1 year ago

Hi everybody! The output of labelme is a .json named with the same name as the image. Find attached an example of what I got. @sdimantsd I cannot figure out how to adapt my configuration to your script,... Any ideas?

version": "5.0.2",
  "flags": {},
  "shapes": [
    {
      "label": "person",
      "points": [
        [
          250.8142292490119,
          107.33596837944665
        ],
        [
          229.8142292490119,
          119.33596837944665
        ],
        [
          221.8142292490119,
          135.33596837944665
        ],
        [
          223.8142292490119,
          148.33596837944665
        ],
        [
          217.8142292490119,
          161.33596837944665
        ],
        [
          202.8142292490119,
          168.33596837944665
        ],
        [
          192.8142292490119,
          200.33596837944665
        ],
        [
          194.8142292490119,
          222.33596837944665
        ],
        [
          199.8142292490119,
          227.33596837944665
        ],
        [
          191.8142292490119,
          234.33596837944665
        ],
        [
          197.8142292490119,
          264.3359683794467
        ],
        [
          213.8142292490119,
          295.3359683794467
        ],
        [
          214.8142292490119,
          320.3359683794467
        ],
        [
          221.8142292490119,
          327.3359683794467
        ],
        [
          235.8142292490119,
          326.3359683794467
        ],
        [
          240.8142292490119,
          323.3359683794467
        ],
        [
          235.8142292490119,
          298.3359683794467
        ],
        [
          238.8142292490119,
          287.3359683794467
        ],
        [
          234.8142292490119,
          268.3359683794467
        ],
        [
          257.81422924901193,
          258.3359683794467
        ],
        [
          264.81422924901193,
          264.3359683794467
        ],
        [
          256.81422924901193,
          273.3359683794467
        ],
        [
          259.81422924901193,
          282.3359683794467
        ],
        [
          284.81422924901193,
          288.3359683794467
        ],
        [
          297.81422924901193,
          278.3359683794467
        ],
        [
          288.81422924901193,
          270.3359683794467
        ],
        [
          281.81422924901193,
          270.3359683794467
        ],
        [
          283.81422924901193,
          264.3359683794467
        ],
        [
          292.81422924901193,
          261.3359683794467
        ],
        [
          308.81422924901193,
          236.33596837944665
        ],
        [
          313.81422924901193,
          217.33596837944665
        ],
        [
          309.81422924901193,
          208.33596837944665
        ],
        [
          312.81422924901193,
          202.33596837944665
        ],
        [
          308.81422924901193,
          185.33596837944665
        ],
        [
          291.81422924901193,
          173.33596837944665
        ],
        [
          269.81422924901193,
          159.33596837944665
        ],
        [
          261.81422924901193,
          154.33596837944665
        ],
        [
          264.81422924901193,
          142.33596837944665
        ],
        [
          273.81422924901193,
          137.33596837944665
        ],
        [
          278.81422924901193,
          130.33596837944665
        ],
        [
          270.81422924901193,
          121.33596837944665
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    },
    {
      "label": "person",
      "points": [
        [
          482.81422924901193,
          87.18098682963114
        ],
        [
          468.81422924901193,
          92.18098682963114
        ],
        [
          460.81422924901193,
          112.18098682963114
        ],
        [
          460.81422924901193,
          129.18098682963114
        ],
        [
          444.81422924901193,
          139.18098682963114
        ],
        [
          419.81422924901193,
          155.18098682963114
        ],
        [
          410.81422924901193,
          165.18098682963114
        ],
        [
          403.81422924901193,
          170.18098682963114
        ],
        [
          394.81422924901193,
          172.18098682963114
        ],
        [
          386.81422924901193,
          170.18098682963114
        ],
        [
          386.81422924901193,
          186.18098682963114
        ],
        [
          392.81422924901193,
          184.18098682963114
        ],
        [
          410.81422924901193,
          189.18098682963114
        ],
        [
          414.81422924901193,
          194.18098682963114
        ],
        [
          437.81422924901193,
          191.18098682963114
        ],
        [
          434.81422924901193,
          206.18098682963114
        ],
        [
          390.81422924901193,
          197.18098682963114
        ],
        [
          386.81422924901193,
          197.18098682963114
        ],
        [
          387.81422924901193,
          210.18098682963114
        ],
        [
          381.81422924901193,
          214.18098682963114
        ],
        [
          372.81422924901193,
          214.18098682963114
        ],
        [
          372.81422924901193,
          218.18098682963114
        ],
        [
          400.81422924901193,
          272.18098682963114
        ],
        [
          389.81422924901193,
          274.18098682963114
        ],
        [
          389.81422924901193,
          276.18098682963114
        ],
        [
          403.81422924901193,
          284.18098682963114
        ],
        [
          444.81422924901193,
          285.18098682963114
        ],
        [
          443.81422924901193,
          261.18098682963114
        ],
        [
          426.81422924901193,
          246.18098682963114
        ],
        [
          462.81422924901193,
          258.18098682963114
        ],
        [
          474.81422924901193,
          272.18098682963114
        ],
        [
          477.81422924901193,
          282.18098682963114
        ],
        [
          473.81422924901193,
          291.18098682963114
        ],
        [
          471.81422924901193,
          298.18098682963114
        ],
        [
          472.81422924901193,
          319.18098682963114
        ],
        [
          480.81422924901193,
          334.18098682963114
        ],
        [
          494.81422924901193,
          337.18098682963114
        ],
        [
          498.81422924901193,
          331.18098682963114
        ],
        [
          494.81422924901193,
          310.18098682963114
        ],
        [
          499.81422924901193,
          299.18098682963114
        ],
        [
          499.81422924901193,
          92.18098682963114
        ]
      ],
      "group_id": 0,
      "shape_type": "polygon",
      "flags": {}
    },
    {
    }
  ],
  "imagePath": "2011_000003.jpg",
  "imageData": 
  "imageHeight": 338,
  "imageWidth": 500
}
Digital2Slave commented 1 year ago

@JNaranjo-Alcazar The script of @sdimantsd is work for COCO format json file. You can use label-studio to label images and export COCO format json file. Or you can modify the script of @sdimantsd to handle json file of labelme.

a COCO format json file maybe like the following:

{
  "images": [
    {
      "width": 1037,
      "height": 501,
      "id": 0,
      "file_name": "images/3/ad37161b-P92902000484212110001_-2_crop.jpg"
    }
  ],
  "categories": [
    {
      "id": 0,
      "name": "Blur"
    },
    {
      "id": 1,
      "name": "Phone"
    },
    {
      "id": 2,
      "name": "ReflectLight"
    },
    {
      "id": 3,
      "name": "Reflection"
    }
  ],
  "annotations": [
    {
      "id": 0,
      "image_id": 0,
      "category_id": 0,
      "segmentation": [
        [
          4.106930693069307,
          65.70491803278689,
          43.122772277227725,
          25.66598360655738,
          988.7435643564356,
          12.319672131147541,
          1029.8128712871287,
          49.278688524590166,
          1028.786138613861,
          428.1086065573771,
          1004.1445544554455,
          459.9344262295082,
          964.1019801980199,
          467.1209016393442,
          529.7940594059406,
          482.5204918032787,
          247.44257425742566,
          487.6536885245902,
          84.1920792079208,
          490.7336065573771,
          26.695049504950497,
          477.38729508196724,
          11.294059405940594,
          444.53483606557376
        ]
      ],
      "bbox": [
        4.106930693069307,
        12.319672131147541,
        1025.7059405940595,
        478.4139344262295
      ],
      "ignore": 0,
      "iscrowd": 0,
      "area": 468549.3681311881
    },
    {
      "id": 1,
      "image_id": 0,
      "category_id": 3,
      "segmentation": [
        [
          57.4970297029703,
          37.98565573770492,
          135.52871287128715,
          55.43852459016394,
          203.2930693069307,
          58.51844262295083,
          242.30891089108914,
          60.57172131147539,
          291.5920792079209,
          63.65163934426229,
          323.4207920792079,
          70.83811475409836,
          355.2495049504951,
          86.23770491803279,
          370.65049504950497,
          103.69057377049181,
          379.891089108911,
          112.93032786885244,
          405.55940594059405,
          120.11680327868852,
          425.0673267326732,
          120.11680327868852,
          464.08316831683175,
          122.17008196721311,
          489.7514851485148,
          124.22336065573771,
          512.339603960396,
          127.30327868852459,
          520.5534653465346,
          137.56967213114754,
          524.660396039604,
          148.86270491803282,
          527.7405940594059,
          165.28893442622945,
          522.6069306930693,
          190.95491803278688,
          525.6871287128713,
          208.4077868852459,
          529.7940594059406,
          431.1885245901639,
          521.580198019802,
          451.7213114754098,
          504.12574257425734,
          459.9344262295082,
          77.00495049504946,
          474.30737704918033,
          48.25643564356436,
          447.61475409836066,
          40.04257425742574,
          414.76229508196724,
          39.015841584158416,
          54.411885245901644
        ]
      ],
      "bbox": [
        39.015841584158416,
        37.98565573770492,
        490.7782178217822,
        436.3217213114754
      ],
      "ignore": 0,
      "iscrowd": 0,
      "area": 188048.61386138643
    },
    {
      "id": 2,
      "image_id": 0,
      "category_id": 3,
      "segmentation": [
        [
          693.0445544554455,
          35.932377049180324,
          772.1029702970297,
          26.692622950819672,
          802.9049504950495,
          441.45491803278685,
          794.6910891089109,
          456.8545081967213,
          752.5950495049505,
          459.9344262295082,
          728.980198019802,
          461.98770491803276
        ]
      ],
      "bbox": [
        693.0445544554455,
        26.692622950819672,
        109.8603960396041,
        435.2950819672131
      ],
      "ignore": 0,
      "iscrowd": 0,
      "area": 33132.500309405965
    }
  ],
  "info": {
    "year": 2022,
    "version": "1.0",
    "description": "",
    "contributor": "Label Studio",
    "url": "",
    "date_created": "2022-12-12 08:37:26.832616"
  }
}
quocnhat commented 1 year ago

Hi everybody! The output of labelme is a .json named with the same name as the image. Find attached an example of what I got. @sdimantsd I cannot figure out how to adapt my configuration to your script,... Any ideas?

version": "5.0.2",
  "flags": {},
  "shapes": [
    {
      "label": "person",
      "points": [
        [
          250.8142292490119,
          107.33596837944665
        ],
        [
          229.8142292490119,
          119.33596837944665
        ],
        [
          221.8142292490119,
          135.33596837944665
        ],
        [
          223.8142292490119,
          148.33596837944665
        ],
        [
          217.8142292490119,
          161.33596837944665
        ],
        [
          202.8142292490119,
          168.33596837944665
        ],
        [
          192.8142292490119,
          200.33596837944665
        ],
        [
          194.8142292490119,
          222.33596837944665
        ],
        [
          199.8142292490119,
          227.33596837944665
        ],
        [
          191.8142292490119,
          234.33596837944665
        ],
        [
          197.8142292490119,
          264.3359683794467
        ],
        [
          213.8142292490119,
          295.3359683794467
        ],
        [
          214.8142292490119,
          320.3359683794467
        ],
        [
          221.8142292490119,
          327.3359683794467
        ],
        [
          235.8142292490119,
          326.3359683794467
        ],
        [
          240.8142292490119,
          323.3359683794467
        ],
        [
          235.8142292490119,
          298.3359683794467
        ],
        [
          238.8142292490119,
          287.3359683794467
        ],
        [
          234.8142292490119,
          268.3359683794467
        ],
        [
          257.81422924901193,
          258.3359683794467
        ],
        [
          264.81422924901193,
          264.3359683794467
        ],
        [
          256.81422924901193,
          273.3359683794467
        ],
        [
          259.81422924901193,
          282.3359683794467
        ],
        [
          284.81422924901193,
          288.3359683794467
        ],
        [
          297.81422924901193,
          278.3359683794467
        ],
        [
          288.81422924901193,
          270.3359683794467
        ],
        [
          281.81422924901193,
          270.3359683794467
        ],
        [
          283.81422924901193,
          264.3359683794467
        ],
        [
          292.81422924901193,
          261.3359683794467
        ],
        [
          308.81422924901193,
          236.33596837944665
        ],
        [
          313.81422924901193,
          217.33596837944665
        ],
        [
          309.81422924901193,
          208.33596837944665
        ],
        [
          312.81422924901193,
          202.33596837944665
        ],
        [
          308.81422924901193,
          185.33596837944665
        ],
        [
          291.81422924901193,
          173.33596837944665
        ],
        [
          269.81422924901193,
          159.33596837944665
        ],
        [
          261.81422924901193,
          154.33596837944665
        ],
        [
          264.81422924901193,
          142.33596837944665
        ],
        [
          273.81422924901193,
          137.33596837944665
        ],
        [
          278.81422924901193,
          130.33596837944665
        ],
        [
          270.81422924901193,
          121.33596837944665
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    },
    {
      "label": "person",
      "points": [
        [
          482.81422924901193,
          87.18098682963114
        ],
        [
          468.81422924901193,
          92.18098682963114
        ],
        [
          460.81422924901193,
          112.18098682963114
        ],
        [
          460.81422924901193,
          129.18098682963114
        ],
        [
          444.81422924901193,
          139.18098682963114
        ],
        [
          419.81422924901193,
          155.18098682963114
        ],
        [
          410.81422924901193,
          165.18098682963114
        ],
        [
          403.81422924901193,
          170.18098682963114
        ],
        [
          394.81422924901193,
          172.18098682963114
        ],
        [
          386.81422924901193,
          170.18098682963114
        ],
        [
          386.81422924901193,
          186.18098682963114
        ],
        [
          392.81422924901193,
          184.18098682963114
        ],
        [
          410.81422924901193,
          189.18098682963114
        ],
        [
          414.81422924901193,
          194.18098682963114
        ],
        [
          437.81422924901193,
          191.18098682963114
        ],
        [
          434.81422924901193,
          206.18098682963114
        ],
        [
          390.81422924901193,
          197.18098682963114
        ],
        [
          386.81422924901193,
          197.18098682963114
        ],
        [
          387.81422924901193,
          210.18098682963114
        ],
        [
          381.81422924901193,
          214.18098682963114
        ],
        [
          372.81422924901193,
          214.18098682963114
        ],
        [
          372.81422924901193,
          218.18098682963114
        ],
        [
          400.81422924901193,
          272.18098682963114
        ],
        [
          389.81422924901193,
          274.18098682963114
        ],
        [
          389.81422924901193,
          276.18098682963114
        ],
        [
          403.81422924901193,
          284.18098682963114
        ],
        [
          444.81422924901193,
          285.18098682963114
        ],
        [
          443.81422924901193,
          261.18098682963114
        ],
        [
          426.81422924901193,
          246.18098682963114
        ],
        [
          462.81422924901193,
          258.18098682963114
        ],
        [
          474.81422924901193,
          272.18098682963114
        ],
        [
          477.81422924901193,
          282.18098682963114
        ],
        [
          473.81422924901193,
          291.18098682963114
        ],
        [
          471.81422924901193,
          298.18098682963114
        ],
        [
          472.81422924901193,
          319.18098682963114
        ],
        [
          480.81422924901193,
          334.18098682963114
        ],
        [
          494.81422924901193,
          337.18098682963114
        ],
        [
          498.81422924901193,
          331.18098682963114
        ],
        [
          494.81422924901193,
          310.18098682963114
        ],
        [
          499.81422924901193,
          299.18098682963114
        ],
        [
          499.81422924901193,
          92.18098682963114
        ]
      ],
      "group_id": 0,
      "shape_type": "polygon",
      "flags": {}
    },
    {
    }
  ],
  "imagePath": "2011_000003.jpg",
  "imageData": 
  "imageHeight": 338,
  "imageWidth": 500
}

super ez, here is the solution:

seg_img = '' seg_txt = ''

anno_file_list = glob.glob(seg_txt+'/*.json')

anno_file_list = [f for f in os.listdir(seg_txt) if f.endswith('json')] save_folder = 'train_seg' if not os.path.isdir(save_folder): p = Path(save_folder) p.mkdir(parents=True, exist_ok=True) for anno_file in tqdm(anno_file_list): file_name = os.path.basename(anno_file) annotate = json.load(open(os.path.join(seg_txt,anno_file))) img = cv2.imread(os.path.join(seg_img,file_name.replace('json','jpg'))) h,w = img.shape[:2] if len(annotate["shapes"]): with open(os.path.join(save_folder,file_name.replace('json','txt')),'w') as wf: for idx,i in enumerate(annotate["shapes"]):
x =[] y =[] out_seg = [] poly_points =[] for j in i['points']: out_seg.append(j[0]/w) out_seg.append(j[1]/h)

            x = np.array(x)
            y = np.array(y) 
            line = str(define_dict[i['label']])

            for coord in out_seg:
                line += ' ' + str(coord)
            wf.write(line)
            wf.write('\n')
pythondever commented 1 year ago
import os
import json
import glob
import cv2
import numpy as np
import matplotlib.pyplot as plt

def labelme_json_to_yolo_seg_format(input_json_dir, output_text_dir):
    # transfer labelme json to yolo txt label  (only for segmentation)
    jsons = glob.glob(os.path.join(input_json_dir, "*.json"))
    for item in jsons:
        with open(item) as fb:
            data = json.load(fb)
            shapes = data['shapes']
            height = data['imageHeight']
            width = data['imageWidth']
            image_path = data['imagePath']
            basename = os.path.basename(image_path).split('.')[0]
            output_txt = os.path.join(output_text_dir, basename + '.txt')
            with open(output_txt, 'w') as fb2:
                for shape in shapes:
                    shape_type = shape['shape_type']
                    points = shape['points']
                    label = shape['label']
                    points = np.array(points)
                    if shape_type == 'polygon':
                        x = points[:, 0] / width
                        y = points[:, 1] / height
                        xyxy = [str(x) for pair in zip(x, y) for x in pair]
                        xyxy = ' '.join(xyxy)
                        line = '{} {}'.format(label, xyxy)
                        fb2.write(line)
                        fb2.write('\n')
            print("output={}, done".format(output_txt))

def show_yolo_txt_image(txt_path, image_path, image_fmt='.png'):
    # show labeled image  from yolo txt
    label_txt = glob.glob(os.path.join(txt_path, "*.txt"))
    images = glob.glob(os.path.join(image_path, "*{}".format(image_fmt)))
    for item in label_txt:
        txt_basename = os.path.basename(item).split('.')[0]
        for image in images:
            img = cv2.imread(image)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            h, w = img.shape[:2]
            img_basename = os.path.basename(image).split('.')[0]
            if img_basename == txt_basename:
                with open(item) as fb:
                    lines = fb.readlines()
                    for line in lines:
                        labeled = line.split(' ')
                        cls = labeled[0]
                        xyxy = labeled[1:]
                        x = xyxy[::2]
                        y = xyxy[1::2]
                        x = np.array(x, dtype=np.float)
                        y = np.array(y, dtype=np.float)
                        x2 = x * w
                        y2 = y * h
                        lst = list(zip(x2, y2))
                        points = np.array(lst, dtype=np.int32)
                        cv2.polylines(img, [points], isClosed=True, color=(0, 0, 255), thickness=5)
                    plt.imshow(img)
                    plt.show()

if __name__ == "__main__":
    input_ = r'E:\2022-12-09'
    output = r'E:\2022-12-09\yolo-txt'
    # labelme_json_to_yolo_seg_format(input_, output)
    show_yolo_txt_image(output, input_)
ryouchinsa commented 1 year ago

You can convert the Labelme JSON files to the COCO format. https://github.com/wkentaro/labelme/blob/main/examples/instance_segmentation/labelme2coco.py

You can convert the COCO JSON file to the YOLO segmentation format. https://github.com/ultralytics/JSON2YOLO

RectLabel is an offline image annotation tool for object detection and segmentation. Although this is not an open source program, with RectLabel you can import the Labelme JSON files and export to the YOLO segmentation format.

yolo_polygon

github-actions[bot] commented 1 year ago

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐!

github-actions[bot] commented 1 year ago

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐!

clebemachado commented 1 year ago

Just contributing the way I did, I saved the data in a list and then exported it to txt. File is the json file

def convert_yolo(file):

    imageHeight = file["imageHeight"]
    imageWidth = file["imageWidth"]

    classe = "0"
    point_txt = []

    for shape in file["shapes"]:
        txt = f"{classe}"

        for w, h in  shape["points"]:
            txt = f'{txt} {float(w)/imageWidth} {float(h)/imageHeight}'

        point_txt.append(txt)
    return point_txt
glenn-jocher commented 1 year ago

@clebemachado you've made a great contribution! Your code looks clean and concise. It's good to see that you've extracted the necessary information from the JSON file, such as imageHeight and imageWidth, and used it to calculate the normalized coordinates for each point.

One suggestion I have is to make the class label (classe) more dynamic so that it can be customized based on your application. Currently, you have set it to "0" for all shapes. If you want to handle multiple classes, you can modify the code to assign different class labels based on the shape or any other criteria.

Overall, your code is well-written and performs the conversion from JSON to the YOLO format effectively. Keep up the good work!

naveenvj25 commented 1 year ago

I am trying to convert Labelme Json file to yolo format, but the bounding box is getting shifted, I will share my conversion code here. import os import json

def json_to_yolo(json_file, output_yolo_file): with open(json_file, 'r') as json_file: data = json.load(json_file) image_width=data['imageWidth'] image_height=data['imageWidth']

with open(output_yolo_file, 'w') as yolo_file:
      for shape in data['shapes']:
          label = shape['label']
          # Extract all x and y coordinates from the points
          x_values = [point[0] for point in shape['points']]
          y_values = [point[1] for point in shape['points']]

# Calculate the bounding box coordinates
          x_min = min(x_values)
          print(x_min)
          x_max = max(x_values)
          print(x_max)
          y_min = min(y_values)
          print(y_min)
          y_max = max(y_values)
          print(y_max)
        # Calculate YOLO coordinates
          x_center = (x_min + x_max) / ( 2*image_width)
          y_center = (y_min + y_max) / ( 2*image_height)
          width = (x_max - x_min) / (image_width)
          height = (y_max - y_min) / (image_height)
          # YOLO format: <class_id> <center_x> <center_y> <width> <height>
          class_id = get_class_id(label)
        # Write YOLO format data to the output YOLO file
          yolo_line = f"{class_id} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f}"
          yolo_file.write(yolo_line + "\n")

def get_class_id(label):

Implement your own logic to map label names to class IDs

class_id_mapping = {
    'Grade 1': 0,
    'Grade 2': 1,
    'Grade 3': 2,
    # Add more class labels as needed
}
return class_id_mapping.get(label, -1)  # -1 for unknown labels

if name == 'main': json_file = '/content/IMG_0188.json' output_yolo_file = '/content/yolo_0188.txt' json_to_yolo(json_file, output_yolo_file)

Using json file, I am getting the correct bounding box on the image, but the box is shifted when I display the bounding box from the text file ..!

glenn-jocher commented 1 year ago

@naveenvj25 it seems that the issue might be related to how the bounding box coordinates are calculated and converted to the YOLO format. One aspect to check is the normalization of the coordinates. Ensure that you're normalizing the coordinates with respect to the correct image width and height, and also that the center coordinates and dimensions are calculated accurately.

Additionally, it's essential to review how the bounding box is being displayed when loaded from the text file. It's possible that there may be an issue with the visualization or parsing of the text file for display. Comparing the calculated YOLO coordinates with the original coordinates from the JSON file can also help in identifying any discrepancies.

Analyzing these points should provide insight into the cause of the bounding box shift when displayed from the text file.

naveenvj25 commented 1 year ago

@glenn-jocher Yes, I got it..! The error was in the center calculation

glenn-jocher commented 1 year ago

@naveenvj25 great to hear that you found the issue! Calculating the bounding box center accurately is crucial for the correct representation of the bounding box coordinates in the YOLO format. If you have any further questions or need assistance with any other aspects of your project, feel free to ask. Good luck with your Labelme JSON to YOLO conversion!