简体   繁体   中英

Trying to remove specific item from json file

I am trying to remove all the items that are in the 'annotation' list and have the value 7 as "category Id". Every time I run the code the function returns a list that has not removed this specific entry. The end goal is to change the category IDs of a large number of specific labels and remove all the other labels.

This is the code I have now

annotations_test = 'test_annotations/_annotations.coco_test.json'
annotations_train = 'train/_annotations.coco.json'

def delete(annotations_test):
    with open(annotations_test, "r") as data_file:
        data = json.load(data_file)
        for i in range(1, len(data["annotations"])):
            len_after=len(data["annotations"])
            if i == len_after:
                print(data)
                return data
            if data["annotations"][i]['category_id'] == 7:
                del data["annotations"][i]
                i = i - 1

and the JSON file looks like this.

{
    "categories": [
         {
            "id": 0,
            "name": "Teeth",
            "supercategory": "none"
        },
      
    ],
    "images": [
        {
            "id": 0,
            "license": 1,
            "file_name": "Output13_jpg.rf.7422a2210c5a09d832d2e0c3d8a765ee.jpg",
            "height": 416,
            "width": 416,
            "date_captured": "2022-04-22T13:07:04+00:00"
        },
        {
            "id": 1,
            "license": 1,
            "file_name": "Output9_jpg.rf.f8ae8e69ef667a8883518fafff1470c0.jpg",
            "height": 416,
            "width": 416,
            "date_captured": "2022-04-22T13:07:04+00:00"
        }
    ],
    "annotations": [
        {
            "id": 0,
            "image_id": 0,
            "category_id": 7,
            "bbox": [
                179,
                43,
                20.5,
                23.5
            ],
            "area": 481.75,
            "segmentation": [],
            "iscrowd": 0
        },
        {
            "id": 1,
            "image_id": 0,
            "category_id": 3,
            "bbox": [
                240,
                54,
                20.5,
                23.5
            ],
            "area": 481.75,
            "segmentation": [],
            "iscrowd": 0
        }

It's not a good idea to use del on the list you loop around.

If what you want is a list of the annotations that don't have a category_id of 7, you can use a list comprehension.

with open(annotations_test, "r") as data_file:
    data = json.load(data_file)
    return [annotation for annotation in  data["annotations"] if annotation['category_id'] != 7]

If you need the whole json data with just the annotation updated.

with open(annotations_test, "r") as data_file:
    data = json.load(data_file)
    without_7 = [annotation for annotation in  data["annotations"] if annotation['category_id'] != 7]
    data["annotations"] = without_7
    return data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM