-
Notifications
You must be signed in to change notification settings - Fork 18
/
fakenews.py
75 lines (55 loc) · 3.06 KB
/
fakenews.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
import numpy as np
import lib
class FakeNeuralNewsNetwork:
def __init__(self, dets, shape=(33, 60)):
self.already_tracked_ids = [[], [], [], [], [], [], [], []]
self.dets = dets
self.shape = shape
def tick(self, curr_frame):
self.curr_dets = lib.slice_all(self.dets, self.dets['GFIDs'] == curr_frame)
def fake_camera(self, icam):
self.curr_cam_dets = lib.slice_all(self.curr_dets, self.curr_dets['Cams'] == icam)
self.fake_curr_cam = icam
def embed_crop(self, crop, fake_id):
return fake_id
def embed_image(self, image):
return None # z.B. (30,60,128)
def search_person(self, img_embs, person_emb, fake_track_id):
id_det_boxes = self.curr_cam_dets['boxes'][self.curr_cam_dets['TIDs'] == fake_track_id]
return self._heatmap_sampling_for_dets(id_det_boxes)
def personness(self, image, known_embs, return_pose=False):
already_tracked_ids = self.already_tracked_ids[self.fake_curr_cam - 1]
new_det_indices = np.where(np.logical_not(np.in1d(self.curr_cam_dets['TIDs'], already_tracked_ids)))[0]
new_heatmaps_and_ids = []
for each_det_idx in new_det_indices:
det = self.curr_cam_dets['boxes'][each_det_idx]
#new_heatmap = self._heatmap_sampling_for_dets([det])
new_heatmap = self._one_fake_gaussian(lib.rebox_centered(det, det[3]*2, det[2]*2, bounds=None))
if return_pose:
new_heatmap = (new_heatmap, lib.box_center_xy(lib.box_rel2abs(det, h=self.shape[0], w=self.shape[1])))
new_id = self.curr_cam_dets['TIDs'][each_det_idx]
already_tracked_ids.append(new_id)
new_heatmaps_and_ids.append((new_heatmap, new_id))
return new_heatmaps_and_ids
def _one_fake_gaussian(self, box_rel):
# NOTE: There certainly is a better way, but I'm tired.
heatmap = np.zeros(self.shape)
box_abs = lib.box_rel2abs(box_rel, h=self.shape[0], w=self.shape[1])
x, y = lib.box_center_xy(box_abs)
heatmap[int(round(y)),int(round(x))] = 1
_, __, w, h = box_abs
return lib.convolve_edge_zeropad(heatmap, lib.gauss2d_xy([[(w/4)**2, 0], [0, (h/4)**2]], nstd=2))
def _heatmap_sampling_for_dets(self, dets_boxes):
heatmap = np.random.rand(*self.shape)
for l, t, w, h in dets_boxes:
# score is how many times more samples than pixels in the detection box.
score = np.random.randint(1,5)
add_idx = np.random.multivariate_normal([l+w/2, t+h/2], [[(w/6)**2, 0], [0, (h/6)**2]], int(np.prod(heatmap.shape)*h*w*score))
np.add.at(heatmap, [[int(np.clip(y, 0, 0.999)*self.shape[0]) for x,y in add_idx],
[int(np.clip(x, 0, 0.999)*self.shape[1]) for x,y in add_idx]], 1)
return lib.softmax(heatmap)
def fix_shape(self, net_output, orig_shape, out_shape, fill_value=0):
if net_output.shape == out_shape:
return np.array(net_output)
else:
return lib.resize_map(net_output, out_shape, interp='bicubic')