-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathidTrack.py
97 lines (72 loc) · 2.55 KB
/
idTrack.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
import numpy as np
import kdtree
from sklearn.cluster import DBSCAN
class Bot(object):
def __init__(self, x, y, id):
self.coords = (x, y)
self.id = id
self.pos = None
self.neg = None
self.id = id
self.angle = None
def __getitem__(self, i):
return self.coords[i]
def __len__(self):
return len(self.coords)
def __repr__(self):
return 'Item(x:{}, y:{}, pos:{}, neg:{}, id:{}, angle:{})'.format(self.coords[0], self.coords[1], self.pos, self.neg, self.id, self.angle)
class Clusterer():
def __init__(self, eps, minpts):
self.eps = eps
self.minpts = minpts
def run(self, data):
x, y = np.where(data != 255)
points = np.column_stack((x, y))
clusters = []
try:
clustering = DBSCAN(
eps=self.eps,
min_samples=self.minpts,
algorithm='kd_tree',
).fit(points)
# list of every cluster, including noise (assigned to -1).
found_clusters = set(clustering.labels_)
# remove noise
found_clusters.remove(-1)
# get each label class in it's own cluster collection
for i, each in enumerate(found_clusters):
points_with_class = np.column_stack(
(points, clustering.labels_))
detected_pixel_indexes = np.where(
points_with_class[:, 2] == each)
detected_pixels = points_with_class[detected_pixel_indexes]
clusters.append(detected_pixels[:, 0:2])
return clusters
except Exception as e:
# we allow this to fail gracefully if no detections are found
print('Error:{}'.format(e))
return clusters
class KD_Tree():
def __init__(self):
self.tree = kdtree.create(dimensions=2)
self.nextID = 0
def asList(self):
return list(self.tree.inorder())
def addNode(self, x, y):
# make a bot with new data
bot = Bot(x, y, self.nextID)
# add to tree
self.tree.add(bot)
# increment nextID counter
self.nextID +=1
def updateNode(self, x, y, neighbor):
# save the neighbor's id
id = neighbor[0].data.id
# remove old match
self.tree = self.tree.remove(neighbor[0].data)
# make a bot with new coordinates and old ID
bot = Bot(x, y, id)
# insert bot to tree
self.tree.add(bot)
def NN(self, x, y):
return self.tree.search_nn((x,y))