import json
import os
import cv2
if __name__ == '__main__':
json_file = "G:/other/bdd100k_det_20_labels_trainval/bdd100k/labels/det_20/det_val.json" # json file path
save_path = "G:/other/bdd100k_images_100k/bdd100k/labels/val" # txt save path
with open(json_file, 'r') as f:
diction = json.load(f)
for i in range(len(diction)):
names = diction[i].get("name") # image name
img = cv2.imread('G:/other/bdd100k_images_100k/bdd100k/images/100k/val/' + names)
height, width = img.shape[:2]
labels = diction[i].get("labels")
label_txt = os.path.join(save_path, names[:-4] + ".txt") # txt name
with open(label_txt, 'a') as file:
for j in range(len(labels)):
boxes = labels[j].get("box2d") # box
box_class = labels[j].get("category") # class
if box_class == 'car':