簡介
COCO數據集現在有3種標註類型:object instances(目標實例), object keypoints(目標上的關鍵點), and image captions(看圖說話),使用JSON文件存儲。
格式結構
{
"info": info,
"licenses": [license],
"images": [image], #劃入訓練集(或者測試集)的圖片的數量;
"annotations": [annotation], #訓練集(或者測試集)中bounding box的數量
"categories": [category] #類別的數量
}
image
每一個image的實例是一個dict。其中有一個id字段,代表的是圖片的id,每一張圖片具有唯一的一個獨特的id。
annotations
annotations字段是包含多個annotation實例的一個數組,annotation類型本身又包含了一系列的字段,如這個目標的category id和segmentation mask。segmentation格式取決於這個實例是一個單個的對象(即iscrowd=0,將使用polygons格式)還是一組對象(即iscrowd=1,將使用RLE格式)
annotation{
"id": int,
"image_id": int, #圖片序列
"category_id": int,
"segmentation": RLE or [polygon],
"area": float,
"bbox": [x,y,width,height],
"iscrowd": 0 or 1, #是否爲polygon
}
{
"segmentation": [[510.66,423.01,511.72,420.03,510.45......]],
"area": 702.1057499999998,
"iscrowd": 0,
"image_id": 289343,
"bbox": [473.07,395.93,38.65,28.67],
"category_id": 18,
"id": 1768
},
另外,每個對象(不管是iscrowd=0還是iscrowd=1)都會有一個矩形框bbox ,矩形框左上角的座標和矩形框的長寬會以數組的形式提供,數組第一個元素就是左上角的橫座標值。
area是area是area of encoded masks,是標註區域的面積。如果是矩形框,那就是高乘寬;如果是polygon或者RLE,那就複雜點。
最後,annotation結構中的categories字段存儲的是當前對象所屬的category的id,以及所屬的supercategory的name。
polygon格式mask
polygon格式比較簡單,這些數按照相鄰的順序兩兩組成一個點的xy座標,如果有n個數(必定是偶數),那麼就是n/2個點座標。下面就是一段解析polygon格式的segmentation並且顯示多邊形的示例代碼:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
fig, ax = plt.subplots()
polygons = []
num_sides = 100
gemfield_polygons = [[125.12, 539.69, 140.94, 522.43......]]
gemfield_polygon = gemfield_polygons[0]
max_value = max(gemfield_polygon) * 1.3
gemfield_polygon = [i * 1.0/max_value for i in gemfield_polygon]
poly = np.array(gemfield_polygon).reshape((int(len(gemfield_polygon)/2), 2))
polygons.append(Polygon(poly,True))
p = PatchCollection(polygons, cmap=matplotlib.cm.jet, alpha=0.4)
colors = 100*np.random.rand(1)
p.set_array(np.array(colors))
ax.add_collection(p)
plt.show()
categories
```python
{
"supercategory": "person",
"id": 1,
"name": "person"
},
{
"supercategory": "vehicle",
"id": 2,
"name": "bicycle"
},
參考文獻
知乎推薦【鏈接】
製作自己數據集【鏈接】
文本檢測數據集轉換成COCO【github鏈接】