最近再做有关Mask R-CNN有关的项目,需要将标注好的json文件批量转换为coco格式用于Mask R-CNN的训练 。
下面的代码可以参考一下,直接在pycharm上运行即可。
import os
import json
import glob
def polygon_to_bbox(polygon):
# 提取多边形所有点的 x 和 y 坐标
x_coords = [point[0] for point in polygon]
y_coords = [point[1] for point in polygon]
# 计算最小和最大 x、y 坐标
x_min = min(x_coords)
x_max = max(x_coords)
y_min = min(y_coords)
y_max = max(y_coords)
# 计算边界框的宽度和高度
width = x_max - x_min
height = y_max - y_min
# 返回边界框 [x_min, y_min, width, height]
return [x_min, y_min, width, height]
def convert_to_coco(input_folder, output_file):
coco_data = {
"images": [],
"annotations": [],
"categories": [],
}
category_map = {}
annotation_id = 1
image_id = 1
categories = ['tree'] # 假设只有一个类别,在json文件里定义的类别名字
for i, category in enumerate(categories):
coco_data['categories'].append({
"id": i + 1,
"name": category,
"supercategory": "none"
})
category_map[category] = i + 1
# 获取所有json文件
json_files = glob.glob(os.path.join(input_folder, "*.json"))
# 处理每个json文件
for json_file in json_files:
with open(json_file, 'r') as f:
content = f.read().replace('\\', '\\\\') # 修复反斜杠问题
try:
data = json.loads(content) # 使用 json.loads 来解析修复后的内容
print(data.keys()) # 打印出所有的键,查看是否正常加载
except json.JSONDecodeError as e:
print(f"Error parsing {json_file}: {e}")
continue # 如果有解析错误,跳过当前文件
# 假设图像路径字段是 'imagePath',如果实际字段不同,修改成正确的字段名
image_info = {
"id": image_id,
"file_name": data["imagePath"], # 使用 imagePath 字段
"width": data["imageWidth"],
"height": data["imageHeight"]
}
coco_data["images"].append(image_info)
for ann in data["shapes"]:
bbox = polygon_to_bbox(ann["points"])
annotation_info = {
"id": annotation_id,
"image_id": image_id,
"category_id": category_map[ann["label"]],
"bbox": bbox,
"area": bbox[2] * bbox[3], # 宽 * 高
"iscrowd": 0
}
coco_data["annotations"].append(annotation_info)
annotation_id += 1
image_id += 1
# 确保输出文件名带有 .json 后缀
if not output_file.endswith(".json"):
output_file += ".json"
# 输出到文件
with open(output_file, 'w') as f:
json.dump(coco_data, f, indent=4)
# 使用示例
input_folder = 'path/to/your/json/folder' # 这里是你的json文件夹路径
output_file = 'path/to/your/json/folder' # 这里是输出文件路径(没有 .json 后缀)
convert_to_coco(input_folder, output_file)