最近在做鱼群密度的工作,采用CSRNet进行。
ShanghaiTech数据集的标注文件是mat,且每个mat包含一个struct,我自己的文件没有struct,所以对代码稍微改动一下。
shanghaitech标注格式
我自己的标注格式
for img_path in img_paths:
print (img_path)
mat = io.loadmat(img_path.replace('.jpg','.mat').replace('images1','mat').replace('',''))
img= plt.imread(img_path)
k = np.zeros((img.shape[0],img.shape[1]))
gt = mat["data"]
for i in range(0,len(gt)):
if int(gt[i][1])<img.shape[0] and int(gt[i][0])<img.shape[1]:
k[int(gt[i][1]),int(gt[i][0])]=1
k = gaussian_filter_density(k)
with h5py.File(img_path.replace('.jpg','.h5').replace('images1','mat'), 'w') as hf:
hf['density'] = k
#测试
print('总数量=',len(gt))
print('密度图=',gt_density_map.sum())
简单的把gt那一行改了一下,可以生成密度图。
简单记录一下,省得以后忘了,哈哈。