YOLOPose将json关键点转化为txt格式

最近做牛的骨架提取相关工作,首先要把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 

但是最后得到的效果并不是很好,左边为真实标签,右边预测标签。

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
JSON换为YOLO格式标签需要经过以下步骤: 1. 将JSON文件加载到Python中。 2. 对于每个图像,将其宽度和高度读入。 3. 对于每个对象,将其类别ID换为YOLO格式。 4. 计算每个对象的中心点坐标和宽度高度。 5. 将所有对象的YOLO格式标签写入一个文本文件。 下面是一个示例Python代码来实现这个换: ``` import json # 加载JSON文件 with open('annotations.json', 'r') as f: data = json.load(f) # 获取图像宽度和高度 width, height = data['imageWidth'], data['imageHeight'] # 换每个对象的类别ID为YOLO格式 class_map = {'cat': 0, 'dog': 1} # 假设类别ID为0和1 yolo_labels = [] for obj in data['annotations']: class_id = class_map[obj['label']] x_min, y_min, x_max, y_max = obj['BoundingBox'] x_center = (x_min + x_max) / 2 / width y_center = (y_min + y_max) / 2 / height w = (x_max - x_min) / width h = (y_max - y_min) / height yolo_labels.append(f"{class_id} {x_center:.6f} {y_center:.6f} {w:.6f} {h:.6f}") # 写入YOLO格式标签文件 with open('labels.txt', 'w') as f: f.write('\n'.join(yolo_labels)) ``` 在这个例子中,我们假设JSON文件包含一个名为`annotations.json`的图像注释文件,其中类别ID为`cat`和`dog`。我们还假设图像的宽度和高度可以在JSON文件中找到,并且我们将YOLO格式标签写入名为`labels.txt`的文件中。最后,我们使用`join()`方法将所有标签连接为单个字符串,并将其写入文件中。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值