forked from marian-margeta/gait-recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
79 lines (54 loc) · 1.91 KB
/
utils.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
import numpy as np
from functools import lru_cache
from scipy.stats import multivariate_normal
@lru_cache()
def get_gauss_pdf(sigma):
n = sigma * 8
x, y = np.mgrid[0:n, 0:n]
pos = np.empty(x.shape + (2,))
pos[:, :, 0] = x
pos[:, :, 1] = y
rv = multivariate_normal([n / 2, n / 2], [[sigma ** 2, 0], [0, sigma ** 2]])
pdf = rv.pdf(pos)
heatmap = pdf / np.max(pdf)
return heatmap
def to_int(num):
return int(round(num))
@lru_cache()
def get_binary_mask(diameter):
d = diameter
_map = np.zeros((d, d), dtype = np.float32)
r = d / 2
s = int(d / 2)
y, x = np.ogrid[-s:d - s, -s:d - s]
mask = x * x + y * y <= r * r
_map[mask] = 1.0
return _map
def get_binary_heat_map(shape, is_present, centers, diameter = 9):
n = diameter
r = int(n / 2)
hn = int(2 * n)
qn = int(4 * n)
pl = np.zeros((shape[0], shape[1] + qn, shape[2] + qn, shape[3]), dtype = np.float32)
for i in range(shape[0]):
for j in range(shape[3]):
my = centers[i, 0, j] - r
mx = centers[i, 1, j] - r
if -n < my < shape[1] and -n < mx < shape[2] and is_present[i, j]:
pl[i, my + hn:my + 3 * n, mx + hn:mx + 3 * n, j] = get_binary_mask(diameter)
return pl[:, hn:-hn, hn:-hn, :]
def get_gauss_heat_map(shape, is_present, mean, sigma = 5):
n = to_int(sigma * 8)
hn = to_int(n / 2)
dn = int(2 * n)
qn = int(4 * n)
pl = np.zeros((shape[0], shape[1] + qn, shape[2] + qn, shape[3]), dtype = np.float32)
for i in range(shape[0]):
for j in range(shape[3]):
my = mean[i, 0, j] - hn
mx = mean[i, 1, j] - hn
if -n < my < shape[1] and -n < mx < shape[2] and is_present[i, j]:
pl[i, my + dn:my + 3 * n, mx + dn:mx + 3 * n, j] = get_gauss_pdf(sigma)
# else:
# print(my, mx)
return pl[:, dn:-dn, dn:-dn, :]