在做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()