目标检测中将得到的txt文件标注在图片上:
import os
import cv2 as cv
path = ‘/hy-tmp/OTB2015/OTB100/Bolt/img/’ #原数据集图片保存位置
save_path = ‘/hy-tmp/Ex/Bolt/’ # 生成结果保存位置
list = os.listdir(path)
list.sort(key= lambda x:int(x[:-4]))
lines = []
with open(‘/hy-tmp/OTB2015/OTB100/Bolt/groundtruth_rect.txt’, ‘r’) as f: # 找到结果的txt
for line in f:
lines.append(line.strip(‘\n’).split(‘\t’))
print(list)
for j in range(len(list)):
listname = list[j]
filename_d =lines[j]
x=filename_d[0].split(“,”)
a = x[0]
b = x[1]
c = x[2]
d = x[3]
filename = ‘’.join(filename_d)
print(a,b,c,d)
x=int(float(a))
y=int(float(b))
w=int(float(c))
h=int(float(d))
print(x, y, w, h)
img1 = cv.imread(path + listname)
img = cv.rectangle(img1, (int(x), int(y)), (int(x) + int(w), int(y) + int(h)), (0, 0, 255), 2)
isExists = os.path.exists(save_path)
if not isExists:
os.mkdir(save_path)
cv.imwrite(save_path + '/' + filename, img)
print(save_path + '/' + filename)
else:
cv.imwrite(save_path + listname, img)
print(save_path+ filename)
`
我是使用的OTB数据集,其他的数据应该也是一样的。