当前位置:   article > 正文

【深度学习Labelme】使用Segment Anything Model (SAM)快速打标,labelme多边形转yolo txt框看看对不对_labelme segmentanything

labelme segmentanything

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.git

pip install  labelme
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

打开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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

选SAM精准模型,然后打开图片路径,然后点击创建AI多边形:

在这里插入图片描述

鼠标点击确认物体控制点,确认完成后,双击鼠标完成选取,并给上标签。

在这里插入图片描述

这样就有对应json了:

在这里插入图片描述

json转为yolotxt

执行这个代码,输入是json文件夹路径、yolo txt保存路径,输出就是将json转为yolotxt:

import os
import json


def 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_width
            height = (y_max - y_min) / img_height

            # round 6
            x_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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

验证对不对看图

输入图片文件夹和yolotxt文件夹,看看yolotxt对不对:

import os

import cv2


def 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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

很对:
在这里插入图片描述

问询、帮助

你如果需要帮助,请看这里:

https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/812807
推荐阅读
相关标签
  

闽ICP备14008679号