Create a dataset of type MS COCO

How to create a MS COCO style dataset for use with TensorFlow? Does anyone have any experience with this? I have images and annotations, as well as ground truth masks. I need to convert them so that they are compatible with MS COCO, and any help is appreciated. I can't find an open source tool for creating COCO style JSON annotations.

TensorFlow MS COCO reads JSON files that I am not very good at.

+5
source share
4 answers

To convert an array of masks from 0 and 1 to a polygon similar to a COCO style dataset, use skimage.measure.find_contours thanks to the waleedka code .

import numpy
from skimage.measure import find_contours 

mask = numpy.zeros(width, height) # Mask
mask_polygons = [] # Mask Polygons

# Pad to ensure proper polygons for masks that touch image edges.
padded_mask = np.zeros(
(mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8)
padded_mask[1:-1, 1:-1] = mask
contours = find_contours(padded_mask, 0.5)
for verts in contours:
    # Subtract the padding and flip (y, x) to (x, y)
    verts = np.fliplr(verts) - 1
    pat = PatchCollection([Polygon(verts, closed=True)], facecolor='green', linewidths=0, alpha=0.6)
    mask_polygons.append(pat) 

To generate a JSON file for a COCO style dataset, you should learn the Python JSON API . Other than that, it just matches the format used by the COCO dataset JSON file.

You should take a look at my COCO Code Set GUI Code Repository . I created a very simple tool for creating COCO style datasets.

, , create_json_file.py, matplotlib (x1, y1, x2, y2...) JSON, COCO .

+3

Python, . .

COCO:

from imantics import Mask, Image, Category

image = Image.from_path('path/to/image.png')
mask = Mask(mask_array)
image.add(mask, category=Category("Category Name"))

# dict of coco
coco_json = image.export(style='coco')
# Saves to file
image.save('coco/annotation.json', style='coco')
+2

pycococreator, RLE, COCO.

https://github.com/waspinator/pycococreator/

, :

annotation_info = pycococreatortools.create_annotation_info(
                    segmentation_id, image_id, category_info, binary_mask,
                    image.size, tolerance=2)

, pycococreator, : https://patrickwasp.com/create-your-own-coco-style-dataset/

0

COCO STYLE API

, ( ).

: ann.json

{"images":[{"id": 73}],"annotations":[{"image_id":73,"category_id":1,"bbox":[10,10,50,100],"id":1,"iscrowd": 0,"area": 10}],"categories": [{"id": 1, "name": "person"}, {"id": 2, "name": "bicycle"}, {"id": 3, "name": "car"}]}

: res.json

[{"image_id":73,"category_id":1,"bbox":[10,10,50,100],"score":0.9}]

COCO:

from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval

annFile = './ann.json'
resFile='./res.json'

cocoGt=COCO(annFile)

cocoDt=cocoGt.loadRes(resFile)

annType = 'bbox'
cocoEval = COCOeval(cocoGt,cocoDt,annType)
cocoEval.evaluate()
cocoEval.accumulate()
cocoEval.summarize()
0

Source: https://habr.com/ru/post/1683164/


All Articles