Linux下安装pycocotools

step1:首先安装cython

pip install cython

step2: git COCOAPI的源码
https://github.com/cocodataset/cocoapi.git

cd $COCOAPI/PythonAPI

并将Makefile中所有的python改成python3
step3:

make install

一些基本用法示例:

from pycocotools.coco import COCO

coco = COCO(os.path.join(self.root_dir, 'annotations', 'instances_' + self.set_name + '.json'))
image_ids =coco.getImgIds() 
>>[391895, 522418, 184613, 318219, 554625, 574769, 60623, 309022, 5802, 222564,...]
categories = coco.loadCats(coco.getCatIds())
>>[{'supercategory': 'person', 'id': 1, 'name': 'person'}, {'supercategory': 'vehicle', 'id': 2, 'name': 'bicycle'}, {'supercategory': 'vehicle', 'id': 3, 'name': 'car'}, {'supercategory': 'vehicle', 'id': 4, 'name': 'motorcycle'}, {'supercategory': 'vehicle', 'id': 5, 'name': 'airplane'}, {'supercategory': 'vehicle', 'id': 6, 'name': 'bus'}, {'supercategory': 'vehicle', 'id': 7, 'name': 'train'}, {'supercategory': 'vehicle', 'id': 8, 'name': 'truck'}, {'supercategory': 'vehicle', 'id': 9, 'name': 'boat'}, {'supercategory': 'outdoor', 'id': 10, 'name': 'traffic light'}]
coco.loadImgs(image_ids[0])
>>[{'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}]
annotations_ids=coco.getAnnIds(imgIds=image_ids[0], iscrowd=False)
>>[151091, 202758, 1260346, 1766676]
coco_annotations =coco.loadAnns(annotations_ids)
>>[{'segmentation': [[376.97, 176.91, 398.81, 176.91, 396.38, 147.78, 447.35, 146.17, 448.16, 172.05, 448.16, 178.53, 464.34, 186.62, 464.34, 192.28, 448.97, 195.51, 447.35, 235.96, 441.69, 258.62, 454.63, 268.32, 462.72, 276.41, 471.62, 290.98, 456.25, 298.26, 439.26, 292.59, 431.98, 308.77, 442.49, 313.63, 436.02, 316.86, 429.55, 322.53, 419.84, 354.89, 402.04, 359.74, 401.24, 312.82, 370.49, 303.92, 391.53, 299.87, 391.53, 280.46, 385.06, 278.84, 381.01, 278.84, 359.17, 269.13, 373.73, 261.85, 374.54, 256.19, 378.58, 231.11, 383.44, 205.22, 385.87, 192.28, 373.73, 184.19]], 'area': 12190.44565, 'iscrowd': 0, 'image_id': 391895, 'bbox': [359.17, 146.17, 112.45, 213.57], 'category_id': 4, 'id': 151091}, 
      {'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.0, 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.0, 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.0, 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]], 'area': 14107.271300000002, 'iscrowd': 0, '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]], 'area': 708.2605500000001, 'iscrowd': 0, 'image_id': 391895, 'bbox': [471.64, 172.82, 35.92, 48.1], 'category_id': 1, 'id': 1260346}, \
      {'segmentation': [[486.01, 217.92, 486.01, 211.11, 487.71, 206.57, 489.6, 204.11, 487.71, 201.84, 488.66, 198.63, 489.98, 196.55, 489.04, 193.52, 495.46, 190.88, 496.22, 190.12, 494.52, 187.28, 497.36, 186.72, 501.33, 187.66, 509.08, 183.88, 513.81, 183.31, 513.99, 183.31, 516.64, 187.28, 515.89, 188.04, 508.51, 188.42, 508.89, 189.93, 511.54, 191.25, 511.16, 194.09, 507.57, 197.68, 507.94, 204.3, 508.7, 208.46, 507.0, 214.89, 506.62, 216.02, 503.6, 216.21, 500.95, 216.21, 495.65, 217.92, 489.79, 218.29]], 'area': 626.9852500000001, 'iscrowd': 0, 'image_id': 391895, 'bbox': [486.01, 183.31, 30.63, 34.98], 'category_id': 2, 'id': 1766676}]

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值