YOLOv8 pose关键节点以及边框box(xywh)实际坐标提取

# txt文件格式

# 第一行是类别 # 第二行是box值xywh # 其余的是关键节点的预测坐标

0 
0.512225 0.505719 0.452323 0.906863 
0.480425 0.126976 
0.50274 0.112235 
0.455859 0.111146 
0.536355 0.125047 
0.416919 0.122777 
0.590721 0.223357 
0.362933 0.225949 
0.623477 0.339576 
0.336917 0.352184 
0.470382 0.324783 
0.50851 0.327616 
0.569603 0.478832 
0.401842 0.480703 
0.602918 0.679878 
0.394951 0.681951 
0.644524 0.884723 
0.384306 0.88459
 
  

# 以下是主程序
def yolo2coordinates(x,y,w1,h1,img_w,img_h):
    xmin = round(img_w*(x-w1/2.0), 2)
    xmax = round(img_w*(x+w1/2.0), 2)
    ymin = round(img_h*(y-h1/2.0), 2)
    ymax = round(img_h*(y+h1/2.0), 2)
    print(xmin,ymin, xmax, ymax)
    return xmin, ymin, xmax,ymax
img_w = 260
img_h = 565
def yolokeypoint(x, y, img_w, img_h):
    x0 = round((1-x)*img_w, 2)
    y0 = round((1-y)*img_h, 2)
    print(x0, y0)
with open(r'C:\Users\Administrator\Desktop\predict\labels\R-C.txt', 'r', encoding='utf-8') as infile:# 这个文件保存的是box以及关键节点的坐标
    data2 = []
    for line in infile:
        data_line = line.strip("\n").split()  # 去除首尾换行符,并按空格划分
        # print(data_line)
        data2.append([float(i) for i in data_line])
    # print(data2)
for i in range(len(data2)):
    if i == 0:
        continue
    elif i == 1:
        yolo2coordinates(data2[1][0], data2[1][1], data2[1][2], data2[1][3], img_w, img_h)
    else:
        if len(data2[i])>2:
            continue
        else:
            yolokeypoint(data2[i][0], data2[i][1], img_w, img_h)

你可能感兴趣的:(YOLO,python,深度学习,算法)