最近做牛的骨架提取相关工作,首先要把coco中的关键点坐标转化为yolopose的txt格式,折腾了挺久,参考文章(1条消息) COCO(.json)格式 转换为 YOLO(.txt)格式训练(详细介绍,避坑贴)_Study->Java的博客-CSDN博客_coco转yolo,加了几行实现了转化关键点的功能,代码如下:
def convert(size,box,kp):
box[0] = round((box[0] + box[2] / 2.0) / (size[0]), 6)
box[1] = round((box[1] + box[3] / 2.0) / (size[1]), 6)
box[2] = round((box[2]) / (size[0]), 6)
box[3] = round((box[3]) / (size[1]), 6)
kp[0::3] = list(map(lambda x: round(x/(size[0]), 6), kp[0::3]))
kp[1::3] = list(map(lambda x: round(x/size[1], 6), kp[1::3]))
lr=' '.join(str(i) for i in (box+kp))
return lr
import os
import json
from tqdm import tqdm
import argparse
parser = argparse.ArgumentParser()
# 这里根据自己的json文件位置,换成自己的就行
parser.add_argument('--json_path',
default='', type=str,
help="input: coco format(json)")
# 这里设置.txt文件保存位置
parser.add_argument('--save_path', default='', type=str,
help="specify where to save the output dir of labels")
arg = parser.parse_args()
if __name__ == '__main__':
json_file = arg.json_path # COCO Object Instance 类型的标注
ana_txt_save_path = arg.save_path # 保存的路径
data = json.load(open(json_file, 'r'))
if not os.path.exists(ana_txt_save_path):
os.makedirs(ana_txt_save_path)
id_map = {} # coco数据集的id不连续!重新映射一下再输出!
with open(os.path.join(ana_txt_save_path, 'classes.txt'), 'w') as f:
# 写入classes.txt
for i, category in enumerate(data['categories']):
f.write(f"{category['name']}\n")
id_map[category['id']] = i
# print(id_map)
# 这里需要根据自己的需要,更改写入图像相对路径的文件位置。
list_file = open(os.path.join(ana_txt_save_path, 'train2017.txt'), 'w')
for img in tqdm(data['images']):
filename = img["file_name"]
img_width = img["width"]
img_height = img["height"]
img_id = img["id"]
head, tail = os.path.splitext(filename)
ana_txt_name = head + ".txt" # 对应的txt名字,与jpg一致
f_txt = open(os.path.join(ana_txt_save_path, ana_txt_name), 'w')
for ann in data['annotations']:
if ann['image_id'] == img_id:
l = convert((img_width, img_height), ann["bbox"],ann['keypoints'])
#f_txt.write("%s %s %s %s %s\n" % (id_map[ann["category_id"]], box[0], box[1], box[2], box[3]))
f_txt.write("%s %s \n" % (id_map[ann["category_id"]], l))
f_txt.close()
# 将图片的相对路径写入train2017或val2017的路径
list_file.write('./images/val2017/%s.jpg\n' % (head))
list_file.close()
得到的转化的txt格式如下,第一个数字代表类别默认为0,二到五个数字代表矩形框的坐标,后面数字为17个关键点,数量为17*3(x,y,置信度)个 。
0 0.153409 0.771701 0.303977 0.338542 0.0 0.0 0 0.0 0.0 0 0.0 0.0 0 0.039773 0.755208 2 0.0 0.0 0 0.052557 0.838542 2 0.0 0.0 0 0.066761 0.913194 2 0.0 0.0 0 0.090909 0.602431 2 0.22017 0.604167 2 0.232955 0.756944 2 0.21733 0.784722 1 0.271307 0.814236 2 0.227273 0.819444 2 0.295455 0.913194 2 0.22017 0.904514 2
但是最后得到的效果并不是很好,左边为真实标签,右边预测标签。