当前位置:   article > 正文

YOLO格式数据集转COCO格式_yolo转coco

yolo转coco

网上找了很久的YOLO格式数据集转COCO格式的代码,但是没有一个成功的,费尽千辛万苦终于找到一个能用的,因此记录一下。

一、首先YOLO格式数据集文件布局

其中lmages和labels 中的内容布局如下,只不过一个存放图片,一个存放标签

二、COCO数据集布局

下面的代码生产生成的是下图的第一个文件夹,存放标注文件json,其他三个文件夹都是图片

三、转换代码

  1. import json
  2. import os
  3. import shutil
  4. import cv2
  5. # info ,license,categories 结构初始化;
  6. # 在train.json,val.json,test.json里面信息是一致的;
  7. # info,license暂时用不到
  8. info = {
  9. "year": 2024,
  10. "version": '1.0',
  11. "date_created": 2024 - 3 - 29
  12. }
  13. licenses = {
  14. "id": 1,
  15. "name": "null",
  16. "url": "null",
  17. }
  18. #自己的标签类别,跟yolo的数据集类别要对应好;
  19. categories = [
  20. {
  21. "id": 0,
  22. "name": 'Eating',
  23. "supercategory": 'lines',
  24. },
  25. {
  26. "id": 1,
  27. "name": 'Raising_a_hand',
  28. "supercategory": 'lines',
  29. },
  30. {
  31. "id": 2,
  32. "name": 'Reading',
  33. "supercategory": 'lines',
  34. },
  35. {
  36. "id": 3,
  37. "name": 'Sleeping_At_a_desk',
  38. "supercategory": 'lines',
  39. },
  40. {
  41. "id": 4,
  42. "name": 'Writing',
  43. "supercategory": 'lines',
  44. }
  45. ]
  46. #初始化train,test、valid 数据字典
  47. # info licenses categories 在train和test里面都是一致的;
  48. train_data = {'info': info, 'licenses': licenses, 'categories': categories, 'images': [], 'annotations': []}
  49. test_data = {'info': info, 'licenses': licenses, 'categories': categories, 'images': [], 'annotations': []}
  50. valid_data = {'info': info, 'licenses': licenses, 'categories': categories, 'images': [], 'annotations': []}
  51. # image_path 对应yolov8的图像路径,比如images/train;
  52. # label_path 对应yolov8的label路径,比如labels/train 跟images要对应;
  53. def yolo_covert_coco_format(image_path, label_path):
  54. images = []
  55. annotations = []
  56. for index, img_file in enumerate(os.listdir(image_path)):
  57. if img_file.endswith('.jpg'):
  58. image_info = {}
  59. img = cv2.imread(os.path.join(image_path, img_file))
  60. height, width, channel = img.shape
  61. image_info['id'] = index
  62. image_info['file_name'] = img_file
  63. image_info['width'], image_info['height'] = width, height
  64. else:
  65. continue
  66. if image_info != {}:
  67. images.append(image_info)
  68. # 处理label信息-------
  69. label_file = os.path.join(label_path, img_file.replace('.jpg', '.txt'))
  70. with open(label_file, 'r') as f:
  71. for idx, line in enumerate(f.readlines()):
  72. info_annotation = {}
  73. class_num, xs, ys, ws, hs = line.strip().split(' ')
  74. class_id, xc, yc, w, h = int(class_num), float(xs), float(ys), float(ws), float(hs)
  75. xmin = (xc - w / 2) * width
  76. ymin = (yc - h / 2) * height
  77. xmax = (xc + w / 2) * width
  78. ymax = (yc + h / 2) * height
  79. bbox_w = int(width * w)
  80. bbox_h = int(height * h)
  81. img_copy = img[int(ymin):int(ymax),int(xmin):int(xmax)].copy()
  82. info_annotation["category_id"] = class_id # 类别的id
  83. info_annotation['bbox'] = [xmin, ymin, bbox_w, bbox_h] ## bbox的坐标
  84. info_annotation['area'] = bbox_h * bbox_w ###area
  85. info_annotation['image_id'] = index # bbox的id
  86. info_annotation['id'] = index * 100 + idx # bbox的id
  87. # cv2.imwrite(f"./temp/{info_annotation['id']}.jpg", img_copy)
  88. info_annotation['segmentation'] = [[xmin, ymin, xmax, ymin, xmax, ymax, xmin, ymax]] # 四个点的坐标
  89. info_annotation['iscrowd'] = 0 # 单例
  90. annotations.append(info_annotation)
  91. return images, annotations
  92. # key == train,test,val
  93. # 对应要生成的json文件,比如instances_train.json,instances_test.json,instances_val.json
  94. # 只是为了不重复写代码。。。。。
  95. def gen_json_file(yolov8_data_path, coco_format_path, key):
  96. # json path
  97. json_path = os.path.join(coco_format_path, f'annotations/instances_{key}.json')
  98. dst_path = os.path.join(coco_format_path, f'{key}')
  99. if not os.path.exists(os.path.dirname(json_path)):
  100. os.makedirs(os.path.dirname(json_path), exist_ok=True)
  101. data_path = os.path.join(yolov8_data_path, f'images/{key}')
  102. label_path = os.path.join(yolov8_data_path, f'labels/{key}')
  103. images, anns = yolo_covert_coco_format(data_path, label_path)
  104. if key == 'train':
  105. train_data['images'] = images
  106. train_data['annotations'] = anns
  107. with open(json_path, 'w') as f:
  108. json.dump(train_data, f, indent=2)
  109. # shutil.copy(data_path,'')
  110. elif key == 'test':
  111. test_data['images'] = images
  112. test_data['annotations'] = anns
  113. with open(json_path, 'w') as f:
  114. json.dump(test_data, f, indent=2)
  115. elif key == 'val':
  116. valid_data['images'] = images
  117. valid_data['annotations'] = anns
  118. with open(json_path, 'w') as f:
  119. json.dump(valid_data, f, indent=2)
  120. else:
  121. print(f'key is {key}')
  122. print(f'generate {key} json success!')
  123. return
  124. if __name__ == '__main__':
  125. yolov8_data_path = 'D:/deep_learn/yolov8_20230701/ClassroomBehavior'
  126. coco_format_path = 'D:/deep_learn/yolov8_20230701/COCO'
  127. gen_json_file(yolov8_data_path, coco_format_path,key='train')
  128. gen_json_file(yolov8_data_path, coco_format_path,key='val')
  129. gen_json_file(yolov8_data_path, coco_format_path, key='test')

编译后打印下列信息,代表转换完成

运行后生成下图中的文件,至此任务完成。如果帮到你,麻烦帮忙点点赞

 参考文章

代码实现如何将yolov5数据格式转换为coco格式_yolo转coco-CSDN博客

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

闽ICP备14008679号