-
Notifications
You must be signed in to change notification settings - Fork 0
/
T1 - Analise.py
311 lines (265 loc) · 7.88 KB
/
T1 - Analise.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/python
import matplotlib.pyplot as plt
import math
import re
import sys
# Person class
class Person:
name = ''
duration = 0
paths = []
speed = 0.0
def __init__(self, name, duration, paths):
self.name = name
self.duration = duration
self.paths = paths
def calculate_speed(self):
global metrics
time_i = int(self.paths[0].time)
time_f = int(self.paths[len(self.paths)-1].time)
x_i = int(self.paths[0].x)
x_f = int(self.paths[len(self.paths)-1].x)
y_i = int(self.paths[0].y)
y_f = int(self.paths[len(self.paths)-1].y)
distance = float(math.sqrt(math.pow((x_i - x_f),2) + math.pow((y_i - y_f), 2)))
speed = float(float((distance/int(metrics))) / (float((time_f - time_i) / 30))) # Return distance in m/s
return speed
# Path class
class Path:
x = 0
y = 0
time = 0
def __init__(self, x, y, time):
self.x = x
self.y = y
self.time = time
# Group class
class Group:
group_id = 0
elements = []
def __init__(self, group_id, elements):
self.group_id = group_id
self.elements = elements
# GLOBAL VARS
people = []
metrics = 0 # pixels to meter scale
g_id = 0 # group id
groups = []
# Parses trajectory file
def parse_trajectory(file):
lines = file.split('\n')
linePointer = 1 # used to point position inside a line
global metrics
personNumber = 0
for line in lines:
if len(line) > 0:
# Get metrics parameter
if line[0] == '[':
line = line.strip('[')[:-2]
metrics = line
# Get each person from file
else:
parameters = line.split('\t')
paths = split_path(parameters[1])
people.append(Person("Person {}".format(personNumber), parameters[0], paths))
personNumber = personNumber + 1
# Returns the paths parsed correctly
def split_path(allpaths):
unparsedPaths = []
parsedPaths = []
allpaths = allpaths.strip('(')[:-2]
unparsedPaths = allpaths.split(')(')
for path in unparsedPaths:
coordinatesTime = path.split(',')
parsedPaths.append(Path(coordinatesTime[0],coordinatesTime[1],coordinatesTime[2]))
return parsedPaths
# Gets the time of the last frame of the video
def get_last_timestamp():
last = 1
for person in people:
for path in person.paths:
if int(path.time) > last:
last = int(path.time)
return int(last)
# Gets the greater x value
def get_max_x():
max_x = 1
for person in people:
for path in person.paths:
if int(path.x) > max_x:
max_x = int(path.x)
return int(max_x)
# Gets the greater x value
def get_max_y():
max_y = 1
for person in people:
for path in person.paths:
if int(path.y) > max_y:
max_y = int(path.y)
return int(max_y)
# Generate paths png files
def generate_png_files():
last = int(get_last_timestamp())
for i in range(1, last+1):
x=[]
y=[]
for person in people:
for path in person.paths:
if int(path.time) == i:
x.append(int(path.x))
y.append(int(path.y))
plt.plot(x, y, 'bs')
plt.axis([0, get_max_x(), get_max_y(), 0])
file_name = '{}.png'.format(i)
plt.savefig(file_name)
# Detect group formation
def detect_group():
threshold = int(metrics) # maximum distance to consider group formation
for person in people:
for otherPerson in people:
tempGroupTime = 0
if person.name != otherPerson.name: # can't check the same person
for path in person.paths:
for otherPath in otherPerson.paths:
if path.time == otherPath.time:
# calculate distance between them on the same timestamp
if int(path.x) > int (otherPath.x):
x1 = int(path.x)
x2 = int(otherPath.x)
else:
x2 = int(path.x)
x1 = int(otherPath.x)
if int(path.y) > int (otherPath.y):
y1 = int(path.y)
y2 = int(otherPath.y)
else:
y2 = int(path.y)
y1 = int(otherPath.y)
distance = math.sqrt(math.pow((x1 - x2),2) + math.pow((y1 - y2), 2))
if int(distance) < threshold: # group formation detected!
tempGroupTime = tempGroupTime + 1
if tempGroupTime > 48: # solid group
add_group(person.name, otherPerson.name)
# create a group
def add_group(personName, otherPersonName):
global g_id
groupExists = False
shouldCreate = True
# Checks if group already exists
for group in groups:
if personName in group.elements and otherPersonName in group.elements:
groupExists = True
if not groupExists:
# Check if one of them belongs to a group (meaning the other person also belongs to that group)
for group in groups:
if personName in group.elements:
group.elements.append(otherPersonName)
shouldCreate = False
elif otherPersonName in group.elements:
group.elements.append(personName)
shouldCreate = False
if shouldCreate:
elementsTemp = [personName, otherPersonName]
groups.append(Group(g_id, elementsTemp))
g_id = g_id + 1
# ANALISE 1
# Calculates the average person speed as walking in group or walking alone
def med_speed():
groupMedSpeed = 0
gCount = 0
aloneMedSpeed = 0
aCount = 0
groupSpeed = []
aloneSpeed = []
for person in people:
belongsToGroup = False
pSpeed = person.calculate_speed()
for group in groups:
for element in group.elements:
if person.name == element:
belongsToGroup = True
if belongsToGroup:
groupSpeed.append(pSpeed)
else:
aloneSpeed.append(pSpeed)
for speed in groupSpeed:
groupMedSpeed = groupMedSpeed + speed
gCount = gCount + 1
for speed in aloneSpeed:
aloneMedSpeed = aloneMedSpeed + speed
aCount = aCount + 1
print "Velocidade media de pessoas andando em grupos: {} m/s".format(groupMedSpeed/gCount)
print "Velocidade media de pessoas andando sozinhas: {} m/s".format(aloneMedSpeed/aCount)
# ANALISE 2
# Calculates the % of people walking alone and formed groups
def group_alone_ratio():
aCount = 0
gCount = 0
for person in people:
belongsToGroup = False
pSpeed = person.calculate_speed()
for group in groups:
for element in group.elements:
if person.name == element:
belongsToGroup = True
if not belongsToGroup:
aCount = aCount + 1
for group in groups:
gCount = gCount + 1
print "Porcentagem de pessoas andando sozinhas: {}%".format((aCount*100)/(aCount+gCount))
print "Porcentagem de grupos: {}%".format((gCount*100)/(aCount+gCount))
# ANALISE 3
# Calculates the average distance of unknown people
def distance_unknown_people():
totalMedDistance = 0
pCount = 0
global metrics
for person in people:
for otherPerson in people:
shouldCalculate = True
medDistance = 0
medDistanceCount = 0
# Validates if shouldCalculate or not
for group in groups:
for element in group.elements:
if person.name == element:
for otherElement in group.elements:
if otherPerson.name == otherElement or otherElement == element:
shouldCalculate = False
if shouldCalculate:
for path in person.paths:
for otherPath in otherPerson.paths:
if path.time == otherPath.time:
# calculate distance between them on the same timestamp
medDistanceCount = medDistanceCount + 1
if int(path.x) > int (otherPath.x):
x1 = int(path.x)
x2 = int(otherPath.x)
else:
x2 = int(path.x)
x1 = int(otherPath.x)
if int(path.y) > int (otherPath.y):
y1 = int(path.y)
y2 = int(otherPath.y)
else:
y2 = int(path.y)
y1 = int(otherPath.y)
medDistance = medDistance + (math.sqrt(math.pow((x1 - x2),2) + math.pow((y1 - y2), 2)))
if medDistanceCount != 0:
pCount = pCount + 1
totalMedDistance = totalMedDistance + (medDistance / medDistanceCount )
print "Distancia media de pessoas desconhecidas: {} metros".format((totalMedDistance / pCount) / int(metrics))
def main():
if len(sys.argv) != 2:
print "Passagem de parametros deve ser no formato: python T1 - Analise.py <arquivo_Path_D.txt>"
return
global metrics
trajectory = open(sys.argv[1]).read()
parse_trajectory(trajectory)
detect_group()
generate_png_files()
med_speed()
group_alone_ratio()
distance_unknown_people()
if __name__ == '__main__':
main()