Datasets:

Modalities:
Image
Text
ArXiv:
License:
NightPro commited on
Commit
3383315
·
verified ·
1 Parent(s): d7d00d4

Upload DroneBird/toolkit/make_data_density.py

Browse files
DroneBird/toolkit/make_data_density.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -- coding: utf-8 --
2
+ import h5py
3
+ import scipy
4
+ import scipy.io as io
5
+ import numpy as np
6
+ import os
7
+ import glob
8
+ from matplotlib import pyplot as plt
9
+ from scipy import spatial, ndimage
10
+ from multiprocessing import Pool
11
+ from functools import partial
12
+ import time
13
+ import json
14
+
15
+
16
+ def gaussian_filter_density(gt):
17
+ density = np.zeros(gt.shape, dtype=np.float32)
18
+ gt_count = np.count_nonzero(gt)
19
+ if gt_count == 0:
20
+ return density
21
+
22
+ pts = np.array(list(zip(np.nonzero(gt)[1], np.nonzero(gt)[0])))
23
+ leafsize = 2048
24
+ # build kdtree
25
+ tree = spatial.KDTree(pts.copy(), leafsize=leafsize)
26
+ # query kdtree
27
+ distances, locations = tree.query(pts, k=4)
28
+
29
+ for i, pt in enumerate(pts):
30
+ pt2d = np.zeros(gt.shape, dtype=np.float32)
31
+ pt2d[pt[1], pt[0]] = 1.0
32
+ if gt_count > 1:
33
+ sigma = (distances[i][1] + distances[i][2] + distances[i][3]) * 0.1
34
+ else:
35
+ sigma = np.average(np.array(gt.shape)) / 2.0 / 2.0 # case: 1 point
36
+ sigma = 6
37
+ density += scipy.ndimage.filters.gaussian_filter(pt2d, sigma, mode="constant")
38
+
39
+ return density
40
+
41
+
42
+ def process(idx, img_paths):
43
+ start = time.time()
44
+ img_path = img_paths[idx]
45
+ mat_path = (
46
+ img_path.replace(".jpg", ".mat")
47
+ .replace("images", "ground_truth")
48
+ .replace("img", "GT_img")
49
+ )
50
+ mat = io.loadmat(mat_path)
51
+ img = plt.imread(img_path)
52
+ k = np.zeros((int(img.shape[0] / 2), int(img.shape[1] / 2)))
53
+ gt = mat["locations"]
54
+ for i in range(0, len(gt)):
55
+ if (
56
+ int(gt[i][1] / 2) < img.shape[0] / 2
57
+ and int(gt[i][0] / 2) < img.shape[1] / 2
58
+ ):
59
+ k[int(gt[i][1] / 2), int(gt[i][0] / 2)] = 1
60
+ k = gaussian_filter_density(k)
61
+
62
+ with h5py.File(mat_path.replace("mat", "h5"), "w") as hf:
63
+ hf["density"] = k
64
+ end = time.time()
65
+ print(idx, len(img_paths), img_path, str(end - start) + "s")
66
+
67
+
68
+ if __name__ == "__main__":
69
+
70
+ img_paths = []
71
+ data_path = "."
72
+ for img_path in glob.glob(os.path.join(data_path, "*/images", "*.jpg")):
73
+ h5_path = (
74
+ img_path.replace(".jpg", ".h5")
75
+ .replace("images", "ground_truth")
76
+ .replace("img", "GT_img")
77
+ )
78
+ if not os.path.exists(h5_path):
79
+ img_paths.append(img_path)
80
+
81
+ img_paths.sort()
82
+ print(img_paths)
83
+ print(len(img_paths))
84
+
85
+ pool = Pool(10)
86
+ partial = partial(process, img_paths=img_paths)
87
+ _ = pool.map(partial, range(len(img_paths)))