@wanghuijiao
2021-10-26T01:41:16.000000Z
字数 3296
阅读 950
技术文档
/ssd01/wanghuijiao/dataset/Summary/yolor_json2yolo.py脚本将json文件转换为yolo格式,此脚本可以实现在原有的yolo标签文件内容后进行添加操作,当原始yolo标签文件不存在时,也可以生成新的yolo标签文件。COCO上的yolov4预训练模型,见56服务器上/ssd01/wanghuijiao/pose_detector02/yolov4.weights,需要自行准备/path/to/your_test_data.data文件,此文件格式为:
classes = 80 # (yolov4.weights 模型对应检测类别)train = /ssd01/wanghuijiao/dataset/crowdhuman/crowdhuman_person_head/train.txt # 训练集路径(存放图片绝对路径,且图片和txt标签文件在同一文件夹)valid = /ssd01/wanghuijiao/dataset/crowdhuman/crowdhuman_person_head/val.txt # 测试集路径(存放图片绝对路径,且图片和txt标签文件在同一文件夹)names = /ssd01/wanghuijiao/dataset/Obj365/coco_classes.txt # yolov4.weights 模型对应检测类别名称,COCO就直接用这个路径吧backup = yolov4_human_head_detector_cfg/backup/ # 训练时权重文件输出路径,测试时这个不用管
用以下命令对测试集生成json结果,结果默认为/ssd01/wanghuijiao/pose_detector02/results/coco_results.json:
cd /ssd01/wanghuijiao/pose_detector02./darknet detector valid \/path/to/your_test_data.data \/ssd01/wanghuijiao/pose_detector02/cfg_init/yolov4.cfg \/ssd01/wanghuijiao/pose_detector02/yolov4.weights \
将上述测试集结果/ssd01/wanghuijiao/pose_detector02/results/coco_results.json转换并与原本的标签文件合并。/ssd01/wanghuijiao/dataset/Summary/yolor_json2yolo.py脚本内容请根据需求自行更改。
import jsonimport osimport cv2from cv2 import dataimport astimport tqdmimport argparsedef convert(size, box):# box: x, y, w, h 绝对坐标dw = 1. / (size[0])dh = 1. / (size[1])x_c = box[0] + box[2] / 2.0y_c = box[1] + box[3] / 2.0w = box[2]h = box[3]# 输出相对坐标 x_center, y_center, w, hx_c = x_c * dww = w * dwy_c = y_c * dhh = h * dhreturn (x_c, y_c, w, h)parser = argparse.ArgumentParser()parser.add_argument('--json_path',type=str ,default='./v1.0/coco_head_results.json', help="which save the output file and classes")parser.add_argument('--label_path_dir',type=str ,default='', help="which save the output file and classes")arg = parser.parse_args()json_path = arg.json_paththreshold = 0.5label_path_src = '/ssd01/wanghuijiao/dataset/human_head_vehicle_RGB/Open_imagesV6/v2.0/labels' # 原始标签路径,请根据需求自行更改label_path_dir = arg.label_path_dir # '/ssd01/wanghuijiao/dataset/human_head_vehicle_RGB/Open_imagesV6/v2.0/labels' # 生成的新标签文件放置的路径,请根据需求自行更改img_path = '/ssd01/wanghuijiao/dataset/Open_imagesV6/person_car/images'# crowdhuman head detector# category_ids = {'0': 'head'} # '1': human# COCO 80 yolov4-officialcategory_ids ={1: "person", 3: "car", 6: "bus", 7: "train", 8: "truck"} # 这个可以筛选json的目标标签,比如json有80类结果,但只需要1,3,6,7,8这些类。# 0:person, 1:car, 2:headwith open(json_path, 'r') as f:for k, line in tqdm(enumerate(f.readlines())):if k == 0 or k == len(f.readlines()):continueline = line.strip()[:-1]# print(line)data = ast.literal_eval(line)if data['score'] > threshold:print(data['bbox'])image_id = data['image_id']category_id = data['category_id']if category_id not in category_ids.keys():continue# print('category_id: ', category_id)bbox = data['bbox']bbox[0] = float(bbox[0])bbox[1] = float(bbox[1])bbox[2] = float(bbox[2])bbox[3] = float(bbox[3])img_file = os.path.join(img_path, f'{image_id}.jpg')img = cv2.imread(img_file)label_file_src = os.path.join(label_path_src, f'{image_id}.txt')img_height = img.shape[0]img_width = img.shape[1]label_file_dir = os.path.join(label_path_dir, f'{image_id}.txt')with open(label_file_dir, 'a+') as f:head_box = convert([img_width, img_height], bbox)content = str('1') + ' ' + str(head_box[0])+ ' ' + str(head_box[1]) + ' ' + \str(head_box[2]) + ' ' + str(head_box[3]) + '\n' # 这里是生成的目标类别的新标签,请根据需求自行更改# print(content)f.write(content)