下载部分COCO数据集并生成新的json标注文件

电脑跑不动完整的COCO数据集(没耐心等它下完),所以想下载部分图片来跑(只是想试跑下mask rcnn),cocoAPI中提供了下载图片的接口,对其做了部分修改,改成从原先的json文件中随机下载指定数量的图片并保留它们的json标注信息重新保存为一个新的小的json.将下面代码存储在cocoapi/pythonAPI/pycocotools下 命名mycoco.py# coding:...
摘要由CSDN通过智能技术生成

电脑跑不动完整的COCO数据集(没耐心等它下完),所以想下载部分图片来跑(只是想试跑下mask rcnn),cocoAPI中提供了下载图片的接口,对其做了部分修改,改成从原先的json文件中随机下载指定数量的图片并保留它们的json标注信息重新保存为一个新的小的json.

将下面代码存储在cocoapi/pythonAPI/pycocotools下 命名mycoco.py

# coding:utf8
__author__ = 'tylin'
__version__ = '2.0'
# Interface for accessing the Microsoft COCO dataset.

# Microsoft COCO is a large image dataset designed for object detection,
# segmentation, and caption generation. pycocotools is a Python API that
# assists in loading, parsing and visualizing the annotations in COCO.
# Please visit http://mscoco.org/ for more information on COCO, including
# for the data, paper, and tutorials. The exact format of the annotations
# is also described on the COCO website. For example usage of the pycocotools
# please see pycocotools_demo.ipynb. In addition to this API, please download both
# the COCO images and annotations in order to run the demo.

# An alternative to using the API is to load the annotations directly
# into Python dictionary
# Using the API provides additional utility functions. Note that this API
# supports both *instance* and *caption* annotations. In the case of
# captions not all functions are defined (e.g. categories are undefined).

# The following API functions are defined:
#  COCO	   - COCO api class that loads COCO annotation file and prepare data structures.
#  decodeMask - Decode binary mask M encoded via run-length encoding.
#  encodeMask - Encode binary mask M using run-length encoding.
#  getAnnIds  - Get ann ids that satisfy given filter conditions.
#  getCatIds  - Get cat ids that satisfy given filter conditions.
#  getImgIds  - Get img ids that satisfy given filter conditions.
#  loadAnns   - Load anns with the specified ids.
#  loadCats   - Load cats with the specified ids.
#  loadImgs   - Load imgs with the specified ids.
#  annToMask  - Convert segmentation in an annotation to binary mask.
#  showAnns   - Display the specified annotations.
#  loadRes	- Load algorithm results and create API for accessing them.
#  download   - Download COCO images from mscoco.org server.
# Throughout the API "ann"=annotation, "cat"=category, and "img"=image.
# Help on each functions can be accessed by: "help COCO>function".

# See also COCO>decodeMask,
# COCO>encodeMask, COCO>getAnnIds, COCO>getCatIds,
# COCO>getImgIds, COCO>loadAnns, COCO>loadCats,
# COCO>loadImgs, COCO>annToMask, COCO>showAnns

# Microsoft COCO Toolbox.	  version 2.0
# Data, paper, and tutorials available at:  http://mscoco.org/
# Code written by Piotr Dollar and Tsung-Yi Lin, 2014.
# Licensed under the Simplified BSD License [see bsd.txt]

import json
import time
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon
import numpy as np
import copy
import itertools
import os
from collections import defaultdict
import sys
import json
PYTHON_VERSION = sys.version_info[0]
if PYTHON_VERSION == 2:
	from urllib impo
  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
生成COCO数据集的标注文件夹需要以下步骤: 1.创建一个annotations文件夹,用于存放标注文件。 2.创建一个JSON格式的标注文件,包含所有图像的注释信息。JSON文件应该包含以下字段: - images:一个包含所有图像信息的列表,每个图像包含id、宽度、高度和文件名等信息。 - annotations:一个包含所有注释信息的列表,每个注释包含id、图像id、类别id、边界框坐标等信息。 - categories:一个包含所有类别信息的列表,每个类别包含id和名称等信息。 3.将JSON文件保存到annotations文件夹中。 4.将所有图像复制到一个名为images的文件夹中。 5.运行以下Python代码,将图像信息添加到JSON文件中: ```python import json import os # 设置文件夹路径 img_folder = 'images' ann_folder = 'annotations' # 获取所有图像文件名 img_names = os.listdir(img_folder) # 创建JSON文件 data = {} data['images'] = [] data['annotations'] = [] data['categories'] = [] # 添加类别信息 category = {'id': 1, 'name': 'class1'} data['categories'].append(category) # 添加图像信息 for i, img_name in enumerate(img_names): img_id = i + 1 img_path = os.path.join(img_folder, img_name) img_data = {'id': img_id, 'file_name': img_name} img = Image.open(img_path) img_data['width'] = img.width img_data['height'] = img.height data['images'].append(img_data) # 添加注释信息 for i, img_name in enumerate(img_names): img_id = i + 1 ann_id = i + 1 ann_path = os.path.join(ann_folder, img_name[:-4] + '.txt') with open(ann_path, 'r') as f: ann_data = f.read().splitlines() for ann in ann_data: ann = ann.split() x, y, w, h = map(float, ann[1:]) bbox = [x, y, w, h] ann = {'id': ann_id, 'image_id': img_id, 'category_id': 1, 'bbox': bbox} data['annotations'].append(ann) ann_id += 1 # 保存JSON文件 json_path = os.path.join(ann_folder, 'annotations.json') with open(json_path, 'w') as f: json.dump(data, f) ``` 这样就可以生成一个包含所有图像注释信息的COCO数据标注文件夹。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值