ground_truth_box.py
import cv2
data_path = r"E:/shipinjihe/WILD_ANIMAL/AnimalData/TheGreatFox/groundtruth_rect.txt" #文本文档所在目录v groundtruth_rect
image_path = "E:/shipinjihe/WILD_ANIMAL/AnimalData/TheGreatFox/img2" #图片所在目录,使用“/”,而不是“\”
with open(data_path) as fb: #将数据逐行读取
lines = fb.readlines()
for i in range(1,1527):
if i<10: #对图片和数据进行循环(注意:第二个数是 图片数+1)
image_path1 = image_path+"/000"+str(i)+".jpg" #图片的绝对路径
elif i<100:
image_path1 = image_path+"/00"+str(i)+".jpg"
elif i<1000:
image_path1 = image_path+"/0"+str(i)+".jpg"
else:
image_path1 = image_path+"/"+str(i)+".jpg"
image = cv2.imread(image_path1) #循环读取图片
value = lines[i-1].split(',') #将数据按“,”进行分割
result = [i.rstrip() for i in value]
x_min = round(float(result[0])) #以下四行建立方框的坐标点
y_min = round(float(result[1]))
x_max = round(float(result[0])) + round(float(result[2]))
y_max = round(float(result[1])) + round(float(result[3]))
print(x_min, y_min, x_max, y_max)
first_point = (x_min, y_min)
last_point = (x_max, y_max)
cv2.rectangle(image, first_point, last_point,(0,0,255), 2) #在图片中构建方框
cv2.imwrite(image_path1, image) #保存图片
# BGR 红gt (0, 0, 255) 绿stark (0, 255, 0) 紫 transt (240, 32, 160) 青siamban (139, 139, 0) 黄GFSDCF(0,255,255) 蓝ASRCF(255,0,0) 橙STRCF(0,165,255)