一、
h5py是Python语言用来操作HDF5的模块。
二、一个HDF5文件就是一个容器,用于储存两类对象:datasets,类似于数组的数据集合;groups,类似于文件夹的容器,可以储存datasets和其它groups。
密集人群检测与计数中
代码首先都是要对数据集进行预处理
1.你就像我电脑上的Context-Aware_Crowd_Counting-pytorch-master这个代码里面的k_nearest_gaussian_kernel.py,里面的代码如下:就是高斯核卷积的生成
import numpy as np
import scipy
import scipy.io as io
import scipy.spatial
from scipy.ndimage.filters import gaussian_filter
import os
import glob
from matplotlib import pyplot as plt
import h5py
import PIL.Image as Image
from matplotlib import cm as CM
#partly borrowed from https://github.com/davideverona/deep-crowd-counting_crowdnet
def gaussian_filter_density(img,points):
'''
This code use k-nearst, will take one minute or more to generate a density-map with one thousand people.
points: a two-dimension list of pedestrians' annotation with the order [[col,row],[col,row],...].
img_shape: the shape of the image, same as the shape of required density-map. (row,col). Note that can not have channel.
return:
density: the density-map we want. Same shape as input image but only has one channel.
example:
points: three pedestrians with annotation:[[163,53],[175,64],[189,74]].
img_shape: (768,1024) 768 is row and 1024 is column.
'''
img_shape=[img.shape[0],img.shape[1]]
print(img_shape)
print("Shape of current image: ",img_shape,". Totally need generate ",len(points),"gaussian kernels.")
density = np.zeros(img_shape, dtype=np.float32)
gt_count = len(points)
#没人的话
if gt_count == 0:
return density
leafsize = 2048
# build kdtree
tree = scipy.spatial.KDTree(points.copy(), leafsize=leafsize)
# query kdtree
#distances:离查询点最近的距离,locations:离查询点最近的点的相应的索引。
distances, locations = tree.query(points, k=4)#返回4个最近邻居。
print ('generate density...')
for i, pt in enumerate(points):
# print("开始输出pt")
# print(pt) 第一张图片找的第一个人头的point的[col,row]是:[ 29.6225116 472.92022152]
pt2d = np.zeros(img_shape, dtype=np.float32)
#因为points是[col,row],而img是[row,col]所以这样比较。
if int(pt[1]) 1:
#mcnn这里B取得的是0.3,(distances[i][1]+distances[i][2]+distances[i][3])*0.1就相当于求平均距离然后乘0.3
sigma = (distances[i][1]+distances[i][2]+distances[i][3])*0.1
else:
sigma = np.average(np.array(gt.shape))/2./2. #case: 1 point
#高斯核函数!
density += scipy.ndimage.filters.gaussian_filter(pt2d, sigma, mode='constant')
print ('done.')
return density
# test code
if __name__=="__main__":
# show an example to use function generate_density_map_with_fixed_kernel.
root = 'C:\\Users\\haojie\\OneDrive\\桌面\\ShanghaiTech_Crowd_Counting_Dataset'
# now generate the ShanghaiA's ground truth
part_A_train = os.path.join(root,'part_A_final/train_data','images')
part_A_test = os.path.join(root,'part_A_final/test_data','images')
# part_B_train = os.path.join(root,'part_B_final/train_data','images')
# part_B_test = os.path.join(root,'part_B_final/test_data','images')
path_sets = [part_A_train,part_A_test]
img_paths = []
for path in path_sets:
#查找文件夹下所有的.jpg图片。
for img_path in glob.glob(os.path.join(path, '*.jpg')):
img_paths.append(img_path)
for img_path in img_paths:
print(img_path)
mat = io.loadmat(img_path.replace('.jpg','.mat').replace('images','ground_truth').replace('IMG_','GT_IMG_'))
img= plt.imread(img_path)#768行*1024列(不是灰度图,三原色所以是(768, 1024, 3))
k = np.zeros((img.shape[0],img.shape[1]))
print(mat) #(768,1024)
points = mat["image_info"][0,0][0,0][0] #1546person*2(col,row)的意思是第一张图片有1546个人头,每个人以col,row表示。
#第一张图片生成的points的shape为(1546,2),dtype = float64
k = gaussian_filter_density(img,points)
# plt.imshow(k,cmap=CM.jet)
# save density_map to disk
np.save(img_path.replace('.jpg','.npy').replace('images','ground_truth'), k)
'''
#now see a sample from ShanghaiA
plt.imshow(Image.open(img_paths[0]))
gt_file = np.load(img_paths[0].replace('.jpg','.npy').replace('images','ground_truth'))
plt.imshow(gt_file,cmap=CM.jet)
print(np.sum(gt_file))# don't mind this slight variation
'''
2.就像FIDT那个模型的处理
他进行两个操作,数据预处理主要是第一个操作
run python fidt_generate_sh.py
import glob
import math
import os
import torch
import cv2
import h5py
import numpy as np
import scipy.io as io
import scipy.spatial
from scipy.ndimage.filters import gaussian_filter
'''change your path'''
root = r'D:\project\Crowd Counting\FIDTM-master\ShanghaiTech_Crowd_Counting_Dataset'
part_A_train = os.path.join(root, 'part_A_final/train_data', 'images')
part_A_test = os.path.join(root, 'part_A_final/test_data', 'images')
part_B_train = os.path.join(root, 'part_B_final/train_data', 'images')
part_B_test = os.path.join(root, 'part_B_final/test_data', 'images')
path_sets = [part_A_train, part_A_test, part_B_train, part_B_test]
if not os.path.exists(part_A_train.replace('images', 'gt_fidt_map')):
os.makedirs(part_A_train.replace('images', 'gt_fidt_map'))
if not os.path.exists(part_A_test.replace('images', 'gt_fidt_map')):
os.makedirs(part_A_test.replace('images', 'gt_fidt_map'))
if not os.path.exists(part_A_train.replace('images', 'gt_show')):
os.makedirs(part_A_train.replace('images', 'gt_show'))
if not os.path.exists(part_A_test.replace('images', 'gt_show')):
os.makedirs(part_A_test.replace('images', 'gt_show'))
if not os.path.exists(part_B_train.replace('images', 'gt_fidt_map')):
os.makedirs(part_B_train.replace('images', 'gt_fidt_map'))
if not os.path.exists(part_B_test.replace('images', 'gt_fidt_map')):
os.makedirs(part_B_test.replace('images', 'gt_fidt_map'))
if not os.path.exists(part_B_train.replace('images', 'gt_show')):
os.makedirs(part_B_train.replace('images', 'gt_show'))
if not os.path.exists(part_B_test.replace('images', 'gt_show')):
os.makedirs(part_B_test.replace('images', 'gt_show'))
img_paths = []
for path in path_sets:
for img_path in glob.glob(os.path.join(path, '*.jpg')):
img_paths.append(img_path)
img_paths.sort()
def fidt_generate1(im_data, gt_data, lamda):
size = im_data.shape
new_im_data = cv2.resize(im_data, (lamda * size[1], lamda * size[0]), 0)
new_size = new_im_data.shape
d_map = (np.zeros([new_size[0], new_size[1]]) + 255).astype(np.uint8)
gt = lamda * gt_data
for o in range(0, len(gt)):
x = np.max([1, math.floor(gt[o][1])])
y = np.max([1, math.floor(gt[o][0])])
if x >= new_size[0] or y >= new_size[1]:
continue
d_map[x][y] = d_map[x][y] - 255
distance_map = cv2.distanceTransform(d_map, cv2.DIST_L2, 0)
distance_map = torch.from_numpy(distance_map)
distance_map = 1 / (1 + torch.pow(distance_map, 0.02 * distance_map + 0.75))
distance_map = distance_map.numpy()
distance_map[distance_map < 1e-2] = 0
return distance_map
for img_path in img_paths:
print(img_path)
Img_data = cv2.imread(img_path)
mat = io.loadmat(img_path.replace('.jpg', '.mat').replace('images', 'ground_truth').replace('IMG_', 'GT_IMG_'))
Gt_data = mat["image_info"][0][0][0][0][0]
fidt_map1 = fidt_generate1(Img_data, Gt_data, 1)
kpoint = np.zeros((Img_data.shape[0], Img_data.shape[1]))
for i in range(0, len(Gt_data)):
if int(Gt_data[i][1]) < Img_data.shape[0] and int(Gt_data[i][0]) < Img_data.shape[1]:
kpoint[int(Gt_data[i][1]), int(Gt_data[i][0])] = 1
with h5py.File(img_path.replace('.jpg', '.h5').replace('images', 'gt_fidt_map'), 'w') as hf:
hf['fidt_map'] = fidt_map1
hf['kpoint'] = kpoint
fidt_map1 = fidt_map1
fidt_map1 = fidt_map1 / np.max(fidt_map1) * 255
fidt_map1 = fidt_map1.astype(np.uint8)
fidt_map1 = cv2.applyColorMap(fidt_map1, 2)
'''for visualization'''
cv2.imwrite(img_path.replace('images', 'gt_show').replace('jpg', 'jpg'), fidt_map1)
python make_npydata.py