【目标检测】小脚本:YOLO标签可视化

需求分析

在下载别人标注好的目标检测数据集时,我突然想到一个问题:怎么直观得看别人标注的是否正确呢?于是我想到了可以利用opencv将标注数据还原到原图上。
更具体的说,指定图片和标签文件夹,批量输出还原后的图片。

需求实现

由于没找到完全符合我需求的脚本,于是在前人的基础上,新增了批量修改,颜色修改等功能,满足了我的需求。
注意标签须是YOLO所需要的txt类型。

import os
import numpy as np
import cv2

# 修改输入图片文件夹
img_folder = "images/"
img_list = os.listdir(img_folder)
img_list.sort()
# 修改输入标签文件夹
label_folder = "labels/"
label_list = os.listdir(label_folder)
label_list.sort()
# 输出图片文件夹位置
path = os.getcwd()
output_folder = path + '/' + str("output")
os.mkdir(output_folder)

colormap = [(0, 255, 0), (132, 112, 255), (0, 191, 255)]  # 色盘,可根据类别添加新颜色


# 坐标转换
def xywh2xyxy(x, w1, h1, img):
    label, x, y, w, h = x
    # print("原图宽高:\nw1={}\nh1={}".format(w1, h1))
    # 边界框反归一化
    x_t = x * w1
    y_t = y * h1
    w_t = w * w1
    h_t = h * h1
    # print("反归一化后输出:\n第一个:{}\t第二个:{}\t第三个:{}\t第四个:{}\t\n\n".format(x_t, y_t, w_t, h_t))
    # 计算坐标
    top_left_x = x_t - w_t / 2
    top_left_y = y_t - h_t / 2
    bottom_right_x = x_t + w_t / 2
    bottom_right_y = y_t + h_t / 2

    # print('标签:{}'.format(labels[int(label)]))
    # print("左上x坐标:{}".format(top_left_x))
    # print("左上y坐标:{}".format(top_left_y))
    # print("右下x坐标:{}".format(bottom_right_x))
    # print("右下y坐标:{}".format(bottom_right_y))
    # 绘制矩形框
    cv2.rectangle(img, (int(top_left_x), int(top_left_y)), (int(bottom_right_x), int(bottom_right_y)), colormap[1], 2)
    """
    # (可选)给不同目标绘制不同的颜色框
    if int(label) == 0:
       cv2.rectangle(img, (int(top_left_x), int(top_left_y)), (int(bottom_right_x), int(bottom_right_y)), (0, 255, 0), 2)
    elif int(label) == 1:
       cv2.rectangle(img, (int(top_left_x), int(top_left_y)), (int(bottom_right_x), int(bottom_right_y)), (255, 0, 0), 2)
    """
    return img


if __name__ == '__main__':
    for i in range(len(img_list)):
        image_path = img_folder + "/" + img_list[i]
        label_path = label_folder + "/" + label_list[i]
        # 读取图像文件
        img = cv2.imread(str(image_path))
        h, w = img.shape[:2]
        # 读取 labels
        with open(label_path, 'r') as f:
            lb = np.array([x.split() for x in f.read().strip().splitlines()], dtype=np.float32)
        # 绘制每一个目标
        for x in lb:
            # 反归一化并得到左上和右下坐标,画出矩形框
            img = xywh2xyxy(x, w, h, img)
        """
        # 直接查看生成结果图
        cv2.imshow('show', img)
        cv2.waitKey(0)
        """
        cv2.imwrite(output_folder + '/' + '{}.png'.format(image_path.split('/')[-1][:-4]), img)

文件结构

在这里插入图片描述

  • 25
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
您可以通过在训练过程中使用TensorBoard来可视化YOLO模型的训练情况。以下是一些步骤来实现这一点: 1. 安装TensorFlow和TensorBoard:首先,您需要安装TensorFlow和TensorBoard库。您可以使用pip命令来安装它们: ``` pip install tensorflow tensorboard ``` 2. 导入必要的库:在Python代码中,您需要导入以下库: ```python import tensorflow as tf from tensorflow.keras.callbacks import TensorBoard ``` 3. 创建TensorBoard回调函数:在模型训练之前,您需要创建一个TensorBoard回调函数,并将其传递给模型训练过程中的fit()函数。这将记录并保存训练过程中的相关信息,以供TensorBoard使用。 ```python tensorboard_callback = TensorBoard(log_dir='./logs', histogram_freq=1) ``` 在上面的示例中,'./logs' 是保存TensorBoard日志文件的目录。 4. 将回调函数添加到模型训练过程中:在您的模型训练代码中,将TensorBoard回调函数作为callbacks参数传递给fit()函数: ```python model.fit(train_dataset, epochs=10, callbacks=[tensorboard_callback]) ``` 确保您的模型已经定义和编译,并且您有一个用于训练的数据集。 5. 启动TensorBoard服务器:在命令行中,导航到您保存TensorBoard日志文件的目录,并运行以下命令来启动TensorBoard服务器: ``` tensorboard --logdir=./logs ``` 上述命令将启动TensorBoard服务器,并将其绑定到本地主机的默认端口(通常是6006)。 6. 访问TensorBoard可视化:打开您的Web浏览器,并输入以下URL以访问TensorBoard的可视化界面: ``` http://localhost:6006 ``` 请注意,如果您在步骤5中使用了不同的端口,则需要相应地更改URL中的端口号。 通过遵循上述步骤,您将能够使用TensorBoard来可视化YOLO模型的训练情况。您可以查看各种指标、损失函数、准确率和其他相关信息的图表和摘要。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zstar-_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值