当前位置:   article > 正文

labelme的json标注文件转yolo的txt训练文件,及划分训练集代码(YOLOv8)_labelme标注转txt

labelme标注转txt

一、代码块目录详情

二、json2txt.py

  1. import json
  2. import os
  3. def convert_annotation(json_file, txt_file, label_to_id_mapping):
  4. # Read the JSON file
  5. with open(json_file, 'r', encoding='utf-8') as f:
  6. data = json.load(f)
  7. # Extract image dimensions, assuming 'imageWidth' and 'imageHeight' fields are present
  8. image_width = data.get('imageWidth')
  9. image_height = data.get('imageHeight')
  10. # Check if image dimensions are present
  11. if image_width is None or image_height is None:
  12. raise ValueError(f"Missing image dimensions in {json_file}")
  13. # Iterate over all shapes (annotations)
  14. with open(txt_file, 'w', encoding='utf-8') as out_file:
  15. for shape in data.get('shapes', []):
  16. # Extract point coordinates, assuming each shape has a 'points' field
  17. points = shape.get('points', [])
  18. # Check if points are present
  19. if not points:
  20. raise ValueError(f"Missing points in a shape in {json_file}")
  21. x_values = [point[0] for point in points]
  22. y_values = [point[1] for point in points]
  23. x_min = min(x_values)
  24. y_min = min(y_values)
  25. x_max = max(x_values)
  26. y_max = max(y_values)
  27. # Calculate bounding box center, width, and height
  28. bbox_center_x = (x_min + x_max) / 2
  29. bbox_center_y = (y_min + y_max) / 2
  30. bbox_width = x_max - x_min
  31. bbox_height = y_max - y_min
  32. # Convert bounding box coordinates to ratios relative to image dimensions
  33. bbox_center_x_ratio = bbox_center_x / image_width
  34. bbox_center_y_ratio = bbox_center_y / image_height
  35. bbox_width_ratio = bbox_width / image_width
  36. bbox_height_ratio = bbox_height / image_height
  37. # Get the category ID, assuming each shape has a 'label' field
  38. category_id = shape.get('label', "unknown")
  39. if isinstance(category_id, str):
  40. # If the label is a string, map it to a numeric ID using the provided mapping
  41. category_id = label_to_id_mapping.get(category_id, -1) # Default to -1 if label is unknown
  42. # Write the result to the TXT file in YOLO format
  43. out_file.write(
  44. f"{int(category_id)} {bbox_center_x_ratio} {bbox_center_y_ratio} {bbox_width_ratio} {bbox_height_ratio}\n")
  45. # Input and output folder paths
  46. input_folder = 'D:/sss/datasets/VOCdevkit/jsons'
  47. output_folder = 'D:/sss/datasets/VOCdevkit/txt'
  48. os.makedirs(output_folder, exist_ok=True)
  49. # 标注物体的类别名
  50. label_to_id_mapping = {
  51. 'Red Light': 0,
  52. 'Red Light': 1,
  53. 'Yellow Light': 2
  54. # Add more mappings as needed
  55. }
  56. # Iterate over all JSON files in the input folder
  57. for filename in os.listdir(input_folder):
  58. if filename.endswith('.json'):
  59. json_file = os.path.join(input_folder, filename)
  60. txt_file = os.path.join(output_folder, filename.replace('.json', '.txt'))
  61. try:
  62. convert_annotation(json_file, txt_file, label_to_id_mapping)
  63. print(f'{txt_file},Conversion successful!')
  64. except Exception as e:
  65. print(f"An error occurred while processing {json_file}: {e}")

三、split_data.py

  1. import os, shutil
  2. from sklearn.model_selection import train_test_split
  3. val_size = 0.1
  4. test_size = 0.1
  5. postfix = 'jpg'
  6. imgpath = 'VOCdevkit/images'
  7. txtpath = 'VOCdevkit/txt'
  8. os.makedirs('images/train', exist_ok=True)
  9. os.makedirs('images/val', exist_ok=True)
  10. os.makedirs('images/test', exist_ok=True)
  11. os.makedirs('labels/train', exist_ok=True)
  12. os.makedirs('labels/val', exist_ok=True)
  13. os.makedirs('labels/test', exist_ok=True)
  14. listdir = [i for i in os.listdir(txtpath) if 'txt' in i]
  15. train, test = train_test_split(listdir, test_size=test_size, shuffle=True, random_state=0)
  16. train, val = train_test_split(train, test_size=val_size, shuffle=True, random_state=0)
  17. print(f'train set size:{len(train)} val set size:{len(val)} test set size:{len(test)}')
  18. for i in train:
  19. shutil.copy('{}/{}.{}'.format(imgpath, i[:-4], postfix), 'images/train/{}.{}'.format(i[:-4], postfix))
  20. shutil.copy('{}/{}'.format(txtpath, i), 'labels/train/{}'.format(i))
  21. for i in val:
  22. shutil.copy('{}/{}.{}'.format(imgpath, i[:-4], postfix), 'images/val/{}.{}'.format(i[:-4], postfix))
  23. shutil.copy('{}/{}'.format(txtpath, i), 'labels/val/{}'.format(i))
  24. for i in test:
  25. shutil.copy('{}/{}.{}'.format(imgpath, i[:-4], postfix), 'images/test/{}.{}'.format(i[:-4], postfix))
  26. shutil.copy('{}/{}'.format(txtpath, i), 'labels/test/{}'.format(i))

四、data.yaml

  1. # 划分数据集的地址
  2. train: D:\sss\datasets\images\train
  3. val: D:\sss\datasets\images\val
  4. test: D:\sss\datasets\images\test
  5. # 标注的类别数量
  6. nc: 3
  7. # 类别名称
  8. names: ['Red Light', 'Red Light', 'Yellow Light']

兄台,本章对你有用的话,记得一键三连哦!

更多资源,请移步至本人的github首页:lcx9451 (lcx9451) (github.com)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/665750
推荐阅读
相关标签
  

闽ICP备14008679号