文章目录[隐藏]
import os
from pycocotools.coco import COCO
import matplotlib.pyplot as plt
%matplotlib inline
%config Completer.use_jedi = False
os.chdir("../../数据/coco/annotations_trainval2017/")
创建 coco 对象
coco = COCO("./annotations/person_keypoints_train2017.json")
loading annotations into memory...
Done (t=10.07s)
creating index...
index created!
os.listdir()
['annotations']
操作 annotations
- instances 是用来实例分割的 annotations
- person_keypoints 则是用来进行关键点训练的 annotations
os.listdir("./annotations/")
['captions_train2017.json',
'captions_val2017.json',
'instances_train2017.json',
'instances_val2017.json',
'person_keypoints_train2017.json',
'person_keypoints_val2017.json']
重要属性
-
self.dataset
-
self.anns
-
self.cats
-
self.imgs
-
从 COCO 类的定义可以看出,dataset, anns, cats, imgs 都是字典,因此我们可以通过 keys 来看他们的结构
coco.dataset.keys()
dict_keys(['info', 'licenses', 'images', 'annotations', 'categories'])
coco.dataset["categories"]
[{'supercategory': 'person',
'id': 1,
'name': 'person',
'keypoints': ['nose',
'left_eye',
'right_eye',
'left_ear',
'right_ear',
'left_shoulder',
'right_shoulder',
'left_elbow',
'right_elbow',
'left_wrist',
'right_wrist',
'left_hip',
'right_hip',
'left_knee',
'right_knee',
'left_ankle',
'right_ankle'],
'skeleton': [[16, 14],
[14, 12],
[17, 15],
[15, 13],
[12, 13],
[6, 12],
[7, 13],
[6, 7],
[6, 8],
[7, 9],
[8, 10],
[9, 11],
[2, 3],
[1, 2],
[1, 3],
[2, 4],
[3, 5],
[4, 6],
[5, 7]]}]
coco.anns.keys()
coco.cats.keys()
dict_keys([1])
coco.imgs.keys()
重要方法
- coco.getAnnIds()
- coco.getCatIds()
- coco.getImgIds()
这三种方法最终都是返回一个确定的 id
getAnnIds()
- 可以看到在这个方法中的两个参数是 catID 和 imgID
- 这个方法实际上是根据 imageID 和 categoryID 来返回唯一确定 annotationID
- 同时从下面的两个例子中可以看到,有的 imgID 里面是存在多个 annotationID 的;这就是一张 img 中有多个 person 的情况啦
coco.getAnnIds(imgIds=[522418],catIds=[],areaRng=[],iscrowd=False)
[455475]
coco.getAnnIds(imgIds=[391895],catIds=[],areaRng=[],iscrowd=False)
[202758, 1260346]
imgID = 391895
imgInfo = coco.loadImgs(391895)
imgInfo
[{'license': 3,
'file_name': '000000391895.jpg',
'coco_url': 'http://images.cocodataset.org/train2017/000000391895.jpg',
'height': 360,
'width': 640,
'date_captured': '2013-11-14 11:18:45',
'flickr_url': 'http://farm9.staticflickr.com/8186/8119368305_4e622c8349_z.jpg',
'id': 391895}]
img = plt.imread("../images/train2017/%s"%imgInfo[0]["file_name"])
plt.imshow(img)
<matplotlib.image.AxesImage at 0x15aa8355128>
annoInfo = coco.loadAnns([202758, 1260346])
annoInfo
[{'segmentation': [[352.55,
146.82,
353.61,
137.66,
356.07,
112.66,
357.13,
94.7,
357.13,
84.49,
363.12,
73.92,
370.16,
68.64,
370.16,
66.53,
368.4,
63.71,
368.05,
54.56,
361,
53.85,
356.07,
50.33,
356.43,
46.46,
364.17,
42.23,
369.1,
35.89,
371.22,
30.96,
376.85,
26.39,
383.54,
22.16,
391.29,
23.22,
400.79,
27.79,
402.2,
30.61,
404.32,
34.84,
406.08,
38.71,
406.08,
41.53,
406.08,
47.87,
407.84,
54.91,
408.89,
59.84,
408.89,
61.25,
408.89,
63.36,
422.28,
67.94,
432.13,
72.52,
445.87,
81.32,
446.57,
84.14,
446.57,
99.2,
451.15,
118.22,
453.26,
128.39,
453.61,
131.92,
453.61,
133.68,
451.5,
137.55,
451.5,
139.31,
455.38,
144.24,
455.38,
153.04,
455.73,
155.16,
461.01,
162.85,
462.07,
166.37,
459.95,
170.6,
459.6,
176.58,
459.95,
178.69,
459.95,
180.1,
448.33,
180.45,
447.98,
177.64,
446.57,
172.36,
447.63,
166.37,
449.74,
160.38,
450.09,
157.57,
448.68,
152.28,
445.16,
147.71,
441.29,
143.48,
435.66,
142.78,
428.26,
141.37,
420.87,
141.37,
418.75,
141.37,
411.71,
144.19,
404.32,
145.24,
396.57,
150.52,
395.87,
152.64,
391.29,
157.92,
391.99,
164.26,
389.53,
172,
389.53,
176.23,
376.85,
174.82,
375.09,
177.29,
374.03,
188.55,
381.08,
192.78,
384.6,
194.19,
384.95,
198.41,
383.19,
203.34,
380.02,
210.03,
378.61,
218.84,
375.79,
220.95,
373.68,
223.42,
368.05,
245.56,
368.05,
256.48,
368.05,
259.3,
360.65,
261.06,
361.71,
266.34,
361.36,
268.8,
358.19,
271.62,
353.26,
274.09,
349.74,
275.49,
341.28,
273.03,
339.88,
270.21,
343.05,
263.52,
347.62,
259.65,
351.5,
253.31,
352.9,
250.84,
356.07,
244.86,
359.24,
235.35,
357.83,
214.58,
357.13,
204.36,
358.89,
196.97,
361.71,
183.94,
365.93,
175.14,
371.92,
169.15,
376.15,
164.22,
377.2,
160.35,
378.61,
151.9,
377.55,
145.56,
375.79,
131.82,
375.09,
131.82,
373.33,
139.22,
370.16,
143.8,
369.1,
148.02,
365.93,
155.42,
361,
158.59,
358.89,
159.99,
358.89,
161.76,
361.71,
163.87,
363.12,
165.98,
363.12,
168.8,
362.06,
170.21,
360.3,
170.56,
358.54,
170.56,
355.02,
168.45,
352.2,
163.52,
351.14,
161.05,
351.14,
156.83,
352.2,
154.36,
353.26,
152.25,
353.61,
152.25,
353.26,
149.43],
[450.45,
196.54,
461.71,
195.13,
466.29,
209.22,
469.11,
227.88,
475.09,
241.62,
479.32,
249.01,
482.49,
262.04,
482.84,
279.96,
485.66,
303.87,
492.7,
307.04,
493.76,
309.5,
491.29,
318.66,
490.59,
321.83,
485.66,
322.89,
480.02,
322.89,
475.45,
317.96,
474.74,
310.91,
470.87,
304.57,
470.87,
294.71,
467.7,
282.34,
463.47,
276.7,
461.71,
272.83,
459.25,
270.01,
454.32,
268.25,
450.09,
259.82,
450.09,
252.07,
445.52,
234.11,
449.04,
229.57,
448.33,
199.29]],
'num_keypoints': 14,
'area': 14107.2713,
'iscrowd': 0,
'keypoints': [368,
61,
1,
369,
52,
2,
0,
0,
0,
382,
48,
2,
0,
0,
0,
368,
84,
2,
435,
81,
2,
362,
125,
2,
446,
125,
2,
360,
153,
2,
0,
0,
0,
397,
167,
1,
439,
166,
1,
369,
193,
2,
461,
234,
2,
361,
246,
2,
474,
287,
2],
'image_id': 391895,
'bbox': [339.88, 22.16, 153.88, 300.73],
'category_id': 1,
'id': 202758},
{'segmentation': [[477.41,
217.71,
475.06,
212.15,
473.78,
208.95,
473.78,
203.39,
473.78,
200.4,
473.35,
196.76,
472.07,
192.49,
471.64,
189.49,
471.64,
186.71,
472.28,
184.36,
473.14,
183.29,
473.14,
179.87,
473.35,
178.16,
474.85,
176.67,
475.92,
175.38,
477.63,
173.46,
479.98,
172.82,
484.04,
175.6,
484.47,
178.16,
484.9,
178.8,
492.38,
180.3,
499.43,
181.16,
506.06,
180.94,
507.34,
182.22,
507.56,
183.51,
506.06,
184.58,
503.28,
185.64,
499.22,
185.86,
493.23,
186.5,
489.17,
186.71,
490.67,
192.06,
490.24,
193.77,
488.74,
194.41,
488.1,
196.98,
488.32,
197.62,
487.03,
198.69,
485.97,
203.17,
486.82,
204.03,
488.53,
204.89,
486.39,
207.88,
485.75,
214.29,
486.39,
218.35,
482.55,
218.57,
481.48,
220.92,
479.77,
220.06,
478.27,
218.57]],
'num_keypoints': 0,
'area': 708.26055,
'iscrowd': 0,
'keypoints': [0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0],
'image_id': 391895,
'bbox': [471.64, 172.82, 35.92, 48.1],
'category_id': 1,
'id': 1260346}]
- 可以看到图中确实是两个人;一个骑摩托车的,另外一个是远处的一个几乎和草地融入一起的 绿色背影
plt.imshow(img)
coco.showAnns(annoInfo)
- 这个图中,确实只有一个人,因此上面的 annotation 列表中只有一个值
imgID = 522418
imgInfo = coco.loadImgs(522418)
img = plt.imread("../images/train2017/%s"%imgInfo[0]["file_name"])
annoInfo = coco.loadAnns([455475])
plt.imshow(img)
coco.showAnns(annoInfo)
getCatIds()
- 其实在这个 person_keypoint 的 json 文件中,只有 person 一个类的标注,因此当我们的 catNms(category name) = person 的时候,返回的 “标注类别的id”是 1
coco.getCatIds(catNms=["person"])
[1]
getImgIds()
- 这个方法的参数是 imgID 和 categoryID 返回一个 imgID
- 我一直不太理解,这个方法有点像,我返回我自己····,我有点无语
coco.getImgIds(imgIds=[522418])
[522418]
annoToMask()
- 顾名思义,这个方法是通过将 anno 生成 mask 的工具
- 输入的是一个 annoInfo 的字典,输出的是一个 mask
- 切记因为 annoInfo 是一个列表形式,里面包含了唯一的一个字典,而我们 annoToMask() 参数是 dict,因此需要先把 annoInfo[0] 再放进去
mask = coco.annToMask(annoInfo[0])
mask
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 1, 1, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)
plt.imshow(mask)
<matplotlib.image.AxesImage at 0x15aa87d1b38>
annoToRLE()
- 将一个 annotation 转换为游程编码
coco.annToRLE(annoInfo[0])
{'size': [480, 640],
'counts': b'Xnc51n>2N2O0O2N2O1N101N10O0100O100O01000O10O10O100000O010000O01000O1000000O1001O00ZBAk<?TCFh<:WCHh<8VCJj<6UCLj<4TCOd1Ih88cE1V18T9HcE2k0g0_9WOeE4>T1k9hNfE64_1S:[NhE84`1Q:XNjE95a1P:UNkE:5d1m9RNnE96e1l9RNnE87f1k9RNnE78g1j9RNmE7:g1i9RNmE6;h1h9RNmE5<i1g9RNmE5<i1g9RNmE5<j1f9QNnE5<j1f9RNlE6=h1g9RNlE6=h1g9RNlE6=h1h9QNkE7=h1h9QNkE7=h1h9TNhE5?g1i9QOWFo0i9QOWFo0i9QOWFP1h9POXFP1h9POXFP1h9QOWFo0i9QOWFo0i9QOWFo0i9QOWFo0i9QOWFo0j9QOUFo0k9QOUFo0k9QOUFP1j9POVFP1j9POVFP1j9QOUFo0k9QOUFo0k9QOUFo0k9QOUFo0k9QOUFP1j9POVFP1j9QOUFo0k9QOUFP1k9oNUFQ1k9oNUFQ1k9oNUFQ1k9POTFQ1k9oNUFQ1k9oNUFQ1k9oNUFR1j9nNVFR1j9nNVFR1j9oNUFR1j9nNWFQ1i9nNXFR1h9nNXFS1g9mNYFY1b9fN^F_1]9aNcFe1W9[NiFk1Q9UNoFP2l8PNTGV2f8jMZG\\2`8dM`G_2]8aMcG_2]8aMcG_2]8aMcG_2]8aMcG_2]8aMcG_2]8aMcG_2^8`MbG_2_8aMaG_2_8aMaG_2_8aMaG_2[8fMdGZ2X8lMfGT2U8SNiGm1R8ZNlGf1P8_NoGa1l7dNTH\\1h7hNXHX1c7mN]HS1^7RObHn0Z7VOfHj0V7ZOjHf0T7\\OlHd0Q7_OoHa0n6BRI>k6EUI;h6HXI8e6K[I5b6N^I2_61aI0[63eIOV64jINQ65oILm57SJKh58XJIe59[JI`5:`JG\\5<dJFW5=iJDS5?mJCo4?QKBk4a0UKAf4b0ZK_Ob4d0^K^O]4e0cK\\OZ4f0fK\\OU4g0kKZOQ4i0oKYOl3k0SLVOi3m0WLUOe3m0[LTOa3o0_LSO\\3P1dLQOX3R1hLoNT3T1lLlNR3V1nLkNn2X1RMiNj2Z1VMgNf2\\1ZMeNb2^1^McN_2_1aMaN\\2b1dM^NY2e1gM\\NU2g1kMYNR2j1nMVNP2l1PNTNn1n1RNRNl1P2TNQNj1P2VNPNi1Q2WNoMg1S2YNmMf1T2ZNmMd1T2\\NlMb1V2^NjMa1W2_NiM`1X2`NhM^1Z2bNgM\\1Z2dNfM[1[2eNeMY1]2gNcMX1^2hNbMW1_2iNbMU1_2kNaMS1a2mN_MR1b2nN^MQ1c2oN]Mo0e2QO\\Mm0e2SO[Mm0e2SO[Ml0f2TOZMk0g2UOZMj0f2VOZMi0g2WOYMh0h2XOXMg0i2ZOVMf0j2ZOWMd0j2\\OUMd0l2\\OTMd0l2\\OTMc0m2]ORMc0o2]OQMb0P3^OPMb0P3^OoLb0R3^OnLa0S3_OmL`0T3@kLa0U3_OkL`0V3@jL?W3AhL`0X3@hL?Y3AgL>Z3BeL>\\3BdL>\\3BdL=]3CcL<^3DaL=_3CaL<`3D`L;a3E^L;c3E]L;c3E]L:d3F[L:f3FZL:f3FZL9g3GXL9i3GWL8j3HVL8j3HUL8l3HTL7m3ISL6n3JQL7o3IQL6P4JPL5Q4KoK5Q4KnK5S4KmK4T4LlK3U4M_50000000000000000n>'}
showAnns()
- 可以将所有的 annotation 都显示在图层中,而 annoToMask 只能将 annoInfo 中的 segmentation 转换成 mask
- 必须先plt.imshow(img),否则什么也显示不出来
plt.imshow(img)
coco.showAnns(annoInfo)
coco.showAnns(annoInfo)
- 上述就是一个错误示范,什么也没显示出来
instance_annotation
- 上面所有的例子,都是基于 coco 数据中的 “person_keypoint annotation”文件,下面的所有例子将会使用 “instance annotation”文件来进行操作和演示
filepath = "./annotations/instances_train2017.json"
instance_coco = COCO(filepath)
loading annotations into memory...
Done (t=24.07s)
creating index...
index created!
类别信息
- 可以看出 instance_coco 加载出 instance 的 annotation 的标注种类有 90 种,
- 而 coco 加载出的 person_keypoint 中的 annotation 标注种类只有 1 种,因为是针对人的~
instance_coco.cats.keys() # 列表中的每一个其实就是一个 catID
dict_keys([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90])
coco.cats.keys()
dict_keys([1])
挑选一类图片
# 返回 catID = 1 的所有的图片的 imgID
coco.getImgIds(catIds=[1])
[262145,
262146,
524291,
262148,
393223,
393224,
524297,
393227,
131084,
393230,
262161,
131089,
524311,
393241,
524314,
393243,
262171,
131101,
524317,
262175,
524320,
393251,
131108,
524325,
36,
131115,
524333,
262191,
49,
524338,
393267,
393268,
262197,
393271,
262200,
131127,
61,
262207,
393284,
74,
393290,
262221,
524366,
77,
131152,
524373,
262229,
86,
524375,
393306,
262235,
262238,
262239,
262242,
131172,
393317,
131174,
109,
110,
524401,
113,
262260,
393333,
131197,
127,
262273,
262274,
262275,
524420,
136,
131208,
131211,
524428,
262286,
524431,
131215,
524436,
149,
151,
131225,
262299,
393372,
393375,
524450,
165,
524453,
393384,
524459,
131245,
524467,
524470,
524476,
262334,
262335,
192,
262336,
393411,
524486,
201,
393418,
393419,
131276,
131277,
131279,
131280,
262353,
131282,
393428,
262359,
524507,
393438,
393442,
131299,
524522,
524525,
241,
131315,
524533,
262389,
524535,
524536,
262391,
262394,
393464,
262399,
257,
524547,
262404,
260,
393478,
131335,
393480,
262407,
524551,
131339,
262413,
262414,
131342,
524557,
524559,
262418,
393489,
131343,
393493,
262425,
393497,
393503,
131361,
524577,
131364,
294,
393511,
131366,
393513,
131373,
131374,
131376,
524594,
524595,
262450,
308,
262454,
524601,
315,
393534,
262463,
262465,
322,
524613,
326,
328,
393544,
262476,
262477,
524623,
393553,
338,
524627,
131418,
131419,
524638,
262495,
131427,
357,
524646,
524645,
360,
262505,
393578,
524651,
262508,
262509,
393575,
524649,
368,
393576,
262514,
370,
393592,
524665,
262521,
393593,
131450,
382,
262528,
262529,
524672,
262531,
524677,
389,
524679,
524681,
131465,
395,
262538,
397,
131470,
131467,
393617,
262545,
524692,
262550,
393629,
524702,
415,
393630,
131487,
131486,
419,
393634,
131493,
131494,
262565,
524709,
262561,
131498,
428,
524718,
393647,
431,
393649,
436,
131511,
393656,
524730,
443,
262588,
446,
393664,
524736,
262594,
131522,
393674,
459,
393675,
262605,
131534,
262603,
393680,
262609,
131539,
393685,
471,
262617,
474,
524766,
393699,
524775,
488,
524777,
393707,
393709,
393710,
262642,
524786,
131572,
393714,
524790,
393720,
131580,
131581,
510,
524799,
393724,
262658,
524802,
131589,
393735,
520,
131593,
393738,
131597,
262670,
393743,
262672,
529,
262673,
531,
532,
524819,
536,
131611,
131612,
262686,
542,
544,
131617,
524830,
262688,
262692,
131621,
524844,
262703,
393777,
262705,
262707,
564,
262710,
569,
262715,
572,
262718,
262724,
131661,
589,
524877,
393809,
262738,
131665,
524887,
524889,
262746,
131674,
262747,
131678,
524897,
393826,
393837,
623,
625,
131697,
262770,
393845,
131703,
634,
524924,
524925,
393852,
641,
524929,
262786,
131721,
393874,
659,
524946,
131735,
262810,
524956,
524957,
131742,
131743,
673,
524962,
524966,
393896,
393900,
684,
393905,
690,
524979,
692,
693,
393909,
524983,
131764,
524984,
131763,
393915,
524992,
262848,
131780,
393924,
716,
262861,
262862,
722,
723,
525012,
262869,
393942,
393943,
131800,
262873,
730,
731,
131804,
525021,
525015,
735,
525024,
393954,
525027,
262884,
131812,
131815,
131816,
262893,
525039,
131825,
393971,
761,
525050,
131833,
764,
262909,
393978,
393984,
131840,
525058,
393985,
262917,
131847,
781,
393997,
262925,
131856,
394002,
525075,
790,
262935,
131865,
795,
131869,
525085,
525087,
394019,
262951,
262952,
810,
262956,
525100,
525104,
394033,
525106,
821,
262967,
828,
131902,
831,
525119,
262976,
394050,
836,
262980,
525124,
839,
131909,
131913,
394058,
131915,
131911,
131918,
131919,
262991,
394071,
131927,
263006,
394079,
131937,
525153,
263011,
131942,
525162,
525163,
525169,
525170,
525171,
263028,
882,
263031,
394104,
131961,
525179,
525180,
525183,
525184,
131969,
131967,
263041,
263043,
394113,
901,
897,
131976,
263042,
525187,
131979,
263052,
131978,
913,
525202,
525206,
923,
525211,
394139,
925,
927,
132000,
263073,
525218,
525212,
394149,
934,
394151,
525226,
525227,
263083,
263084,
943,
525232,
132019,
525236,
263094,
132024,
263098,
394172,
263101,
263104,
525249,
962,
960,
965,
132038,
263111,
969,
132042,
394185,
974,
394190,
525264,
525266,
394197,
984,
985,
525273,
132057,
263136,
394209,
394210,
132067,
394214,
999,
263146,
132077,
1006,
525297,
1011,
525300,
1014,
525303,
263163,
394240,
394242,
132099,
394245,
394246,
132102,
263176,
263177,
394248,
394251,
525318,
132114,
263189,
132120,
394266,
1053,
525344,
263202,
132132,
132136,
263208,
525354,
132137,
263212,
394283,
394286,
132143,
525360,
132141,
132147,
263223,
525369,
525373,
263229,
394303,
1089,
525381,
525382,
1098,
1102,
525391,
394320,
394322,
263251,
1107,
394325,
132182,
1108,
263258,
525405,
525409,
263265,
132196,
263270,
263275,
394349,
263278,
132212,
394359,
1144,
1145,
1146,
132219,
263292,
1149,
132217,
525439,
132223,
525438,
525450,
1164,
1176,
394393,
263323,
1180,
525467,
1183,
263328,
263327,
132258,
394404,
132265,
394412,
394415,
132272,
1200,
394418,
1204,
263351,
263355,
525501,
263358,
263359,
132288,
1216,
263362,
394436,
132294,
525510,
525513,
132298,
132299,
394444,
525518,
394447,
1232,
394449,
263377,
132307,
1237,
132310,
263383,
263381,
263385,
1244,
394460,
525533,
263388,
525539,
394468,
525542,
132328,
525546,
394474,
263406,
132336,
394480,
525555,
1270,
1271,
394486,
263418,
525567,
263428,
394501,
525572,
1290,
1292,
525580,
1295,
263440,
1298,
394514,
525589,
394518,
263450,
1307,
1308,
263453,
1311,
394527,
394529,
132386,
1315,
394532,
394533,
132385,
394535,
1319,
132393,
263462,
394539,
132395,
132394,
525616,
1330,
525619,
1332,
394547,
525622,
132406,
394546,
394554,
132412,
1342,
394560,
525636,
394565,
1350,
525640,
394572,
132430,
1359,
1360,
263505,
263504,
394580,
394583,
263512,
1369,
263516,
525660,
394590,
1375,
525666,
525667,
394596,
1381,
525668,
263526,
394600,
525675,
525678,
1390,
394608,
525682,
525684,
1397,
1398,
1401,
1403,
1404,
132476,
394620,
1407,
525695,
417105,
394627,
525700,
525702,
525705,
394635,
132493,
132495,
1424,
525714,
132500,
1431,
263576,
525721,
1436,
132509,
132510,
263582,
394653,
263586,
132515,
525732,
132516,
394658,
263589,
132521,
1451,
1453,
132527,
132529,
263603,
263604,
132533,
1464,
263609,
263613,
525762,
525763,
394691,
263620,
525766,
132548,
132552,
132554,
394698,
1488,
525777,
525779,
132564,
525782,
132568,
394713,
132571,
132573,
132574,
263647,
132576,
525790,
263651,
394724,
394729,
525802,
132591,
263664,
1523,
263668,
525813,
1527,
394747,
394748,
525823,
132607,
1536,
263685,
263687,
132615,
132617,
1548,
394764,
394766,
263696,
525841,
1554,
132626,
263700,
263703,
525849,
1563,
394784,
1569,
132644,
394790,
394793,
525865,
1579,
132652,
1580,
394794,
263724,
1586,
525876,
263734,
1591,
525880,
1592,
1596,
132669,
132670,
1599,
525898,
132683,
525899,
263759,
263760,
263762,
525908,
263764,
394840,
1626,
525915,
132698,
1637,
132710,
525927,
1639,
525933,
132719,
525939,
132724,
1655,
525944,
394874,
132733,
394879,
394880,
263810,
1667,
1668,
1670,
394889,
132746,
394891,
394892,
132750,
263823,
394896,
263826,
525971,
263828,
394904,
394905,
263834,
132760,
525980,
394909,
1700,
525990,
132776,
394921,
1706,
132778,
526000,
394929,
263858,
526004,
132789,
132791,
526008,
1720,
1722,
394941,
394942,
263873,
263875,
1732,
526021,
263878,
263876,
526028,
132816,
526033,
394964,
526040,
263896,
132826,
526043,
1756,
1757,
394975,
1762,
1764,
132836,
132838,
132841,
526057,
1774,
394992,
526070,
1785,
395005,
1790,
526082,
395013,
526085,
1799,
526087,
526089,
132874,
526091,
1804,
395022,
526098,
1811,
132883,
263957,
1815,
132888,
132889,
1822,
395039,
395040,
263970,
132901,
...]
从中挑选一张图片
# 在这些 imgID 里面拿一个做演示,那就取第 9 个位置吧
imgID = coco.getImgIds(catIds=[1])[8]
imgID
131084
获取图片信息
imgInfo = coco.loadImgs(imgID)
imgInfo
[{'license': 1,
'file_name': '000000131084.jpg',
'coco_url': 'http://images.cocodataset.org/train2017/000000131084.jpg',
'height': 426,
'width': 640,
'date_captured': '2013-11-15 21:01:40',
'flickr_url': 'http://farm8.staticflickr.com/7055/6806786818_1dd81608bd_z.jpg',
'id': 131084}]
按照获得图片信息加载图片
img = plt.imread("../images/train2017/%s"%imgInfo[0]["file_name"])
根据图片 id 获得 anno_id 并加载对应的 anno
# 获得对应图片的 annotation_id
annoID = coco.getAnnIds(imgIds=[imgID])
# 获得对应图片的 annotatation
annoInfo = coco.loadAnns(annoID)
annoInfo
[{'segmentation': [[325.48,
245.07,
325.48,
259.43,
325.48,
264.22,
322.61,
270.92,
316.87,
286.23,
317.82,
294.85,
317.82,
297.72,
326.44,
299.64,
336.01,
300.59,
339.84,
300.59,
339.84,
296.76,
343.67,
287.19,
345.59,
275.7,
356.12,
234.54,
357.07,
226.88,
358.99,
215.39,
368.56,
194.33,
375.26,
159.87,
379.09,
149.34,
377.18,
125.41,
342.71,
102.43,
348.46,
97.64,
352.29,
92.86,
351.33,
80.41,
345.59,
74.67,
344.63,
72.76,
330.27,
64.14,
320.7,
67.97,
315.91,
70.84,
306.34,
78.5,
306.34,
88.07,
306.34,
90.94,
300.59,
100.52,
278.58,
114.88,
274.75,
116.79,
251.77,
129.24,
237.41,
138.81,
226.88,
148.38,
222.09,
157.96,
220.18,
166.57,
223.05,
170.4,
232.62,
171.36,
242.2,
172.31,
248.9,
171.36,
263.26,
162.74,
271.87,
157.96,
280.49,
152.21,
289.11,
146.47,
293.89,
141.68,
296.76,
140.72,
296.76,
143.6,
300.59,
168.49,
291.02,
170.4,
280.49,
178.06,
278.58,
204.86,
281.45,
205.82,
298.68,
205.82,
310.17,
201.99,
327.4,
226.88]],
'num_keypoints': 16,
'area': 14962.71795,
'iscrowd': 0,
'keypoints': [319,
105,
2,
327,
101,
2,
315,
101,
2,
344,
96,
2,
0,
0,
0,
359,
122,
2,
305,
119,
2,
364,
152,
2,
273,
139,
2,
342,
162,
2,
239,
155,
2,
348,
184,
2,
307,
178,
2,
334,
220,
2,
265,
202,
1,
329,
285,
2,
255,
266,
1],
'image_id': 131084,
'bbox': [220.18, 64.14, 158.91, 236.45],
'category_id': 1,
'id': 489645}]
展示结果
plt.imshow(img)
coco.showAnns(annoInfo)
mask = coco.annToMask(annoInfo[0])
plt.imshow(mask)
<matplotlib.image.AxesImage at 0x13682359d68>
版权声明:本文为CSDN博主「暖仔会飞」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_42902997/article/details/121781593
暂无评论