赞
踩
import json import os import cv2 import numpy as np import random def randomcolor(): colorArr = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'] color = "" for i in range(6): color += colorArr[random.randint(0,14)] return "#"+color if __name__ == '__main__': # 获取存放txt的文件夹 # 迭代处理每个文件 # 迭代处理每个文件中的每一行 每个文件插入到一个json txt_path = "../txt" # img_path = "E:\PythonWorkSpace\paperTrain\WiderPersonAll\Images" img_path = "../images" json_path = "../json" write_json_context = dict() # 写入.json文件的大字典 # write_json_context['info'] = {'description': '', 'url': '', 'version': '', 'year': 2023, 'contributor': '', # 'date_created': '2023-02-16'} # write_json_context['licenses'] = [{'id': 1, 'name': None, 'url': None}] write_json_context['categories'] = [ {"id": 1, "name": "person", "supercategory": "", "color": "#75e955", "metadata": {}, "keypoint_colors": [], "keypoints": ["nose", "left_eye", "right_eye", "left_ear", "right_ear", "left_shoulder", "right_shoulder", "left_elbow", "right_elbow", "left_wrist", "right_wrist", "left_hip", "right_hip", "left_knee", "right_knee", "left_ankle", "right_ankle"], "skeleton": [[16, 14], [14, 12], [17, 15], [15, 13], [12, 13], [6, 12], [7, 13], [6, 7], [6, 8], [7, 9], [8, 10], [9, 11], [2, 3], [1, 2], [1, 3], [2, 4], [3, 5], [4, 6], [5, 7]]}] img_list = os.listdir(img_path) for index, img_name in enumerate(img_list): write_json_context['images'] = [] write_json_context['annotations'] = [] print(img_name) # f = open(txt_path+"/"+img_name) # 遍历一个txt的每一行 # for line in f.readlines(): # det_list = list(map(float, line.split(' '))) # print(det_list) # lines1 = f.readlines() # print(lines1) categories = [] im0 = cv2.imread(img_path+"/"+img_name) W, H = im0.shape[1], im0.shape[0] img_context = {} # 使用一个字典存储该图片信息 # img_name=os.path.basename(imagePath) #返回path最后的文件名。如果path以/或\结尾,那么就会返回空值 img_context["file_name"] = img_name src_front, src_back = os.path.splitext(img_name) # 将文件名和文件格式分开 # print("imageFile", src_front) img_context["height"] = H img_context["width"] = W # img_context["date_captured"] = '2021-07-25' img_context["id"] = index # 该图片的id # img_context["license"] = 1 # img_context["color_url"] = "" # img_context["flickr_url"] = "" img_context["path"] = "/datasets/wider/"+img_name img_context["dataset_id"] = "3" write_json_context["images"].append(img_context) # 将该图片信息添加到'image'列表中 txt_name = src_front + '.txt' #对应的txt文件名 with open(os.path.join(txt_path, txt_name), 'r') as fr: lines = fr.readlines() # 迭代一个txt文件中的每一行,插入到annotations for j, line in enumerate(lines): bbox_dict = {} class_id, x, y, w, h = line.strip().split(' ')[:5] # 获取每一个标注框的详细信息 class_id, x, y, w, h = int(class_id), float(x), float(y), float(w), float(h) # 将字符串类型转为可计算的int和float类型 xmin = (x - w / 2) * W # 坐标转换 ymin = (y - h / 2) * H xmax = (x + w / 2) * W ymax = (y + h / 2) * H w = w * W h = h * H bbox_dict["id"] = index * 10000 + j # bounding box的坐标信息 bbox_dict["image_id"] = index bbox_dict["category_id"] = class_id + 1 # 注意目标类别要加一 bbox_dict["iscrowd"] = False bbox_dict["isbbox"] = True bbox_dict["color"] = randomcolor() height, width = abs(ymax - ymin), abs(xmax - xmin) bbox_dict["area"] = np.round(height * width) bbox_dict["bbox"] = list(np.round([xmin, ymin, w, h])) # bbox_dict["segmentation"] = [[xmin, ymin, xmax, ymin, xmax, ymax, xmin, ymax]] bbox_dict["segmentation"] = [list(np.round([xmin, ymin, xmax, ymin, xmax, ymax, xmin, ymax], 1))] kpts = line.strip().split(' ')[6:] for k_index, i in enumerate(kpts): if k_index % 3 == 2: kpts[k_index] = 2 else: kpts[k_index] = int(float(i)) bbox_dict["keypoints"] = kpts write_json_context["annotations"].append(bbox_dict) # 将每一个由字典存储的bounding box 信息添加到'annotations'列表中 name = os.path.join(json_path, "val" + '.json') # print(write_json_context) print(json.dumps(write_json_context)) # with open(name,'w') as fw: # 将字典信息写入.json文件中 # json.dump(write_json_context, fw, indent=2)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。