Yolopose关键点检测:自己标注数据集,制作数据集
import os
import cv2
import numpy as np
import random
# 创建标注文件夹
def create_annotation_folder(dataset_path):
annotations_dir = os.path.join(dataset_path, "annotations")
if not os.path.exists(annotations_dir):
os.makedirs(annotations_dir)
return annotations_dir
# 生成关键点标注文件
def generate_keypoint_annotation(annotations_dir, image_path, keypoints):
annotation_file_name = os.path.basename(image_path).split('.')[0] + '.txt'
annotation_file_path = os.path.join(annotations_dir, annotation_file_name)
with open(annotation_file_path, 'w') as f:
for kp in keypoints:
f.write('{} {} {}\n'.format(kp[0], kp[1], kp[2]))
# 生成随机关键点
def generate_random_keypoints(image_size, num_keypoints):
height, width = image_size
keypoints = []
for _ in range(num_keypoints):
x = np.random.randint(0, width)
y = np.random.randint(0, height)
v = random.choice([0, 1]) # visibility: 0=not labeled, 1=labeled
keypoints.append((x, y, v))
return keypoints
# 示例使用
dataset_path = "/path/to/your/dataset" # 替换为你的数据集路径
annotations_dir = create_annotation_folder(dataset_path)
image_path = "/path/to/your/image.jpg" # 替换为你的图片路径
image = cv2.imread(image_path)
height, width = image.shape[:2]
keypoints = generate_random_keypoints((height, width), 10) # 生成10个随机关键点
generate_keypoint_annotation(annotations_dir, image_path, keypoints)
这段代码首先定义了创建标注文件夹的函数,然后是生成关键点标注文件的函数,随后是生成随机关键点的函数。最后,我们提供了一个示例使用这些函数来为一张图片生成随机关键点并保存标注。这个过程可以被扩展来为数据集中的所有图片重复使用。
评论已关闭