项介绍:
使用YOLOv8关键点检测之前需要进行标注,在人体标注的时候由于每个关键点都是不一样的,在标注时需要进行区分各个关键点。但是如果标注大量相同等级的关键点时,如果进行分别标注效率会变低,因此考虑进行统一标注后,再修改其标签,具体如下:
问题描述
在标注具有相同等级的关键点时,无法对每个点进行区分,且一个一个选标签耽误时间。
人体的关键点都是不相同的
而对于章鱼脚来说,各个脚的都是相同等级的关键点!!
如果想要进行区分标注十分困难,因此考虑统一标注相同的关键点,后续进行修改!!!!
1.首选统一标注如下:
2.得到标注JSON文件
{
"version": "5.1.1",
"flags": {},
"shapes": [
{
"label": "foot",
"points": [
[
176.1094674556213,
45.95857988165682
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
},
{
"label": "foot",
"points": [
[
335.28106508875743,
42.70414201183433
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
},
{
"label": "foot",
"points": [
[
400.9615384615385,
216.66863905325448
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
},
{
"label": "foot",
"points": [
[
264.2751479289941,
324.9526627218935
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
},
{
"label": "foot",
"points": [
[
117.52958579881656,
208.97633136094677
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
}
],
3.运行修改代码
import os
import json
# 载入JSON数据
def load_json(json_path):
with open(json_path, 'r') as file:
data = json.load(file)
return data
# 判断点是否在矩形框内
def is_point_in_rect(point, rect):
x_min = min(rect[0][0], rect[1][0])
x_max = max(rect[0][0], rect[1][0])
y_min = min(rect[0][1], rect[1][1])
y_max = max(rect[0][1], rect[1][1])
return x_min <= point[0] <= x_max and y_min <= point[1] <= y_max
# 处理JSON数据,寻找并重命名关键点
def process_data(data):
for shape in data['shapes']:
if shape['shape_type'] == 'rectangle':
# 找到矩形框
rect = shape['points']
# 寻找矩形内的所有关键点
seed_heads = [s for s in data['shapes'] if s['shape_type'] == 'point' and s['label'] == 'Seed_head' and is_point_in_rect(s['points'][0], rect)]
# 对关键点进行排序并重新命名
seed_heads.sort(key=lambda x: (x['points'][0][0], x['points'][0][1])) # 按x坐标和y坐标排序
for idx, head in enumerate(seed_heads):
head['label'] = f'foot{idx + 1}'
# 主函数
def main():
input_folder = 'keypoint' # 输入文件夹路径
output_folder = 'keypoint1' # 输出文件夹路径
# 确保输出文件夹存在,若不存在则创建
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 遍历输入文件夹中的所有文件
for filename in os.listdir(input_folder):
if filename.endswith('.json'):
json_path = os.path.join(input_folder, filename)
output_path = os.path.join(output_folder, filename)
data = load_json(json_path)
process_data(data)
# 保存修改后的数据
with open(output_path, 'w') as f:
json.dump(data, f, indent=4)
main()
需要修改的位置!!!!!!!!!
seed_heads = [s for s in data['shapes'] if s['shape_type'] == 'point' and s['label'] == 'foot' and is_point_in_rect(s['points'][0], rect)]#将foot修改成你的标签
head['label'] = f'foot{idx + 1}'#将foot修改成你的标签
input_folder = 'keypoint' # 输入文件夹路径
output_folder = 'keypoint1' # 输出文件夹路径
结果:
下面是修改后的JSON文件
{
"version": "5.1.1",
"flags": {},
"shapes": [
{
"label": "foot2",
"points": [
[
176.1094674556213,
45.95857988165682
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
},
{
"label": "foot4",
"points": [
[
335.28106508875743,
42.70414201183433
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
},
{
"label": "foot5",
"points": [
[
400.9615384615385,
216.66863905325448
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
},
{
"label": "foot3",
"points": [
[
264.2751479289941,
324.9526627218935
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
},
{
"label": "foot1",
"points": [
[
117.52958579881656,
208.97633136094677
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
},
{
"label": "star",
"points": [
[
102.22868217054264,
30.984496124031068
],
[
414.63178294573646,
339.5116279069768
]
],
"group_id": null,
"shape_type": "rectangle",
"flags": {}
}
],
标签修改成功
总结
1.标注时可以选用相同名称进行标注节省时间
2.使用代码对JSON文件进行修改
1.找到框
2.找到所有点
3.判断哪个点在哪个框里后修改
3.完成标注关键点