ultralytics / JSON2YOLO

Convert JSON annotations into YOLO format.
https://docs.ultralytics.com
GNU Affero General Public License v3.0
855 stars 231 forks source link

How to convert this format into yolov5/v7 compatible .txt file. #35

Open Awader28 opened 1 year ago

Awader28 commented 1 year ago

[{ 2 " Id ": <-- int: label id -->, 3 " ObjectClassName ": <-- string : object class name -->, 4 " ObjectClassId ": <-- int: object class id as mentioned in the Objectclasses . json -->, 5 " Left ": <-- int: left bbox coordinates -->, 6 " Top ": <-- int: top bbox coordinates -->, 7 " Right ": <-- int: right bbox coordinates -->, 8 " Bottom ": <-- int: bottom bbox coordinates --> 9 }, 10 { 11 " Id ": 18111997, 12 " ObjectClassName ": " example ", 13 " ObjectClassId ": 7, 14 " Left ": 294, 15 " Top ": 115, 16 " Right ": 314, 17 " " Bottom ": 154 18 }]

glenn-jocher commented 1 year ago

👋 Hello! Thanks for asking about YOLOv5 🚀 dataset formatting. To train correctly your data must be in YOLOv5 format. Please see our Train Custom Data tutorial for full documentation on dataset setup and all steps required to start training your first model. A few excerpts from the tutorial:

1.1 Create dataset.yaml

COCO128 is an example small tutorial dataset composed of the first 128 images in COCO train2017. These same 128 images are used for both training and validation to verify our training pipeline is capable of overfitting. data/coco128.yaml, shown below, is the dataset config file that defines 1) the dataset root directory path and relative paths to train / val / test image directories (or *.txt files with image paths) and 2) a class names dictionary:

# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ../datasets/coco128  # dataset root dir
train: images/train2017  # train images (relative to 'path') 128 images
val: images/train2017  # val images (relative to 'path') 128 images
test:  # test images (optional)

# Classes (80 COCO classes)
names:
  0: person
  1: bicycle
  2: car
  ...
  77: teddy bear
  78: hair drier
  79: toothbrush

1.2 Create Labels

After using a tool like Roboflow Annotate to label your images, export your labels to YOLO format, with one *.txt file per image (if no objects in image, no *.txt file is required). The *.txt file specifications are:

The label file corresponding to the above image contains 2 persons (class 0) and a tie (class 27):

1.3 Organize Directories

Organize your train and val images and labels according to the example below. YOLOv5 assumes /coco128 is inside a /datasets directory next to the /yolov5 directory. YOLOv5 locates labels automatically for each image by replacing the last instance of /images/ in each image path with /labels/. For example:

../datasets/coco128/images/im0.jpg  # image
../datasets/coco128/labels/im0.txt  # label

Good luck 🍀 and let us know if you have any other questions!