数据集格式如下【标注的真差!!!
Dataset/ArtificialLight/image-116.png, 845, 104, 104, 126, 929, 795, 104, 118,
Dataset/ArtificialLight/image-117.png, 606, 250, 118, 127, 540, 93, 111, 106, 797, 167, 103, 121,
Dataset/ArtificialLight/image-118.png, 751, 33, 132, 144, 309, 82, 120, 130, 522, 312, 127, 133, 369, 677, 149, 155, 770, 219, 117, 133, 897, 18, 116, 113, 492, 678, 123, 144, 89, 297, 129, 111,
Dataset/ArtificialLight/image-119.png, 736, 382, 133, 146, 742, 722, 102, 119, 1175, 157, 100, 109, 1088, 264, 101, 122, 458, 573, 98, 119, 141, 583, 129, 134, 786, 476, 115, 149,
Dataset/ArtificialLight/image-120.png, 786, 45, 139, 157, 1039, 71, 128, 133, 150, 29, 139, 122,
Dataset/ArtificialLight/image-121.png, 735, 455, 132, 134, 962, 294, 101, 101,
代码如下
import torch
import numpy as np
import matplotlib.pyplot as plt
from d2l import torch as d2l
imgPath = "/liu/WSU_apple_dataset/ArtificialLight/image-33.png"
labelPath = "/liu/WSU_apple_dataset/ArtificialLight/AnnoArtificialLight.txt"
image_list = []
with open(labelPath,"r") as file:
pictures = file.readlines()
for i in pictures:
img = i.strip().split(',')
image_list.append(img[0].split('/')[-1])
gt_list = []
with open(labelPath,'r') as file:
pictures = file.readlines()
for i in pictures:
gt_list.append(i.strip().split(',')[1:-1:])
def list_split(items, n):
for i in range(len(items)):
items[i] = int(items[i].strip())
return [items[i:i+n] for i in range(0, len(items), n)]
gt = []
for i in gt_list:
tem = list_split(i,4)
gt.append(tem)
img = d2l.plt.imread(imgPath)
d2l.set_figsize()
d2l.plt.imshow(img);
#@save
def bbox_to_rect(bbox, color):
return d2l.plt.Rectangle(xy=(bbox[0], bbox[1]), width=bbox[2],
height=bbox[3], fill=False,
edgecolor=color, linewidth=0.5)
def show_bbox(img,apples,color):
fig = d2l.plt.imshow(img)
for i in apples:
fig.axes.add_patch(bbox_to_rect(i,color))
index = imgPath.split(".")[0].split("-")[1]
show_bbox(img,gt[int(index)-1],'red')
#plt.savefig("2.png",dpi=300)
plt.show()
效果如下