Use json header file and coco api to view and modify json in coco format

Use the json header file to view the coco format json

You can use the json of coco2017 to test with me

#Use the json header file to read into the coco dataset
import json
with open("/home/fengsiyuan/detectron2/datasets/coco/annotations/coco_ycbv_train_000000.json",'r') as load_f:
    f = json.load(load_f)


#Take a look at the annotations information in this file

#Read the information of the first annotation box in annotations

#Read the first bbox
#Read the info information of this file

#Read the licenses information in this file
#Read category

#Read picture information

#Try reading the box with id=0

#Try reading the file path again
#Then modify the file path according to this
#File pointer closed
#Reference code [Python file open, close, file pointer](

Use the COCO API to view JSON in COCO format

[COCO dataset] Introduction to COCO API super detailed notes

load annotations file(Load here instances_val2017.json)´╝îgenerate COCO object
import numpy as np 
from pycocotools.coco import COCO
import json
annFile = os.path.join(COCODIRS['annotations'], 'coco_ycbv_train_000000.json')
# with open(annFile,'r') as fp: # This comment was originally to load json directly to see the contents
#     a = json.load(fp)
# a
coco = COCO(annFile) # Initializing and generating COCO objects
coco # Generated COCO object

  • coco. Getcat id s() this function is used to obtain the IDs of all categories contained in the loading object (i.e. the sequence number of category)
  • coco.getAnnIds() gets all the tag information contained in the loading object (that is, the Segmentation of all pictures, that is, the divided coordinate data)
  • coco.getImgIds() gets the original image id corresponding to all tags
categories = coco.loadCats(coco.getCatIds()) # loadCats() needs to pass in the category id sequence (catIds) to be loaded
#categories[:10] # For beautiful output, only 10 pieces of data are displayed
categories[:]#Show all data 

names = [cat['name'] for cat in categories]
#names[:10]#For beautiful output, we only show ten categories here
names[:]#Show all categories

Get the category sequence number of the specified name (find the category id with category name x)
Here, the category id corresponding to 8, 14 and 21 is obtained because my dataset json file only contains objects of categories 8, 14 and 21

catIds = coco.getCatIds(catNms=['8', '14', '21'])

Get all picture IDs that match cat id s
That is, the categories we obtained above are 8, 14 and 21, so we can obtain the id corresponding to the pictures containing these three categories in the picture

  • Note that getImgIds takes the intersection, not the union. Adding an id will only return the empty string ¶
imgIds = coco.getImgIds(catIds=catIds)

Randomly select a picture from the sequence numbers of all the pictures that meet the above conditions (here, the random function of numpy) and load the information of the picture through loadImgs()

img_info = coco.loadImgs(imgIds[np.random.randint(0, len(imgIds))])

You can see that loadImgs() returns a list, and each element is the specific information of a corresponding image

Since what we need here is a picture information, we use the following method to extract the first information

img_info = coco.loadImgs(imgIds[np.random.randint(0, len(imgIds))])[0]
#Select only one picture to try (the bottom line of code can only be run once, so we annotated the bottom and selected the above code to run)
#img_info = img_info[0]

You can see that the obtained picture information includes:

  • license
  • file_name
  • coco_url
  • height
  • width
  • date_captured
  • flickr_url
  • id

Modify the coco format json using the json header file

Let me take the filename of the json file I modified ycbv as an example

import json
dic = {}
with open("/home/fengsiyuan/detectron2/datasets/coco/annotations/data_train.json",'r',encoding='utf8') as load_f:
    f = json.load(load_f)

#Read picture information

As you can see, the file in my file_name is'... \ 000000 \ RGB \ 00000 1 Png 'format. I can't use this format in detectron2, so I want to use python's string function to change it to' / 000000 / RGB / 00000 1 png’

for num in range(0,len(f['images'])):  
    str = f['images'][num]['file_name']
    str = str.replace('..\\','')
    str = str.replace('\\','/')
#Save all the processing just now to dic, and then close load_f
dic = {}
dic = f

Now reopen the file and use the jump function to overwrite the modified content into the file

with open("/home/fengsiyuan/detectron2/datasets/coco/annotations/data_train.json",'w') as load_f:

Check the result of the modification

#Check the modification results
with open("/home/fengsiyuan/detectron2/datasets/coco/annotations/data_train.json",'r',encoding='utf8') as load_f:
    f = json.load(load_f)

As you can see, the file of the file_name has been modified to the format we want. So we close the file pointer


Keywords: Python JSON AI

Added by jtbaker on Sun, 02 Jan 2022 00:36:16 +0200