当前位置:   article > 正文

YOLOPOSE算法COCOkeypoints关键点标注YOLO格式转COCO格式,供coco annotator标注软件导入_yolov8-pose的关键点数据集转为coco格式

yolov8-pose的关键点数据集转为coco格式
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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117

通过coco annotator导入后

在这里插入图片描述

注意:Datasets要设置category才会显示关键点信息

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/97828
推荐阅读
相关标签
  

闽ICP备14008679号