-
Notifications
You must be signed in to change notification settings - Fork 6
/
make_dataset.py
143 lines (110 loc) · 4.52 KB
/
make_dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Borrowed from https://github.com/leeyeehoo/CSRNet-pytorch
import h5py
import scipy.io as io
import PIL.Image as Image
import numpy as np
import os
import glob
from matplotlib import pyplot as plt
from scipy.ndimage.filters import gaussian_filter
import scipy
import json
from matplotlib import cm as CM
from tqdm import tqdm
from numba import cuda
import cv2
import tensorflow as tf
#this is borrowed from https://github.com/davideverona/deep-crowd-counting_crowdnet
# @cuda.autojit()
def gaussian_filter_density(gt):
#print (gt.shape)
density = np.zeros(gt.shape, dtype=np.float32)
gt_count = np.count_nonzero(gt)
if gt_count == 0:
return density
pts = np.array(np.c_[np.nonzero(gt)[1], np.nonzero(gt)[0]])
leafsize = 2048
# build kdtree
tree = scipy.spatial.KDTree(pts.copy(), leafsize=leafsize)
# query kdtree
distances, locations = tree.query(pts, k=4)
# print ('generate density...')
for i, pt in (enumerate(pts)):
pt2d = np.zeros(gt.shape, dtype=np.float32)
pt2d[pt[1],pt[0]] = 1.
if gt_count > 1:
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
# set the root to the Shanghai dataset
root = '/home/saivinay/Documents/jipmer-crowd-analysis/shanghai_dataset/'
# now generate the ShanghaiA's ground truth
part_A_train = os.path.join(root,'part_A/train_data','images')
part_A_test = os.path.join(root,'part_A/test_data','images')
part_B_train = os.path.join(root,'part_B/train_data','images')
part_B_test = os.path.join(root,'part_B/test_data','images')
path_sets = [part_A_train,part_A_test]
count_path = os.path.join(root,'part_A/train_data/count/')
img_paths = []
for path in path_sets:
for img_path in glob.glob(os.path.join(path, '*.jpg')):
img_paths.append(img_path)
for img_path in tqdm(img_paths):
mat = io.loadmat(img_path.replace('.jpg','.mat').replace('images','ground-truth').replace('IMG_','GT_IMG_'))
# print (mat.keys())
lab_path = img_path.replace('.jpg','.npy').replace('images','labels').replace('IMG_','LAB_')
img = plt.imread(img_path)
k = np.zeros((img.shape[0],img.shape[1]))
gt = mat["image_info"][0,0][0,0][0]
''' img = cv2.imread(img_path)
for i in range(len(gt)):
if (int(gt[i][0])<=img.shape[0] and int(gt[i][1])<=img.shape[1]):
img[int(gt[i][1]),int(gt[i][0])] = [255,255,255 ]
img = np.asarray(img).astype('uint8')
print(img.shape)
plt.imshow(img)
plt.show()
# print((gt))
exit(0) '''
###### saving the number of people counted in corresponding image at count_path #####
count_path = img_path.replace('.jpg','.npy').replace('images','count').replace('IMG_','COUNT_')
np.save(count_path,len(gt))
##### saving the heatmaps generated from images at lab_path #####
if not os.path.exists(path=lab_path):
# continue
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)
np.save(lab_path,k)
# plt.imshow(k, cmap='jet')
# plt.show()
# with h5py.File(img_path.replace('.jpg','.h5').replace('images','ground_truth'), 'w') as hf:
# hf['density'] = k
#now see a sample from ShanghaiA
plt.imshow(Image.open(img_paths[0]))
gt_file = h5py.File(img_paths[0].replace('.jpg','.h5').replace('images','ground-truth'),'r')
groundtruth = np.asarray(gt_file['density'])
plt.imshow(groundtruth,cmap=CM.jet)
np.sum(groundtruth)# don't mind this slight variation
#now generate the ShanghaiB's ground truth
path_sets = [part_B_train,part_B_test]
img_paths = []
for path in path_sets:
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)
k = np.zeros((img.shape[0],img.shape[1]))
gt = mat["image_info"][0,0][0,0][0]
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(k,15)
with h5py.File(img_path.replace('.jpg','.h5').replace('images','ground-truth'), 'w') as hf:
hf['density'] = k