forked from raffaelemazziotti/oc_chamber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cooTracker.py
executable file
·145 lines (114 loc) · 3.94 KB
/
cooTracker.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
143
144
145
import time
import numpy as np
import os
import csv
import cv2
# Output file extension
EXT='.txt'
# THIS CLASS MANAGES TRACKING DATA
# COORDINATES WITH TIMESTAMP AND INFO
class coo():
def __init__(self,x=0,y=0,t=None,info='NaN'):
self.x = x
self.y = y
if t:
self.time = t
else:
self.time = time.time()
self.info = info
def tostr(self):
if type(self.info) != str:
info = str(self.info)
else:
info = self.info
return str(self.time) + '\t' + str(self.x) + '\t' + str(self.y) + '\t' + info
# LIST OF COORDINATES WITH TIMESTAMP
class coolist():
def __init__(self,coo = None,rect=None):
self.coos=list()
self.rect=rect
if coo:
self.coos.append(coo)
def append(self,coo):
if self.rect is not None:
if coo.x<self.rect[0]:
coo.x=self.rect[0]
if coo.y<self.rect[1]:
coo.y=self.rect[1]
if coo.x>self.rect[2]:
coo.x=self.rect[2]
if coo.y>self.rect[3]:
coo.y=self.rect[3]
self.coos.append(coo)
def add(self,x,y,t=None,info='NaN'):
if self.rect is not None:
if x<self.rect[0]:
x=self.rect[0]
if y<self.rect[1]:
y=self.rect[1]
if x>self.rect[2]:
x=self.rect[2]
if y>self.rect[3]:
y=self.rect[3]
self.coos.append(coo(x,y,t,info=info))
def tot(self):
return len(self.coos)
# RETURNS A LIST OF (X,Y) COORDINATES
def xy(self):
res= list()
for ci in self.coos:
res.append((ci.x,ci.y))
return res
def x(self):
return [x[0] for x in self.xy()]
def y(self):
return [y[1] for y in self.xy()]
# RETURNS A LIST OF TIMESTAMPS
def t(self):
res= list()
for ci in self.coos:
res.append(ci.time)
return res
# WRITES TRACKING FILE
def write(self,filename=None,path=None):
if filename is None:
filename=time.strftime("%Y%m%d_%H%M-tracker") + EXT
else:
filename=time.strftime("%Y%m%d_%H%M-tracker-") + filename + EXT
if path is None:
path = os.path.join(os.getcwd(),'trackerDATA')
if not os.path.exists(path):
os.makedirs(path)
with open(os.path.join(path,filename),"w") as f:
f.write('rect\t' + str(self.rect[0]) + '\t' + str(self.rect[1]) + '\t' + str(self.rect[2]) + '\t' + str(self.rect[3]) + '\t' + "\n")
for t in self.coos:
f.write(t.tostr() + "\n")
# READS TRACKING FILE
def read(self,filepath=None):
with open(filepath , 'rb') as csvfile:
txt = csv.reader(csvfile,delimiter='\t')
for l in txt:
if l[0] =='rect':
self.rect=(int(l[1]),int(l[2]),int(l[3]),int(l[4]))
continue
self.add(int(l[1]),int(l[2]),t=float(l[0]),info=l[3])
#FIND ARENA BORDERS USING TEMPLATE MATCHING
# returns the boundaries of the arena (x,y,x+w,y+h)
def templateArena(img,template='template.jpg'):
template = cv2.imread(template,0)
wi, he = template.shape[::-1]
method=cv2.TM_CCOEFF_NORMED
res = cv2.matchTemplate(img,template,method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
bottom_right = (top_left[0] + wi, top_left[1] + he)
x,y,w,h = top_left[0],top_left[1],bottom_right[0],bottom_right[1]
return (x,y,w,h)
if __name__=='__main__':
c = coolist(rect=(2,2,10,7))
c.append(coo(5,7,info="first"))
c.append(coo(10,7,info="second"))
print(c.xy())