目录
一、代码、权重下载
YOLOv8代码:ultralytics/ultralytics首页 - GitCode
环境安装
pip install ultralytics
关键点训练权重下载:
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n-pose.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8s-pose.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8m-pose.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8l-pose.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8x-pose.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8x-pose-p6.pt
二、标注规则
标签组成:类型,1个数据;目标框坐标,4个数据;关键点,n*3个数据(n为关键点个数,'3'为坐标及是否可见,点标签:0代表此点不在图像上,点标签:1 代表此点在图像上且在未遮挡处,点标签:2 代表此点在图像上但在遮挡处)
0 0.07390752032520326 0.5280939476061427 0.1384654471544715 0.22244805781391144 0.14059959349593495 0.5489837398373983 0 0.04024390243902439 0.5354336043360434 2 0.006580284552845528 0.6133468834688347 1 0.11709857723577236 0.6122177055103883 1
0 0.11709857723577234 0.30451671183378504 0.0431910569105691 0.09146341463414637 0.10312499999999998 0.3163730803974706 2 0.10566565040650407 0.32540650406504057 1 0.13678861788617885 0.3220189701897019 0 0.13678861788617885 0.3141147244805781 0
0 0.7535315040650405 0.857813911472448 0.49034552845528445 0.28116531165311665 0.8964430894308942 0.99726738934056 0 0.9967987804878048 0.9679087624209575 0 0.6404725609756097 0.8753161698283649 2 0.5108993902439024 0.9679087624209575 1
0 0.982981220657277 0.212245696400626 0.02787558685446001 0.06585811163275952 0.9807805164319249 0.220396452790819 2 0.9697769953051644 0.220396452790819 0 0.9705105633802817 0.22691705790297342 0 0.9936179577464789 0.22756911841418886 1
三、标注要求及步骤
(1)标注工具:labelme
(2)按顺序先标注车辆长方形包围拉框,再标记车辆ABCD四个顶点(备注 :若拉框顺序与标点顺序反了,请重新标注)
(3)四个车底盘顶点顺序
逆时针:A点、B点、C点、D点
(4)顶点标签
0表示没有显露出不可见,1表示可见,2表示被遮挡不可见
四、json标签样式
{
"version": "5.0.1",
"flags": {},
"shapes": [
{
"label": "car",
"points": [
[
8.97560975609756,
450.2195121951219
],
[
274.8292682926829,
690.4634146341463
]
],
"group_id": null,
"shape_type": "rectangle",
"flags": {}
},
{
"label": "0",
"points": [
[
269.9512195121951,
592.9024390243902
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
},
{
"label": "2",
"points": [
[
77.26829268292683,
578.2682926829268
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
},
{
"label": "1",
"points": [
[
12.634146341463413,
662.4146341463414
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
},
{
"label": "1",
"points": [
[
224.8292682926829,
661.1951219512194
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
}
],
"imagePath": "1127_52.jpg",
"imageData":
"imageHeight": 1080,
"imageWidth": 1920
}
五、json转txt格式
要求:标注步骤必须按照先拉框,然后按逆时针标注关键点生成json文件
转换脚本
import json
import os
import argparse
import cv2
from tqdm import tqdm
import numpy as np
#标签类别字典
# 0: car
dictlist=['car']
def convert(img_size, box):
dw = 1. / (img_size[0])
dh = 1. / (img_size[1])
x = (box[0] + box[2]) / 2.0
y = (box[1] + box[3]) / 2.0
w = abs(box[2] - box[0])
h = abs(box[3] - box[1])
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)
def convert_label_json(json_dir, img_path, save_dir):
json_paths = os.listdir(json_dir)
for json_path in tqdm(json_paths):
path = os.path.join(json_dir,json_path)
with open(path,'r',encoding='UTF-8') as load_f:
json_dict = json.load(load_f)
img_w = json_dict['imageWidth'] # 图片的高
img_h = json_dict['imageHeight'] # 图片的宽
txt_path = os.path.join(save_dir, json_path.replace('json', 'txt'))
with open(txt_path, 'w') as txt_file:
points_nor_list = []
fournum=1
for shape_dict in json_dict["shapes"]:
if shape_dict["shape_type"]=="rectangle":
x1 = float(shape_dict['points'][0][0])
y1 = float(shape_dict['points'][0][1])
x2 = float(shape_dict['points'][1][0])
y2 = float(shape_dict['points'][1][1])
bb = (x1, y1, x2, y2)
bbox = convert((img_w, img_h), bb)
label1=shape_dict["label"]
no_0=dictlist.index(label1)
line=np.append(str(no_0),bbox)
if shape_dict["shape_type"]=="point":
x0=float(shape_dict['points'][0][0])/img_w
y0=float(shape_dict['points'][0][1])/img_h
pointlist=[x0,y0,shape_dict["label"]]
line=np.append(line,pointlist)
if fournum%5==0:
stringline=' '.join(line)
stringline=stringline+'\n'
txt_file.writelines(stringline)
fournum+=1
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='json convert to txt params')
parser.add_argument('--json-dir', type=str, default=r"C:xxxxxxxxxxxxx", help='json path dir')#标签路径
parser.add_argument('--img-path', type=str, default=r"C:xxxxxxxxxxxxx", help='json path dir')#图片路径
parser.add_argument('--save-dir', type=str, default=r"C:xxxxxxxxxxxxx", help='txt save dir')#txt保存路径
args = parser.parse_args()
json_dir = args.json_dir
img_path = args.img_path
save_dir = args.save_dir
convert_label_json(json_dir, img_path, save_dir)