coco实例标注文件拆分为单张图片Labelme标注格式文件
程序是在CHATGPT基础上自己改的
用了SAM+SAM tool快速标注工具进行了标注,但生成的ann.json文件是所有文件标注信息的集合,有些标注错的地方也不方便修改。多批次标注结果想放在一起打包也比较麻烦,就想着把ann文件合集再拆成一张一张的标注结果。
拆分代码如下:
import json
import os
def coco_to_labelme(coco_file, output_dir):
with open(coco_file, 'r') as f:
data = json.load(f)
images = data['images']
annotations = data['annotations']
categories = {category['id']: category['name'] for category in data['categories']}
for image in images:
image_id = image['id']
image_file = image['file_name']
dir, image_file_1 = image['file_name'].rsplit('\\', 1)
image_width = image['width']
image_height = image['height']
labelme_data = {
"version": "5.0.1",
"flags": {},
"shapes": [],
"imagePath": image_file_1,
"imageData": None,
"imageHeight": image_height,
"imageWidth": image_width
}
for annotation in annotations:
if annotation['image_id'] == image_id:
category_id = annotation['category_id']
category_name = categories[category_id]
bbox = annotation['bbox']
segmentation = annotation['segmentation'][0]
# Convert segmentation to polygon format
polygon = []
for i in range(0, len(segmentation), 2):
x = segmentation[i]
y = segmentation[i + 1]
polygon.append([x, y])
shape_data = {
"label": category_name,
"points": polygon,
"group_id": None,
"shape_type": "polygon",
"flags": {}
}
labelme_data['shapes'].append(shape_data)
image_name = os.path.splitext(os.path.basename(image_file))[0]
labelme_output_file = os.path.join(output_dir, image_name + '.json')
with open(labelme_output_file, 'w') as f:
json.dump(labelme_data, f, indent=4)
print(f"Converted {image_file} to {labelme_output_file}")
# 使用示例
coco_file = r'D:\Deep Learning\SAM\SAM-Tool-main\split\annotations.json'
output_dir = r'D:\Deep Learning\SAM\SAM-Tool-main\split\labelme_annotations'
coco_to_labelme(coco_file, output_dir)
CHATGPT真方便