COCO 2 mask

文章概述了一个Python脚本,用于从COCOJSON文件中提取图像,生成并保存每个实例的掩码。
摘要由CSDN通过智能技术生成
import os
import numpy as np
import pycocotools.mask as mask_utils
from pycocotools.coco import COCO
import cv2
from tqdm import tqdm
 
# 根据自己情况设置图片、json以及mask保存路径
img_path = ''
json_path = ''
save_mask = ''
 
os.makedirs(save_mask, exist_ok=True)
 
# 通过pycocotools加载json文件
coco = COCO(json_path)
 
# 获取json中的图像id
images_ids = coco.getImgIds()
 
# 逐图处理
for img_id in tqdm(images_ids):
    # 根据图像唯一id获取对应信息
    img_info = coco.loadImgs(img_id)[0]
    img_files_path = os.path.join(img_path, img_info['file_name'])
    height, width = img_info['height'], img_info['width'] 
   
    # 根据图像id获取对应标签信息
    ann_ids = coco.getAnnIds(imgIds=img_id)    
    anns = coco.loadAnns(ann_ids)
    
    # 逐实例处理(一个图像存在多个实例)
    for ann in anns:
        rle = coco.annToRLE(ann)
        mask = mask_utils.decode(rle)
        mask[mask == 1] = 255   # 调整mask中的像素值
 
        # 保存mask
        mask_name = img_info['file_name'].replace('.jpg', f'_{ann['category_id']}.jpg')
        cv2.imwrite(os.path.join(save_mask, mask_name), mask)

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先使用labelme将图像标注为json文件,然后将这些json文件转换为COCO格式,具体步骤如下: 1. 使用labelme标注并保存图片,得到对应的json标注文件。 2. 安装labelme2coco库,可以使用pip install labelme2coco进行安装。 3. 在Python代码中导入必要的库和函数: ``` import json import os import numpy as np from labelme2coco import labelme2coco ``` 4. 定义函数将所有的labelme json文件转换为COCO格式的json文件: ``` def labelme2cocoDir(labelme_dir, coco_path): # labelme_dir:包含所有labelme json标注文件的目录 # coco_path:生成的COCO格式json文件路径 info = { "description": "dataset_name", "url": "https://github.com/me", "version": "0.1.0", "year": 2021, "contributor": "me", "date_created": "2021/01/01" } categories = [] category_index = {} # 遍历labelme json文件,提取类别信息 for filename in os.listdir(labelme_dir): if filename.endswith(".json"): with open(os.path.join(labelme_dir, filename), "r") as f: labelme_json = json.load(f) for shape in labelme_json["shapes"]: label = shape["label"] if label not in category_index: category_index[label] = len(categories) categories.append({"id": category_index[label], "name": label}) # 将labelme json文件转换为COCO格式json文件 coco_dict = labelme2coco(labelme_dir) coco_dict["info"] = info coco_dict["categories"] = categories # 写入COCO格式json文件 with open(coco_path, "w") as f: json.dump(coco_dict, f) ``` 5. 调用以上函数将所有的labelme json文件转换为COCO格式json文件,执行以下代码即可: ``` labelme_dir = "path/to/labelme/json/files" coco_path = "path/to/coco/json/file" labelme2cocoDir(labelme_dir, coco_path) ``` 一些注意事项: 1. 在labelme中标注的类别名称需要保持一致,否则转换后的COCO格式json文件中会有重复的类别; 2. 如果只有单个labelme json文件,可以使用以下代码将其转换为COCO格式json文件: ``` labelme_json = "path/to/labelme/json/file" coco_path = "path/to/coco/json/file" labelme_dict = json.load(open(labelme_json)) coco_dict = labelme2coco(labelme_dict) with open(coco_path, 'w') as f: json.dump(coco_dict, f) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值