【亲测有用】使用Image和Mask图像转化成COCO数据格式

方法1: 轮廓处理

import json
import numpy as np
from pycocotools import mask
from skimage import measure
import cv2
import os
import sys
if sys.version_info[0] >= 3:
    unicode = str
__author__ = 'hcaesar'
import io
def maskToanno(ground_truth_binary_mask,ann_count,category_id):
    fortran_ground_truth_binary_mask = np.asfortranarray(ground_truth_binary_mask)
    encoded_ground_truth = mask.encode(fortran_ground_truth_binary_mask)
    ground_truth_area = mask.area(encoded_ground_truth)
    ground_truth_bounding_box = mask.toBbox(encoded_ground_truth)
    contours = measure.find_contours(ground_truth_binary_mask, 0.5)

    annotation = {
        "segmentation": [],
        "area": ground_truth_area.tolist(),
        "iscrowd": 0,
        "image_id": ann_count,
        "bbox": ground_truth_bounding_box.tolist(),
        "category_id": category_id,
        "id": ann_count
    }
    for contour in contours:
        contour = np.flip(contour, axis=1)
        segmentation = contour.ravel().tolist()
        annotation["segmentation"].append(segmentation)
    return annotation
block_mask_path="D:\\6Ddataset\\XieSegmentation\\1block\\block_mask_thresh"
mouse_mask_path="D:\\6Ddataset\\XieSegmentation\\2mouse\\mouse_mask_thresh"
block_mask_image_files=os.listdir(block_mask_path)
mouse_mask_image_files=os.listdir(mouse_mask_path)
jsonPath="blockmouseAll.json"
annCount=0
imageCount=0
path="D:\\6Ddataset\\XieSegmentation\\1block\\block_rgb"
rgb_image_files=os.listdir(path)
with io.open(jsonPath, 'w', encoding='utf8') as output:
    # 那就全部写在一个文件夹好了
    # 先写images的信息
    output.write(unicode('{\n'))
    output.write(unicode('"images": [\n'))
    for image in rgb_image_files:
        output.write(unicode('{'))
        annotation = {
            "height": 480,
            "width": 640,
            "id": imageCount,
            "file_name": image
        }
        str_ = json.dumps(annotation,indent=4)
        str_ = str_[1:-1]
        if len(str_) > 0:
            output.write(unicode(str_))
            imageCount = imageCount + 1
        if(image=="0000000599_rgb.png"):
            output.write(unicode('}\n'))
        else:
            output.write(unicode('},\n'))
    output.write(unicode('],\n'))
    #接下来写cate
    output.write(unicode('"categories": [\n'))
    output.write(unicode('{\n'))
    categories={
        "supercategory": "block",
        "id": 0,
        "name": "block"
    }
    str_ = json.dumps(categories, indent=4)
    str_ = str_[1:-1]
    if len(str_) > 0:
        output.write(unicode(str_))
    output.write(unicode('},\n'))
    output.write(unicode('{\n'))
    categories={
        "supercategory": "mouse",
        "id": 1,
        "name": "mouse"
    }
    str_ = json.dumps(categories, indent=4)
    str_ = str_[1:-1]
    if len(str_) > 0:
        output.write(unicode(str_))
    output.write(unicode('}\n'))
    output.write(unicode('],\n'))
    output.write(unicode('"annotations": [\n'))
    for i in range(len(block_mask_image_files)):
    #for (block_image,mouse_image) in (block_mask_image_files,mouse_mask_image_files):
        block_image=block_mask_image_files[i]
        mouse_image=mouse_mask_image_files[i]
        #output.write(unicode('{\n'))
        block_im=cv2.imread(os.path.join(block_mask_path,block_image),0)
        mouse_im = cv2.imread(os.path.join(mouse_mask_path, mouse_image), 0)
        _,block_im=cv2.threshold(block_im,100,1,cv2.THRESH_BINARY)
        _, mouse_im = cv2.threshold(mouse_im, 100, 1, cv2.THRESH_BINARY)
        block_im=np.array(block_im).astype(np.uint8)
        mouse_im = np.array(mouse_im).astype(np.uint8)
        block_anno=maskToanno(block_im,annCount,0)
        mouse_anno = maskToanno(mouse_im, annCount,1)
        str_block = json.dumps(block_anno,indent=4)
        str_block = str_block[1:-1]
        str_mouse = json.dumps(mouse_anno,indent=4)
        str_mouse = str_mouse[1:-1]
        if len(str_block) > 0:
            output.write(unicode('{\n'))
            output.write(unicode(str_block))
            output.write(unicode('},\n'))
            output.write(unicode('{\n'))
            output.write(unicode(str_mouse))
            if (block_image == "0000000599_labels.png"):
                output.write(unicode('}\n'))
            else:
                output.write(unicode('},\n'))
            #output.write(unicode('},\n'))
            annCount = annCount + 1
    output.write(unicode(']\n'))
    output.write(unicode('}\n'))
        #output.write(unicode('},\n'))
#cate

方法2-利用pycococreator

import cv2
import numpy as np
import os, glob
 
 
def rgb2masks(label_name):
    lbl_id = os.path.split(label_name)[-1].split('.')[0]
    lbl = cv2.imread(label_name, 1)
    h, w = lbl.shape[:2]
    leaf_dict = {}
    idx = 0
    white_mask = np.ones((h, w, 3), dtype=np.uint8) * 255
    for i in range(h):
        for j in range(w):
            if tuple(lbl[i][j]) in leaf_dict or tuple(lbl[i][j]) == (0, 0, 0):
                continue
            leaf_dict[tuple(lbl[i][j])] = idx
            mask = (lbl == lbl[i][j]).all(-1)
            # leaf = lbl * mask[..., None]      # colorful leaf with black background
            # np.repeat(mask[...,None],3,axis=2)    # 3D mask
            leaf = np.where(mask[..., None], white_mask, 0)
            mask_name = './shapes/train/annotations/' + lbl_id + '_leaf_' + str(idx) + '.png'
            cv2.imwrite(mask_name, leaf)
            idx += 1
 
 
label_dir = './labels'
label_list = glob.glob(os.path.join(label_dir, '*.png'))
for label_name in label_list:
    rgb2masks(label_name)

利用pycococreator和得到的黑白masks生成coco json格式的数据集,代码如下:

import datetime
import json
import os
import re
import fnmatch
from PIL import Image
import numpy as np
from pycococreatortools import pycococreatortools
 
 
ROOT_DIR = './shapes/train'
IMAGE_DIR = os.path.join(ROOT_DIR, "shapes_train2017")
ANNOTATION_DIR = os.path.join(ROOT_DIR, "annotations")
 
INFO = {
    "description": "Leaf Dataset",
    "url": "https://github.com/waspinator/pycococreator",
    "version": "0.1.0",
    "year": 2017,
    "contributor": "Francis_Liu",
    "date_created": datetime.datetime.utcnow().isoformat(' ')
}
 
LICENSES = [
    {
        "id": 1,
        "name": "Attribution-NonCommercial-ShareAlike License",
        "url": "http://creativecommons.org/licenses/by-nc-sa/2.0/"
    }
]
 
# 根据自己的需要添加种类
CATEGORIES = [
    {
        'id': 1,
        'name': 'leaf',
        'supercategory': 'leaf',
    }
]
 
 
def filter_for_jpeg(root, files):
    file_types = ['*.jpeg', '*.jpg', '*.png']
    file_types = r'|'.join([fnmatch.translate(x) for x in file_types])
    files = [os.path.join(root, f) for f in files]
    files = [f for f in files if re.match(file_types, f)]
    return files
 
 
def filter_for_annotations(root, files, image_filename):
    file_types = ['*.png']
    file_types = r'|'.join([fnmatch.translate(x) for x in file_types])
    basename_no_extension = os.path.splitext(os.path.basename(image_filename))[0]
    file_name_prefix = basename_no_extension + '.*'
    files = [os.path.join(root, f) for f in files]
    files = [f for f in files if re.match(file_types, f)]
    files = [f for f in files if re.match(file_name_prefix, os.path.splitext(os.path.basename(f))[0])]
    return files
 
 
def main():
    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }
 
    image_id = 1
    segmentation_id = 1
 
    # filter for jpeg images
    for root, _, files in os.walk(IMAGE_DIR):
        image_files = filter_for_jpeg(root, files)
 
        # go through each image
        for image_filename in image_files:
            image = Image.open(image_filename)
            image_info = pycococreatortools.create_image_info(
                    image_id, os.path.basename(image_filename), image.size)
            coco_output["images"].append(image_info)
 
            # filter for associated png annotations
            for root, _, files in os.walk(ANNOTATION_DIR):
                annotation_files = filter_for_annotations(root, files, image_filename)
 
                # go through each associated annotation
                for annotation_filename in annotation_files:
 
                    print(annotation_filename)
                    class_id = [x['id'] for x in CATEGORIES if x['name'] in annotation_filename][0]
 
                    category_info = {'id': class_id, 'is_crowd': 'crowd' in image_filename}
                    binary_mask = np.asarray(Image.open(annotation_filename)
                                             .convert('1')).astype(np.uint8)
 
                    annotation_info = pycococreatortools.create_annotation_info(
                            segmentation_id, image_id, category_info, binary_mask,
                            image.size, tolerance=2)
 
                    if annotation_info is not None:
                        coco_output["annotations"].append(annotation_info)
 
                    segmentation_id = segmentation_id + 1
 
            image_id = image_id + 1
 
    with open('{}/instances_leaf_train2017.json'.format(ROOT_DIR), 'w') as output_json_file:
        json.dump(coco_output, output_json_file)
 
 
if __name__ == "__main__":
    main()
  • 2
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将VOC数据集格式转换为COCO数据集格式,可以按照以下步骤进行操作: 1. 安装Python COCO API库 COCO API库可以帮助我们处理COCO数据集格式。可以使用以下命令安装COCO API库: ``` pip install pycocotools ``` 2. 编写转换脚本 可以编写Python脚本来将VOC数据集格式转换为COCO数据集格式。下面是一个简单的脚本示例: ``` import os import json from xml.etree import ElementTree as ET from collections import defaultdict def parse_xml(xml_file): tree = ET.parse(xml_file) root = tree.getroot() size = root.find('size') width = int(size.find('width').text) height = int(size.find('height').text) objects = [] for obj in root.findall('object'): name = obj.find('name').text bbox = obj.find('bndbox') xmin = int(bbox.find('xmin').text) ymin = int(bbox.find('ymin').text) xmax = int(bbox.find('xmax').text) ymax = int(bbox.find('ymax').text) objects.append({'name': name, 'bbox': [xmin, ymin, xmax-xmin, ymax-ymin]}) return {'width': width, 'height': height, 'objects': objects} def convert_voc_to_coco(voc_dir, coco_file): images = [] annotations = [] categories = defaultdict(dict) category_id = 1 annotation_id = 1 for root, _, files in os.walk(voc_dir): for file in files: if file.endswith('.xml'): xml_file = os.path.join(root, file) image_file = os.path.splitext(xml_file)[0] + '.jpg' image_id = len(images) + 1 image = {'id': image_id, 'file_name': image_file} images.append(image) data = parse_xml(xml_file) for obj in data['objects']: category_name = obj['name'] if category_name not in categories: categories[category_name] = {'id': category_id, 'name': category_name} category_id += 1 category = categories[category_name] annotation = {'id': annotation_id, 'image_id': image_id, 'category_id': category['id'], 'bbox': obj['bbox']} annotations.append(annotation) annotation_id += 1 coco_data = {'images': images, 'annotations': annotations, 'categories': list(categories.values())} with open(coco_file, 'w') as f: json.dump(coco_data, f) if __name__ == '__main__': voc_dir = 'path/to/voc' coco_file = 'path/to/coco.json' convert_voc_to_coco(voc_dir, coco_file) ``` 在这个脚本中,我们使用`parse_xml`函数从VOC格式的XML文件中解析出图像和目标位置信息,然后使用`convert_voc_to_coco`函数将整个VOC数据集转换为COCO格式的JSON文件。 3. 运行脚本 将VOC格式数据集放置在指定的目录下,然后运行上面的Python脚本即可将VOC数据集格式转换为COCO数据集格式。 ``` python voc_to_coco.py ``` 转换后的COCO格式的JSON文件将保存在指定的位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值