格式输出为OTB格式,代码如下:
# 定义图像文件夹路径和对应txt文件的前缀
image_folder = 'D:/traker_resuts/rpnrfnttban9-1/'#原始图像文件夹
txt_file = 'D:/traker_resuts/tt/test_txt/ours11-1.txt'#预测txt文件
# 获取所有图像文件的路径
image_files = [os.path.join(image_folder, f) for f in os.listdir(image_folder) if
f.endswith('.jpg')] # 假设图像文件以.jpg为扩展名
# 读取txt文件,获取所有标注的坐标信息
with open(txt_file, 'r') as f:
annotations = f.readlines()
# 遍历所有行和所有图像
for index, annotation in enumerate(annotations):
# 获取当前行的数
print(index)
if 'nan' in annotation:
x1, y1, w, h =0,0,0,0
else:
x1, y1, w, h = map(int, annotation.strip().split(','))
# 获取当前图像文件的路
#current_image = image_files[index]#按图像文件夹索引
current_image = image_folder+str(index)+'.jpg'#image_files[index]#按图像名称0,1,2..的顺序。
# 加载图像
image = cv2.imread(current_image)
x2 = x1 + w
y2 = y1 + h
# 在图像上绘制矩形框(假设第五行包含四个整数,分别代表矩形左上角和右下角的坐标(x1, y1, x2, y2))
cv2.rectangle(image, (x1, y1), (x2, y2), (0,0,255), 2) # 在图像上绘制红色矩形框
# 保存绘制了矩形框的图像文件(不覆盖原图)
output_file = 'D:/traker_resuts/rpnrfnttbanour9-1/' + str(index) + '.jpg' # 输出图像文件"
cv2.imwrite(output_file, image)