文章目录
- windows安装环境
- 打开labelme
- 自动保存勾选上,保存图片数据不要勾选
- 选SAM精准模型,然后打开图片路径,然后点击创建AI多边形:
- 鼠标点击确认物体控制点,确认完成后,双击鼠标完成选取,并给上标签。
- 这样就有对应json了:
- json转为yolotxt
- 验证对不对看图
windows安装环境
# py310
# torch>=1.7
# torchvision>=0.8
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
pip install appdirs
pip install opencv-python-headless
pip install git+https://github.com/facebookresearch/segment-anything.gitpip install labelme
打开labelme
自动保存勾选上,保存图片数据不要勾选
或者修改C:\Users\Administrator.labelmerc的默认配置为:
auto_save: true
display_label_popup: true
store_data: false
keep_prev: false
keep_prev_scale: false
keep_prev_brightness: false
keep_prev_contrast: false
logger_level: info
选SAM精准模型,然后打开图片路径,然后点击创建AI多边形:
鼠标点击确认物体控制点,确认完成后,双击鼠标完成选取,并给上标签。
这样就有对应json了:
json转为yolotxt
执行这个代码,输入是json文件夹路径、yolo txt保存路径,输出就是将json转为yolotxt:
import os
import jsondef convert_to_yolo(json_path, dst_path, label_dict):# 打开JSON文件with open(json_path, 'r') as f:data = json.load(f)# 获取图片的宽度和高度img_width = data['imageWidth']img_height = data['imageHeight']# 打开目标txt文件dst_file_path = os.path.join(dst_path, os.path.splitext(os.path.basename(json_path))[0] + '.txt')with open(dst_file_path, 'w') as dst_file:# 遍历多边形标记for shape in data['shapes']:label = shape['label']# 如果标签是新的,为其分配一个新的yolo标签数字if label not in label_dict:label_dict[label] = len(label_dict)# 获取yolo标签数字yolo_label = label_dict[label]# 获取多边形的点坐标points = shape['points']# 断言是多边形"shape_type": "polygon",assert shape['shape_type'] == 'polygon'# 计算多边形的矩形包裹框x_min = min(point[0] for point in points)y_min = min(point[1] for point in points)x_max = max(point[0] for point in points)y_max = max(point[1] for point in points)# 计算矩形中心点的归一化坐标x_center = (x_min + x_max) / (2 * img_width)y_center = (y_min + y_max) / (2 * img_height)width = (x_max - x_min) / img_widthheight = (y_max - y_min) / img_height# round 6x_center = round(x_center, 6)y_center = round(y_center, 6)width = round(width, 6)height = round(height, 6)# 将数据写入到txt文件中dst_file.write(f"{yolo_label} {x_center} {y_center} {width} {height}\n")def convert_folder_to_yolo(src_folder, dst_folder):# 如果想自己自定义标签数字,可以修改为label_dict= {'person': 0, 'car': 1, ...} 这种形式label_dict = {}# 遍历文件夹中的所有文件for filename in os.listdir(src_folder):if filename.endswith('.json'):json_path = os.path.join(src_folder, filename)convert_to_yolo(json_path, dst_folder, label_dict)print("Label与YOLO标签数字的字典:")print(label_dict)# 用法示例
# json 路径
src_folder = r'C:\Users\Administrator\Pictures\car'
# yolo txt文件保存路径
dst_folder = r'C:\Users\Administrator\Pictures\car'
convert_folder_to_yolo(src_folder, dst_folder)
验证对不对看图
输入图片文件夹和yolotxt文件夹,看看yolotxt对不对:
import osimport cv2def draw_boxes(image_path, yolo_txt_path):# 读取图像image = cv2.imread(image_path)if image is None:print(f"Error: Unable to read image from {image_path}")return# 打开YOLO格式的txt文件with open(yolo_txt_path, 'r') as file:lines = file.readlines()# 遍历每行数据for line in lines:# 解析每行数据parts = line.strip().split(' ')yolo_label = int(parts[0])x_center, y_center, width, height = map(float, parts[1:])# 计算矩形左上角和右下角的坐标x_min = int((x_center - width / 2) * image.shape[1])y_min = int((y_center - height / 2) * image.shape[0])x_max = int((x_center + width / 2) * image.shape[1])y_max = int((y_center + height / 2) * image.shape[0])# 获取标签label = str(yolo_label)# 绘制矩形框和标签cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)cv2.putText(image, label, (x_min, y_min - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)# 显示图像cv2.imshow("Image", image)cv2.waitKey(0)cv2.destroyAllWindows()# 用法示例
image_path = r'C:\Users\Administrator\Pictures\car'
# yolo txt文件保存路径
yolo_txt_path = r'C:\Users\Administrator\Pictures\car'images_files = [f for f in os.listdir(image_path) if f.lower().endswith(('.jpg', '.png', '.jpeg'))]
images_files = [os.path.join(image_path, f) for f in images_files]
for img_path in images_files:yolo_txt_path = os.path.splitext(img_path)[0] + '.txt'draw_boxes(img_path, yolo_txt_path)
很对: