Below is just a few entries from a JSON file that I am working with. I've committed irrelevant data from it with .... I have a series of images in a directory and this JSON has the annotations for the object(s) in each image. "External ID" is the name of the image and "bbox":{} are the bounding box values. I want to create a txt file that has the same name as the image for every image and in every txt file, I want to write the top, left, height and width values. Some images have 2 or more box dictionaries.
In short, every image will have a corresponding txt file with the same name and that txt file will have all of the bbox values for that image.
[ { "ID": "ckkskkmhf00003e5meakh9qhr", "DataRow ID": "ckkskgs501y2s0sgoa5uj54rc", "Labeled Data": "https://storage.labelbox.com/ckkqt5y5ulks20757tbduodbw%2Fd6edbf6a-94b1-592a-e28c-79428cae89a7-Screenshot%202021-02-05%20at%2012.52.55.png?Expires=1614084797275&KeyName=labelbox-assets-key-1&Signature=NFtiQv2dIU4ztDR9lbtnwF9RMeA", "Label": { "objects": [ { "featureId": "ckkxvi5xl05pr0y8w3ojde1bi", "schemaId": "ckkskil310j1o0y8r0fu252jy", "title": "Mastercard", "value": "mastercard", "color": "#006FA6", "bbox": { "top": 715, "left": 2558, "height": 38, "width": 68 }, "instanceURI": "https://api.labelbox.com/masks/feature/ckkxvi5xl05pr0y8w3ojde1bi?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja2txdDV5NmFtbm1iMDc0MGpsem03d2RwIiwib3JnYW5pemF0aW9uSWQiOiJja2txdDV5NXVsa3MyMDc1N3RiZHVvZGJ3IiwiaWF0IjoxNjEyODc1MTk3LCJleHAiOjE2MTU0NjcxOTd9.P-MyOgZidmSZSTLfhwpSsRfOaovsReP5MzpDtsrnWs0" } ], "classifications": [] }, ... }, { "ID": "ckkskm7r300013e5ml1p0v0a7", "DataRow ID": "ckkskgs511y6c0sgodqe20gh1", "Labeled Data": "https://storage.labelbox.com/ckkqt5y5ulks20757tbduodbw%2F2d4f59f4-a494-4b67-f4bb-d6b0724eb07d-Screenshot%202021-02-05%20at%2017.15.14.png?Expires=1614084797276&KeyName=labelbox-assets-key-1&Signature=zoVC260R42pGOMd_5qXXJj1Mn6c", "Label": { "objects": [ { "featureId": "ckkskkv2e0iv90y9ugnu7d9mq", "schemaId": "ckkskil300j1g0y8r5kbgguk9", "title": "OPPO", "value": "oppo", "color": "#1CE6FF", "bbox": { "top": 382, "left": 245, "height": 34, "width": 87 }, "instanceURI": "https://api.labelbox.com/masks/feature/ckkskkv2e0iv90y9ugnu7d9mq?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja2txdDV5NmFtbm1iMDc0MGpsem03d2RwIiwib3JnYW5pemF0aW9uSWQiOiJja2txdDV5NXVsa3MyMDc1N3RiZHVvZGJ3IiwiaWF0IjoxNjEyODc1MTk3LCJleHAiOjE2MTU0NjcxOTd9.P-MyOgZidmSZSTLfhwpSsRfOaovsReP5MzpDtsrnWs0" }, { "featureId": "ckksklm6p0j8j0y8r3dee7nfs", "schemaId": "ckkskil300j1g0y8r5kbgguk9", "title": "OPPO", "value": "oppo", "color": "#1CE6FF", "bbox": { "top": 251, "left": 1918, "height": 39, "width": 68 }, "instanceURI": "https://api.labelbox.com/masks/feature/ckksklm6p0j8j0y8r3dee7nfs?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja2txdDV5NmFtbm1iMDc0MGpsem03d2RwIiwib3JnYW5pemF0aW9uSWQiOiJja2txdDV5NXVsa3MyMDc1N3RiZHVvZGJ3IiwiaWF0IjoxNjEyODc1MTk3LCJleHAiOjE2MTU0NjcxOTd9.P-MyOgZidmSZSTLfhwpSsRfOaovsReP5MzpDtsrnWs0" } ], "classifications": [] }, ... "External ID": "Screenshot 2021-02-05 at 17.33.15.png", ... }, { "ID": "ckksknatu00023e5mzabcr62d", "DataRow ID": "ckkskgs521y9w0sgo43t648gy", "Labeled Data": "https://storage.labelbox.com/ckkqt5y5ulks20757tbduodbw%2F3ca3454b-24f3-434b-ef1e-034f9eeab55d-Screenshot%202021-02-05%20at%2017.21.54.png?Expires=1614084797276&KeyName=labelbox-assets-key-1&Signature=ycxwibdnOpH-yHkKYrIt5l1cwE0", "Label": { "objects": [ { "featureId": "ckkxvhk3j02lf0y8d6pnc62pi", "schemaId": "ckkskil310j1o0y8r0fu252jy", "title": "Mastercard", "value": "mastercard", "color": "#006FA6", "bbox": { "top": 884, "left": 1504, "height": 68, "width": 72 }, "instanceURI": "https://api.labelbox.com/masks/feature/ckkxvhk3j02lf0y8d6pnc62pi?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja2txdDV5NmFtbm1iMDc0MGpsem03d2RwIiwib3JnYW5pemF0aW9uSWQiOiJja2txdDV5NXVsa3MyMDc1N3RiZHVvZGJ3IiwiaWF0IjoxNjEyODc1MTk3LCJleHAiOjE2MTU0NjcxOTd9.P-MyOgZidmSZSTLfhwpSsRfOaovsReP5MzpDtsrnWs0" } ], "classifications": [] }, ... } Below is my attempt:
with open('Worlds.2020.NewDataSet.json') as json_file: # Loads the json file as a json object data = json.load(json_file) ... for i in range(0, 143): line = data[i] objects = line['Label']['objects'] external_id = line['External ID'] file_title = external_id[:-4] element = objects[i] bbox = element['bbox'] topLeft_y = bbox['top'] topLeft_x = bbox['left'] height = bbox['height'] width = bbox['width'] # Create the annotation file for each image if not os.path.exists(current_dir + '/Worlds-2020-Logos/' + file_title + '.txt'): with open(current_dir + '/Worlds-2020-Logos/' + file_title + '.txt', 'w+') as file: file.write(str(topLeft_x) + ' ' + str(topLeft_y) + ' ' + str(height) + ' ' + str(width)) However, this only creates .txt files for a few images. Then it throws this error:
Traceback (most recent call last): File "/home/annotate-new-dataset.py", line 28, in <module> element = objects[i] IndexError: list index out of range All lines in the JSON file are identical. However, after the 3rd iteration, it is throwing the above error.
https://stackoverflow.com/questions/66147185/python-throws-an-index-error-when-trying-to-parse-json-file-and-write-it-to-a-tx February 11, 2021 at 08:36AM
没有评论:
发表评论