当前位置: 首页 > news >正文

YoloV8如何将标签绘制在原图中

为了对比训练结果和标签是否一致,有时候需要可视化标签图像进行对比查看,可通过如下代码查看自己标签在图像中的具体框

import os
import cv2
import numpy as np# 输入和输出路径
images_path = 'images/val'  # 图像文件夹路径
labels_path = 'labels/val'  # 标签文件夹路径
output_path = 'output'      # 输出文件夹路径# 确保输出文件夹存在
os.makedirs(output_path, exist_ok=True)# 获取所有图像文件
image_files = [f for f in os.listdir(images_path) if f.endswith(('.bmp', '.jpg', '.jpeg', '.png'))]for image_file in image_files:# 读取图像image_path = os.path.join(images_path, image_file)image = cv2.imread(image_path)height, width, _ = image.shape# 标签文件路径(假设标签文件名与图像文件名相同但扩展名为.txt)label_file = os.path.splitext(image_file)[0] + '.txt'label_path = os.path.join(labels_path, label_file)# 如果标签文件存在,读取标签if os.path.exists(label_path):with open(label_path, 'r') as f:lines = f.readlines()for line in lines:# 解析标签:类别 左上x 左上y 右上x 右上y 右下x 右下y 左下x 左下yparts = line.strip().split()class_id = int(parts[0])points = list(map(float, parts[1:]))# 归一化坐标转换为图像坐标points = [(int(p[0] * width), int(p[1] * height)) for p in zip(points[::2], points[1::2])]# 绘制多边形points = [(points[0][0], points[0][1]), (points[1][0], points[1][1]), (points[2][0], points[2][1]), (points[3][0], points[3][1])]cv2.polylines(image, [np.array(points, np.int32)], isClosed=True, color=(0, 255, 0), thickness=2)# 保存标注后的图像output_image_path = os.path.join(output_path, image_file)cv2.imwrite(output_image_path, image)

http://www.mrgr.cn/news/18150.html

相关文章:

  • 测试用例的具体方法
  • 燃油车淘汰倒计时开始了?
  • element-plus 报错 ResizeObserver loop limit exceeded 解决
  • C语言学习笔记 Day15(文件管理--下)
  • Nginx: 配置文件重载的原理和热部署
  • 【Oracle点滴积累】解决IMP-00017、ORA-20005、ORA-06512错误的方法
  • 代码随想录冲冲冲 Day34 动态规划Part2
  • 智慧理财新纪元:蚂蚁集团“支小宝”智能理财助理深度体验与测评
  • Notepad下载安装
  • JavaWeb(基于SpringBoot开发)全总结(1)
  • DORIS - DORIS注意事项(一)
  • 基于VUE的校园二手物品交易管理系统的设计与实现 (含源码+sql+视频导入教程)
  • 【influxdb 第二篇章】docker-compose、windows、linux 安装
  • 【卡码网C++基础课 16.出现频率最高的字母】
  • 惠中科技:开启综合光伏清洗新征程
  • Elasticsearch:使用 inference API 进行语义搜索
  • 自制efficientnet网络
  • 新一代网络研发利器——开物™,让用户每一行代码都贡献在核心创新
  • wlanapi.dll丢失怎么办?有没有什么靠谱的修复wlanapi.dll方法
  • U-Mail垃圾邮件过滤网关‍是如何过滤垃圾邮件的?