Python将COCO格式实例分割数据集转换为YOLO格式实例分割数据集
前言
相关介绍
- Python是一种跨平台的计算机程序设计语言。是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越多被用于独立的、大型项目的开发。
- PyTorch 是一个深度学习框架,封装好了很多网络和深度学习相关的工具方便我们调用,而不用我们一个个去单独写了。它分为 CPU 和 GPU 版本,其他框架还有 TensorFlow、Caffe 等。PyTorch 是由 Facebook 人工智能研究院(FAIR)基于 Torch 推出的,它是一个基于 Python 的可续计算包,提供两个高级功能:1、具有强大的 GPU 加速的张量计算(如 NumPy);2、构建深度神经网络时的自动微分机制。
COCO格式实例分割数据集转换为YOLO格式实例分割数据集
coco格式对应的json文件,以test.json为例
{
"annotations": [
{
"id": 2094,
"iscrowd": 0,
"image_id": 173,
"category_id": 1,
"segmentation": [
[
1113,
777,
1115,
785,
1118,
784,
1120,
786,
1120,
792,
1118,
796,
1117,
801,
1117,
868,
1118,
875,
1120,
880,
1124,
882,
1138,
882,
1146,
884,
1153,
899,
1157,
901,
1166,
901,
1176,
899,
1178,
897,
1185,
888,
1204,
887,
1217,
884,
1291,
886,
1299,
885,
1302,
883,
1312,
883,
1323,
890,
1325,
899,
1332,
905,
1353,
905,
1360,
895,
1362,
885,
1364,
863,
1364,
833,
1359,
797,
1351,
774,
1326,
735,
1313,
726,
1297,
722,
1198,
720,
1191,
716,
1186,
718,
1177,
718,
1155,
732,
1150,
736,
1145,
745,
1145,
747,
1140,
755,
1135,
769,
1124,
776,
1121,
776,
1119,
774
]
],
"area": 38102,
"bbox": [
1113,
716,
251,
189
]
},
{
"id": 577,
"iscrowd": 0,
"image_id": 43,
"category_id": 3,
"segmentation": [
[
950,
795,
954,
803,
960,
803,
961,
802,
963,
801,
959,
796,
957,
794,
952,
794
]
],
"area": 76.5,
"bbox": [
950,
794,
13,
9
]
},
{
"id": 606,
"iscrowd": 0,
"image_id": 43,
"category_id": 3,
"segmentation": [
[
632,
782,
628,
780,
619,
780,
607,
785,
601,
785,
597,
786,
596,
787,
594,
787,
591,
790,
588,
791,
581,
791,
569,
797,
558,
799,
552,
803,
541,
804,
523,
809,
515,
812,
510,
815,
501,
816,
495,
820,
485,
821,
476,
825,
470,
827,
459,
829,
456,
832,
447,
833,
435,
840,
427,
840,
420,
842,
418,
844,
403,
847,
398,
850,
390,
851,
373,
857,
368,
857,
356,
862,
345,
864,
327,
869,
315,
874,
307,
875,
297,
881,
300,
883,
310,
883,
318,
881,
321,
879,
336,
876,
341,
873,
381,
860,
388,
859,
410,
852,
421,
847,
427,
847,
434,
843,
451,
838,
453,
836,
460,
834,
467,
834,
472,
831,
492,
826,
500,
822,
514,
818,
524,
817,
536,
811,
544,
810,
550,
808,
556,
804,
579,
797,
583,
797,
594,
792,
601,
792,
609,
788,
622,
786
]
],
"area": 1939,
"bbox": [
297,
780,
335,
103
]
}
],
"images": [
{
"id": 762,
"width": 1920,
"height": 1080,
"file_name": "0762.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 179,
"width": 1920,
"height": 1080,
"file_name": "0179.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 13,
"width": 1920,
"height": 1080,
"file_name": "0013.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1871,
"width": 1920,
"height": 1080,
"file_name": "1871.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 396,
"width": 1920,
"height": 1080,
"file_name": "0396.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1762,
"width": 1920,
"height": 1080,
"file_name": "1762.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 89,
"width": 1920,
"height": 1080,
"file_name": "0089.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 2198,
"width": 1920,
"height": 1080,
"file_name": "2198.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 262,
"width": 1920,
"height": 1080,
"file_name": "0262.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1132,
"width": 1920,
"height": 1080,
"file_name": "1099.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 929,
"width": 1920,
"height": 1080,
"file_name": "0929.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1396,
"width": 1920,
"height": 1080,
"file_name": "1318.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 2208,
"width": 1920,
"height": 1080,
"file_name": "2208.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1248,
"width": 1920,
"height": 1080,
"file_name": "1248.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1909,
"width": 1920,
"height": 1080,
"file_name": "1909.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1487,
"width": 1920,
"height": 1080,
"file_name": "1492.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 341,
"width": 1920,
"height": 1080,
"file_name": "0341.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1851,
"width": 1920,
"height": 1080,
"file_name": "1851.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1742,
"width": 1920,
"height": 1080,
"file_name": "1742.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1957,
"width": 1920,
"height": 1080,
"file_name": "1957.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1418,
"width": 1920,
"height": 1080,
"file_name": "1418.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1592,
"width": 1920,
"height": 1080,
"file_name": "1592.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 19,
"width": 1920,
"height": 1080,
"file_name": "0019.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 579,
"width": 1920,
"height": 1080,
"file_name": "0579.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1407,
"width": 1920,
"height": 1080,
"file_name": "1372.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1975,
"width": 1920,
"height": 1080,
"file_name": "1975.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 931,
"width": 1920,
"height": 1080,
"file_name": "0931.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1977,
"width": 1920,
"height": 1080,
"file_name": "1977.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 40,
"width": 1920,
"height": 1080,
"file_name": "0040.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1366,
"width": 1920,
"height": 1080,
"file_name": "1403.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 889,
"width": 1920,
"height": 1080,
"file_name": "0889.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1050,
"width": 1920,
"height": 1080,
"file_name": "1050.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 669,
"width": 1920,
"height": 1080,
"file_name": "0669.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 2216,
"width": 1920,
"height": 1080,
"file_name": "2216.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 659,
"width": 1920,
"height": 1080,
"file_name": "0659.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 294,
"width": 1920,
"height": 1080,
"file_name": "0294.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1895,
"width": 1920,
"height": 1080,
"file_name": "1895.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 706,
"width": 1920,
"height": 1080,
"file_name": "0710.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1273,
"width": 1920,
"height": 1080,
"file_name": "1273.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 461,
"width": 1920,
"height": 1080,
"file_name": "0461.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1023,
"width": 1920,
"height": 1080,
"file_name": "1023.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1594,
"width": 1920,
"height": 1080,
"file_name": "1594.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1747,
"width": 1920,
"height": 1080,
"file_name": "1747.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1854,
"width": 1920,
"height": 1080,
"file_name": "1854.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 750,
"width": 1920,
"height": 1080,
"file_name": "0750.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1580,
"width": 1920,
"height": 1080,
"file_name": "1580.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1735,
"width": 1920,
"height": 1080,
"file_name": "1735.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1504,
"width": 1920,
"height": 1080,
"file_name": "1509.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1630,
"width": 1920,
"height": 1080,
"file_name": "1630.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 629,
"width": 1920,
"height": 1080,
"file_name": "0629.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 367,
"width": 1920,
"height": 1080,
"file_name": "0367.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1833,
"width": 1920,
"height": 1080,
"file_name": "1833.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 583,
"width": 1920,
"height": 1080,
"file_name": "0583.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 178,
"width": 1920,
"height": 1080,
"file_name": "0178.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 199,
"width": 1920,
"height": 1080,
"file_name": "0199.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1836,
"width": 1920,
"height": 1080,
"file_name": "1836.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1636,
"width": 1920,
"height": 1080,
"file_name": "1636.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 382,
"width": 1920,
"height": 1080,
"file_name": "0382.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 421,
"width": 1920,
"height": 1080,
"file_name": "0421.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1296,
"width": 1920,
"height": 1080,
"file_name": "1328.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 2054,
"width": 1920,
"height": 1080,
"file_name": "2054.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1072,
"width": 1920,
"height": 1080,
"file_name": "1095.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 601,
"width": 1920,
"height": 1080,
"file_name": "0601.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1313,
"width": 1920,
"height": 1080,
"file_name": "1345.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 726,
"width": 1920,
"height": 1080,
"file_name": "0704.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 924,
"width": 1920,
"height": 1080,
"file_name": "0924.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 139,
"width": 1920,
"height": 1080,
"file_name": "0139.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 445,
"width": 1920,
"height": 1080,
"file_name": "0445.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1436,
"width": 1920,
"height": 1080,
"file_name": "1441.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 1835,
"width": 1920,
"height": 1080,
"file_name": "1835.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 311,
"width": 1920,
"height": 1080,
"file_name": "0311.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 173,
"width": 1920,
"height": 1080,
"file_name": "0173.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
},
{
"id": 43,
"width": 1920,
"height": 1080,
"file_name": "0043.jpg",
"license": "",
"flickr_url": "",
"coco_url": "",
"date_captured": ""
}
],
"categories": [
{
"id": 1,
"name": "car",
"color": [
52,
101,
164
],
"supercategory": ""
},
{
"id": 2,
"name": "traffic_sign",
"color": [
245,
128,
6
],
"supercategory": ""
},
{
"id": 3,
"name": "lane_lines",
"color": [
115,
210,
22
],
"supercategory": ""
},
{
"id": 4,
"name": "person",
"color": [
239,
41,
41
],
"supercategory": ""
},
{
"id": 5,
"name": "motorcyclist",
"color": [
145,
104,
190
],
"supercategory": ""
},
{
"id": 6,
"name": "cyclist",
"color": [
239,
41,
41
],
"supercategory": ""
}
]
}
格式转换代码,内容如下
import os
import json
import shutil
def write_yolo_txt_file(txt_file_path,label_seg_x_y_list):
if not os.path.exists(txt_file_path):
with open(txt_file_path, "w") as file:
for element in label_seg_x_y_list:
file.write(str(element) + " ")
file.write('\n')
else:
with open(txt_file_path, "a") as file:
for element in label_seg_x_y_list:
file.write(str(element) + " ")
file.write('\n')
def read_json(in_json_path,img_dir,target_dir):
with open(in_json_path, "r", encoding='utf-8') as f:
# json.load数据到变量json_data
json_data = json.load(f)
# print(len(json_data['annotations']))
# print(len(json_data['images']))
# print(len(json_data['categories']))
for annotation in json_data['annotations']: # 遍历标注数据信息
# print(annotation)
category_id = annotation['category_id']
image_id = annotation['image_id']
for image in json_data['images']: # 遍历图片相关信息
if image['id'] == image_id:
width = image['width'] # 图片宽
height = image['height'] # 图片高
img_file_name = image['file_name'] # 图片名称
txt_file_name = image['file_name'].split('.')[0] + '.txt' # 要保存的对应txt文件名
break
# print(width,height,img_file_name,txt_file_name)
segmentation = annotation['segmentation'] # 图像分割点信息[[x1,y1,x2,y2,...,xn,yn]]
seg_x_y_list = [i/width if num%2==0 else i/height for num,i in enumerate(segmentation[0])] # 归一化图像分割点信息
label_seg_x_y_list = seg_x_y_list[:]
label_seg_x_y_list.insert(0,category_id) # 图像类别与分割点信息[label,x1,y1,x2,y2,...,xn,yn]
# print(label_seg_x_y_list)
# 写txt文件
txt_file_path = target_dir + txt_file_name
# print(txt_file_path)
write_yolo_txt_file(txt_file_path,label_seg_x_y_list)
# 选出txt对应img文件
img_file_path = img_dir + img_file_name
# print(img_file_path)
shutil.copy(img_file_path,target_dir)
if __name__=="__main__":
img_dir = 'JPEGImages/'
target_dir = 'testset/'
if not os.path.exists(target_dir):
os.mkdir(target_dir)
in_json_path = './test.json'
read_json(in_json_path,img_dir,target_dir)