coco数据集转换SAM2格式
coco是一个大json汇总了所有train的标签
SAM2训练一张图对应一个json标签
import json
import os
from pycocotools import mask as mask_utils
import numpy as np
import cv2def poly2mask(points, width, height):points_array = np.array(points, dtype=np.int32).reshape(-1, 2)mask = np.zeros((height, width), dtype=np.uint8) # 注意顺序是(height, width)cv2.fillPoly(mask, [points_array], 255) # 填充多边形区域为255return mask2rle(mask)def mask2rle(mask):"""将二值化掩码转换为RLE编码"""rle = mask_utils.encode