Mercurial Hosting > traffic-intelligence
annotate python/ubc_utils.py @ 566:07b1bd0785cd
simplifications to interactions
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 28 Jul 2014 02:57:59 -0400 |
parents | 2f39c4ed0b62 |
children | 15e244d2a1b5 |
rev | line source |
---|---|
13
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
1 #! /usr/bin/env python |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
2 '''Various utilities to load data saved by the UBC tool(s)''' |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
3 |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
329
diff
changeset
|
4 import utils, events |
13
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
5 from moving import MovingObject, TimeInterval, Trajectory |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
6 |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
7 __metaclass__ = type |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
8 |
52
441c8387f34f
changed type detection to load trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
51
diff
changeset
|
9 fileTypeNames = ['feature', |
441c8387f34f
changed type detection to load trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
51
diff
changeset
|
10 'object', |
441c8387f34f
changed type detection to load trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
51
diff
changeset
|
11 'prototype', |
441c8387f34f
changed type detection to load trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
51
diff
changeset
|
12 'contoursequence'] |
441c8387f34f
changed type detection to load trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
51
diff
changeset
|
13 |
57
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
14 severityIndicatorNames = ['Distance', |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
329
diff
changeset
|
15 'Collision Course Cosine', |
57
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
16 'Velocity Cosine', |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
17 'Speed Differential', |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
18 'Collision Probability', |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
19 'Severity Index', |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
329
diff
changeset
|
20 'Time to Collision'] |
57
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
21 |
329
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
306
diff
changeset
|
22 userTypeNames = ['car', |
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
306
diff
changeset
|
23 'pedestrian', |
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
306
diff
changeset
|
24 'twowheels', |
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
306
diff
changeset
|
25 'bus', |
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
306
diff
changeset
|
26 'truck'] |
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
306
diff
changeset
|
27 |
57
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
28 # severityIndicator = {'Distance': 0, |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
29 # 'Cosine': 1, |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
30 # 'Velocity Cosine': 2, |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
31 # 'Speed Differential': 3, |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
32 # 'Collision Probability': 4, |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
33 # 'Severity Index': 5, |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
34 # 'TTC': 6} |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
35 |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
36 mostSevereIsMax = [False, |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
37 False, |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
38 True, |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
39 True, |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
40 True, |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
41 True, |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
42 False] |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
43 |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
44 ignoredValue = [None, None, None, None, None, None, -1] |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
45 |
47
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
46 def getFileType(s): |
52
441c8387f34f
changed type detection to load trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
51
diff
changeset
|
47 'Finds the type in fileTypeNames' |
441c8387f34f
changed type detection to load trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
51
diff
changeset
|
48 for fileType in fileTypeNames: |
441c8387f34f
changed type detection to load trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
51
diff
changeset
|
49 if s.find(fileType)>0: |
441c8387f34f
changed type detection to load trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
51
diff
changeset
|
50 return fileType |
441c8387f34f
changed type detection to load trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
51
diff
changeset
|
51 return '' |
441c8387f34f
changed type detection to load trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
51
diff
changeset
|
52 |
441c8387f34f
changed type detection to load trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
51
diff
changeset
|
53 def isFileType(s, fileType): |
441c8387f34f
changed type detection to load trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
51
diff
changeset
|
54 return (s.find(fileType)>0) |
47
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
55 |
48
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
47
diff
changeset
|
56 def saveTrajectoryUserTypes(inFilename, outFilename, objects): |
47
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
57 '''The program saves the objects, |
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
58 by just copying the corresponding trajectory and velocity data |
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
59 from the inFilename, and saving the characteristics in objects (first line) |
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
60 into outFilename''' |
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
61 infile = utils.openCheck(inFilename) |
51
3aed17fc468d
utils to save object types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
62 outfile = utils.openCheck(outFilename,'w') |
47
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
63 |
51
3aed17fc468d
utils to save object types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
64 if (inFilename.find('features') >= 0) or (not infile) or (not outfile): |
47
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
65 return |
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
66 |
48
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
47
diff
changeset
|
67 lines = utils.getLines(infile) |
53 | 68 objNum = 0 # in inFilename |
47
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
69 while lines != []: |
53 | 70 # find object in objects (index i) |
47
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
71 i = 0 |
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
72 while (i<len(objects)) and (objects[i].num != objNum): |
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
73 i+=1 |
53 | 74 |
47
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
75 if i<len(objects): |
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
76 l = lines[0].split(' ') |
51
3aed17fc468d
utils to save object types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
77 l[3] = str(objects[i].userType) |
3aed17fc468d
utils to save object types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
78 outfile.write(' '.join(l)+'\n') |
3aed17fc468d
utils to save object types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
79 for l in lines[1:]: |
3aed17fc468d
utils to save object types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
80 outfile.write(l+'\n') |
3aed17fc468d
utils to save object types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
81 outfile.write(utils.delimiterChar+'\n') |
47
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
82 # next object |
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
83 objNum += 1 |
48
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
47
diff
changeset
|
84 lines = utils.getLines(infile) |
47
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
85 |
51
3aed17fc468d
utils to save object types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
86 print('read {0} objects'.format(objNum)) |
3aed17fc468d
utils to save object types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
87 |
110
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
88 def modifyTrajectoryFile(modifyLines, filenameIn, filenameOut): |
161
41a5853ec495
added utility to offset the time intervales of complete trajectory files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
111
diff
changeset
|
89 '''Reads filenameIn, replaces the lines with the result of modifyLines and writes the result in filenameOut''' |
110
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
90 fileIn = utils.openCheck(filenameIn, 'r', True) |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
91 fileOut = utils.openCheck(filenameOut, "w", True) |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
92 |
111
48e3de4acb65
corrected bugs in file copying functions and added utility to get concatenate feature numbers for a set of objects
Nicolas Saunier <nico@confins.net>
parents:
110
diff
changeset
|
93 lines = utils.getLines(fileIn) |
110
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
94 trajNum = 0 |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
95 while (lines != []): |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
96 modifiedLines = modifyLines(trajNum, lines) |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
97 if modifiedLines: |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
98 for l in modifiedLines: |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
99 fileOut.write(l+"\n") |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
100 fileOut.write(utils.delimiterChar+"\n") |
111
48e3de4acb65
corrected bugs in file copying functions and added utility to get concatenate feature numbers for a set of objects
Nicolas Saunier <nico@confins.net>
parents:
110
diff
changeset
|
101 lines = utils.getLines(fileIn) |
110
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
102 trajNum += 1 |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
103 |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
104 fileIn.close() |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
105 fileOut.close() |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
106 |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
107 def copyTrajectoryFile(keepTrajectory, filenameIn, filenameOut): |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
108 '''Reads filenameIn, keeps the trajectories for which the function keepTrajectory(trajNum, lines) is True |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
109 and writes the result in filenameOut''' |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
110 fileIn = utils.openCheck(filenameIn, 'r', True) |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
111 fileOut = utils.openCheck(filenameOut, "w", True) |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
112 |
111
48e3de4acb65
corrected bugs in file copying functions and added utility to get concatenate feature numbers for a set of objects
Nicolas Saunier <nico@confins.net>
parents:
110
diff
changeset
|
113 lines = utils.getLines(fileIn) |
110
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
114 trajNum = 0 |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
115 while (lines != []): |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
116 if keepTrajectory(trajNum, lines): |
111
48e3de4acb65
corrected bugs in file copying functions and added utility to get concatenate feature numbers for a set of objects
Nicolas Saunier <nico@confins.net>
parents:
110
diff
changeset
|
117 for l in lines: |
110
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
118 fileOut.write(l+"\n") |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
119 fileOut.write(utils.delimiterChar+"\n") |
111
48e3de4acb65
corrected bugs in file copying functions and added utility to get concatenate feature numbers for a set of objects
Nicolas Saunier <nico@confins.net>
parents:
110
diff
changeset
|
120 lines = utils.getLines(fileIn) |
110
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
121 trajNum += 1 |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
122 |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
123 fileIn.close() |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
124 fileOut.close() |
fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
Nicolas Saunier <nico@confins.net>
parents:
67
diff
changeset
|
125 |
13
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
126 def loadTrajectories(filename, nObjects = -1): |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
127 '''Loads trajectories''' |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
128 |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
129 file = utils.openCheck(filename) |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
130 if (not file): |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
131 return [] |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
132 |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
133 objects = [] |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
134 objNum = 0 |
47
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
135 objectType = getFileType(filename) |
48
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
47
diff
changeset
|
136 lines = utils.getLines(file) |
13
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
137 while (lines != []) and ((nObjects<0) or (objNum<nObjects)): |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
138 l = lines[0].split(' ') |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
139 parsedLine = [int(n) for n in l[:4]] |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
140 obj = MovingObject(num = objNum, timeInterval = TimeInterval(parsedLine[1],parsedLine[2])) |
42
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
25
diff
changeset
|
141 #add = True |
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
25
diff
changeset
|
142 if len(lines) >= 3: |
43
6d11d9e7ad4e
methods for trajectories and objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
143 obj.positions = Trajectory.load(lines[1], lines[2]) |
42
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
25
diff
changeset
|
144 if len(lines) >= 5: |
43
6d11d9e7ad4e
methods for trajectories and objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
145 obj.velocities = Trajectory.load(lines[3], lines[4]) |
52
441c8387f34f
changed type detection to load trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
51
diff
changeset
|
146 if objectType == 'object': |
42
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
25
diff
changeset
|
147 obj.userType = parsedLine[3] |
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
25
diff
changeset
|
148 obj.nObjects = float(l[4]) |
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
25
diff
changeset
|
149 obj.featureNumbers = [int(n) for n in l[5:]] |
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
25
diff
changeset
|
150 |
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
25
diff
changeset
|
151 # load contour data if available |
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
25
diff
changeset
|
152 if len(lines) >= 6: |
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
25
diff
changeset
|
153 obj.contourType = utils.line2Floats(lines[6]) |
43
6d11d9e7ad4e
methods for trajectories and objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
154 obj.contourOrigins = Trajectory.load(lines[7], lines[8]) |
6d11d9e7ad4e
methods for trajectories and objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
155 obj.contourSizes = Trajectory.load(lines[9], lines[10]) |
52
441c8387f34f
changed type detection to load trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
51
diff
changeset
|
156 elif objectType == 'prototype': |
47
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
157 obj.userType = parsedLine[3] |
e27598865af3
modified loadtrajectories to load objects and prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
44
diff
changeset
|
158 obj.nMatchings = int(l[4]) |
13
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
159 |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
160 if len(lines) != 2: |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
161 objects.append(obj) |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
162 objNum+=1 |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
163 else: |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
164 print("Error two lines of data for feature %d"%(f.num)) |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
165 |
48
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
47
diff
changeset
|
166 lines = utils.getLines(file) |
13
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
167 |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
168 file.close() |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
169 return objects |
30559b2cf7a9
minimal code to load UBC data
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
170 |
111
48e3de4acb65
corrected bugs in file copying functions and added utility to get concatenate feature numbers for a set of objects
Nicolas Saunier <nico@confins.net>
parents:
110
diff
changeset
|
171 def getFeatureNumbers(objects): |
48e3de4acb65
corrected bugs in file copying functions and added utility to get concatenate feature numbers for a set of objects
Nicolas Saunier <nico@confins.net>
parents:
110
diff
changeset
|
172 featureNumbers=[] |
48e3de4acb65
corrected bugs in file copying functions and added utility to get concatenate feature numbers for a set of objects
Nicolas Saunier <nico@confins.net>
parents:
110
diff
changeset
|
173 for o in objects: |
48e3de4acb65
corrected bugs in file copying functions and added utility to get concatenate feature numbers for a set of objects
Nicolas Saunier <nico@confins.net>
parents:
110
diff
changeset
|
174 featureNumbers += o.featureNumbers |
48e3de4acb65
corrected bugs in file copying functions and added utility to get concatenate feature numbers for a set of objects
Nicolas Saunier <nico@confins.net>
parents:
110
diff
changeset
|
175 return featureNumbers |
48e3de4acb65
corrected bugs in file copying functions and added utility to get concatenate feature numbers for a set of objects
Nicolas Saunier <nico@confins.net>
parents:
110
diff
changeset
|
176 |
57
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
177 def loadInteractions(filename, nInteractions = -1): |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
178 'Loads interactions from the old UBC traffic event format' |
306
93d851d0d21e
bug correction, minor work on indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
294
diff
changeset
|
179 from events import Interaction |
274
ea2a8e8e4e77
corrected bug from moving interaction classes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
161
diff
changeset
|
180 from indicators import SeverityIndicator |
57
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
181 file = utils.openCheck(filename) |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
182 if (not file): |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
183 return [] |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
184 |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
185 interactions = [] |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
186 interactionNum = 0 |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
187 lines = utils.getLines(file) |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
188 while (lines != []) and ((nInteractions<0) or (interactionNum<nInteractions)): |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
189 parsedLine = [int(n) for n in lines[0].split(' ')] |
61
a8c6d544f015
corrected bug loading Interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
57
diff
changeset
|
190 inter = Interaction(interactionNum, TimeInterval(parsedLine[1],parsedLine[2]), parsedLine[3], parsedLine[4], categoryNum = parsedLine[5]) |
57
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
191 |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
192 indicatorFrameNums = [int(n) for n in lines[1].split(' ')] |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
193 for indicatorNum,line in enumerate(lines[2:]): |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
194 values = {} |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
195 for i,v in enumerate([float(n) for n in line.split(' ')]): |
286
fa95796a76b3
simplified indicators (only non-measured values, whether measurable or not)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
274
diff
changeset
|
196 if not ignoredValue[indicatorNum] or v != ignoredValue[indicatorNum]: |
fa95796a76b3
simplified indicators (only non-measured values, whether measurable or not)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
274
diff
changeset
|
197 values[indicatorFrameNums[i]] = v |
294
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
198 inter.addIndicator(SeverityIndicator(severityIndicatorNames[indicatorNum], values, None, mostSevereIsMax[indicatorNum])) |
57
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
199 |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
200 interactions.append(inter) |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
201 interactionNum+=1 |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
202 lines = utils.getLines(file) |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
203 |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
204 file.close() |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
205 return interactions |
b8b3768f8d54
added functions to load interactions and indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
53
diff
changeset
|
206 |
44
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
207 def loadCollisionPoints(filename, nPoints = -1): |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
208 '''Loads collision points and returns a dict |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
209 with keys as a pair of the numbers of the two interacting objects''' |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
210 file = utils.openCheck(filename) |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
211 if (not file): |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
212 return [] |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
213 |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
214 points = {} |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
215 num = 0 |
48
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
47
diff
changeset
|
216 lines = utils.getLines(file) |
44
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
217 while (lines != []) and ((nPoints<0) or (num<nPoints)): |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
218 parsedLine = [int(n) for n in lines[0].split(' ')] |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
219 protagonistNums = (parsedLine[0], parsedLine[1]) |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
220 points[protagonistNums] = [[float(n) for n in lines[1].split(' ')], |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
221 [float(n) for n in lines[2].split(' ')]] |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
222 |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
223 num+=1 |
48
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
47
diff
changeset
|
224 lines = utils.getLines(file) |
44
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
225 |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
226 file.close() |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
43
diff
changeset
|
227 return points |