在ubuntu系统把txt文件的坐标打印到原图上,检测是否标注准确。

本文介绍了一个Python脚本,用于YOLOV5_obb旋转目标检测中的自我检查,通过读取标注文件,将标注框以不同颜色绘制在图片上,便于检查框的准确性。
摘要由CSDN通过智能技术生成

        在做YOLOV5_obb旋转目标检测时,需要检测自己所标注的框是否标注准确,可以运用这个代码进行自我检查,这段代码仅支持做旋转目标检测时的自我检测。

import os
import cv2

# 输入文件夹路径
txt_folder = '/home/zhang/yolov5_obb_master/old_new/labels/'
img_folder = '/home/zhang/yolov5_obb_master/old_new/images/'

# 输出文件夹路径
output_folder = '/home/zhang/yolov5_obb_master/draw_frame'

# 创建输出文件夹
os.makedirs(output_folder, exist_ok=True)

color_map = {
    1: (0, 0, 255),  # 红色
    2: (0, 255, 0),  # 绿色
    3: (255, 0, 0),  # 蓝色
    4: (255, 255, 0)  # 黄色
}

# 遍历txt文件夹和图片文件夹
for txt_filename in os.listdir(txt_folder):
    if txt_filename.endswith('.txt'):
        # 构造txt文件路径
        txt_filepath = os.path.join(txt_folder, txt_filename)
        # 从txt文件中读取坐标数据
        with open(txt_filepath, 'r') as file:
            lines = file.readlines()
        # 构造对应的图片文件路径
        img_filename = os.path.splitext(txt_filename)[0] + '.jpg'
        img_filepath = os.path.join(img_folder, img_filename)
        # 读取图片
        image = cv2.imread(img_filepath)
        # 处理每行数据
        for line in lines:
            data = line.strip().split(' ')
            x1, y1, x2, y2, x3, y3, x4, y4 = map(int, map(float, data[:8]))
            image = cv2.circle(image, (x1, y1), 3, color_map[1], -1)
            image = cv2.circle(image, (x2, y2), 3, color_map[2], -1)
            image = cv2.circle(image, (x3, y3), 3, color_map[3], -1)
            image = cv2.circle(image, (x4, y4), 3, color_map[4], -1)
            image = cv2.line(image, (x1, y1), (x2, y2), color_map[1], 3)
            image = cv2.line(image, (x2, y2), (x3, y3), color_map[2], 3)
            image = cv2.line(image, (x3, y3), (x4, y4), color_map[3], 3)
            image = cv2.line(image, (x4, y4), (x1, y1), color_map[4], 3)
        # 保存结果到输出文件夹
        output_filepath = os.path.join(output_folder, img_filename)
        cv2.imwrite(output_filepath, image)

# 显示结果
cv2.imshow('Points and Boxes', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值