【代码】YOLOv8标注信息验证

此代码的功能是标注信息验证,将原图和YOLOv8标注文件(txt)放在同一个文件夹中,作为输入文件夹

程序将标注的信息还原到原图中,并将原图和标注后的图像一同保存,以便查看

两个draw_labels函数,分别是将目标区域轮廓标出来或者颜色填充

python 复制代码
import cv2
import numpy as np
import os

def read_txt_labels(txt_file):
    """
    从 txt 标注文件中读取标签
    :param txt_file: txt 标注文件路径
    :return: 标签列表
    """
    with open(txt_file, "r") as f:
      labels = []
      for line in f.readlines():
        label_data = line.strip().split(" ")
        class_id = int(label_data[0])
        # 解析边界框坐标
        coordinates = [float(x) for x in label_data[1:]]
        labels.append([class_id, coordinates])
    return labels


def draw_labels(image, labels):
  """
  在图像上绘制分割区域轮廓
  :param image: 图像
  :param labels: 标签列表
  """
  for label in labels:
    class_id, coordinates = label
    # 将坐标转换为整数并重新塑形为多边形
    points = [(int(x * image.shape[1]), int(y * image.shape[0])) for x, y in zip(coordinates[::2], coordinates[1::2])]
    # 使用多边形绘制轮廓
    cv2.polylines(image, [np.array(points)], True, (0, 0, 255), 2)  # 红色表示分割区域轮廓


# def draw_labels(image, labels):
#   """
#   在图像上绘制分割区域
#   :param image: 图像
#   :param labels: 标签列表
#   """
#   for label in labels:
#     class_id, coordinates = label
#     # 将坐标转换为整数并重新塑形为多边形
#     points = [(int(x * image.shape[1]), int(y * image.shape[0])) for x, y in zip(coordinates[::2], coordinates[1::2])]
#     # 使用多边形填充
#     cv2.fillPoly(image, [np.array(points)], (0, 255, 0))  # 绿色表示分割区域


def process_and_save_image(image_path, txt_path, output_folder):
    # 读取图像
    image = cv2.imread(image_path)
    labels = read_txt_labels(txt_path)
    # 复制原始图像以进行绘制
    image_with_labels = image.copy()
    # 绘制分割区域
    draw_labels(image_with_labels, labels)
    # 创建一个新的图像,左侧是原图,右侧是带标注的图
    combined_image = np.concatenate((image, image_with_labels), axis=1)
    # 定义输出图像路径
    base_filename = os.path.basename(image_path)
    output_image_path = os.path.join(output_folder, base_filename)
    # 保存图像
    cv2.imwrite(output_image_path, combined_image)

def main(input_folder, output_folder):
    # 确保输出文件夹存在
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    # 获取输入文件夹中的所有图片文件
    image_files = [f for f in os.listdir(input_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
    for image_file in image_files:
        # 构建图像和标注文件的路径
        image_path = os.path.join(input_folder, image_file)
        txt_file = os.path.splitext(image_path)[0] + '.txt'
        # 检查标注文件是否存在
        if os.path.exists(txt_file):
            process_and_save_image(image_path, txt_file, output_folder)
        else:
            print(f"标注文件 {txt_file} 不存在,跳过图像 {image_file}")

if __name__ == "__main__":
    input_folder_path = 'D:\Desktop\images'  # 替换为实际的输入文件夹路径
    output_folder_path = 'D:\Desktop\images_02'  # 替换为实际的输出文件夹路径
    main(input_folder_path, output_folder_path)

示例:


相关推荐
qq_252941316812 分钟前
航拍垃圾堆检测数据集 | 航拍垃圾检测 固废管理 无人机巡检 环保监测9025期
yolo·目标检测·计算机视觉·分类·无人机·垃圾检测·垃圾
成都佳洋光电科技有限公司17 分钟前
一文了解机器视觉成像
图像处理·人工智能·工业相机·工业镜头·短波红外
星月夜语30 分钟前
2026 TPAMI | StarIR:空间-频域融合的高效图像复原
图像处理·深度学习·计算机视觉
爱吃火鸡面呀1 小时前
图片拼接与答题卡判断对错:从图像处理到自动判卷的完整实践
图像处理·人工智能
qq_25294131681 小时前
建筑物缺陷目标检测数据集 | 建筑裂缝检测 房屋缺陷 结构损伤 目标检测9022期
人工智能·yolo·目标检测·计算机视觉·视觉检测·建筑裂缝·建筑外立面
java1234_小锋2 小时前
YOLO26 计算机视觉 - YOLO26 模型架构解析
人工智能·yolo·计算机视觉·机器视觉·yolo26
AndrewHZ4 小时前
图像处理入门018 | 简易图像浏览器开发:用 OpenCV + Tkinter 打造你的第一个图像 GUI 工具
图像处理·人工智能·opencv·canvas·tkinter·事件绑定
YOLO数据集集合4 小时前
无人机视角交通目标检测数据集 | 无人机感知 交通检测 城市监控 目标检测 YOLO格式 深度学习数据集 9016期
人工智能·深度学习·yolo·目标检测·计算机视觉·无人机·交通数据集
小张帅三代5 小时前
yolo基础学习笔记
笔记·学习·yolo
智购科技智能售货柜1 天前
2026自动售货机峰值交易流量应对方案:从消息削峰到弹性扩容的工程实践~YH
数据库·人工智能·单片机·嵌入式硬件·yolo