Mercurial Hosting > traffic-intelligence
comparison python/ubc_utils.py @ 48:8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Thu, 23 Sep 2010 17:24:31 -0400 |
parents | e27598865af3 |
children | 3aed17fc468d |
comparison
equal
deleted
inserted
replaced
47:e27598865af3 | 48:8aed225f71d8 |
---|---|
3 | 3 |
4 import utils | 4 import utils |
5 from moving import MovingObject, TimeInterval, Trajectory | 5 from moving import MovingObject, TimeInterval, Trajectory |
6 | 6 |
7 __metaclass__ = type | 7 __metaclass__ = type |
8 | |
9 delimiterChar = '%'; | |
10 | 8 |
11 def getFileType(s): | 9 def getFileType(s): |
12 filePrefix = utils.removeExtension(s) | 10 filePrefix = utils.removeExtension(s) |
13 i = filePrefix.rfind('-') | 11 i = filePrefix.rfind('-') |
14 if i>0: | 12 if i>0: |
15 return filePrefix[i+1:] | 13 return filePrefix[i+1:] |
16 else: | 14 else: |
17 return '' | 15 return '' |
18 | 16 |
19 def getLines(f): | 17 def saveTrajectoryUserTypes(inFilename, outFilename, objects): |
20 '''Gets a complete entry (all the lines) in between delimiterChar.''' | |
21 dataStrings = [] | |
22 s = utils.readline(f) | |
23 while (len(s) > 0) and (not s.startswith(delimiterChar)): | |
24 dataStrings += [s.strip()] | |
25 s = utils.readline(f) | |
26 return dataStrings | |
27 | |
28 def saveTrajectorieUserTypes(inFilename, outFilename, objects): | |
29 '''The program saves the objects, | 18 '''The program saves the objects, |
30 by just copying the corresponding trajectory and velocity data | 19 by just copying the corresponding trajectory and velocity data |
31 from the inFilename, and saving the characteristics in objects (first line) | 20 from the inFilename, and saving the characteristics in objects (first line) |
32 into outFilename''' | 21 into outFilename''' |
33 infile = utils.openCheck(inFilename) | 22 infile = utils.openCheck(inFilename) |
34 outfile = utils.openCheck(outFilename) | 23 outfile = utils.openCheck(outFilename) |
35 | 24 |
36 if (not infile) | (not outfile): | 25 if (not infile) | (not outfile): |
37 return | 26 return |
38 | 27 |
39 lines = getLines(file) | 28 lines = utils.getLines(infile) |
40 objNum = 0 | 29 objNum = 0 |
41 while lines != []: | 30 while lines != []: |
42 i = 0 | 31 i = 0 |
43 while (i<len(objects)) and (objects[i].num != objNum): | 32 while (i<len(objects)) and (objects[i].num != objNum): |
44 i+=1 | 33 i+=1 |
45 if i<len(objects): | 34 if i<len(objects): |
46 l = lines[0].split(' ') | 35 l = lines[0].split(' ') |
47 outfile.write() | 36 outfile.write() |
48 # next object | 37 # next object |
49 objNum += 1 | 38 objNum += 1 |
39 lines = utils.getLines(infile) | |
50 | 40 |
51 def loadTrajectories(filename, nObjects = -1): | 41 def loadTrajectories(filename, nObjects = -1): |
52 '''Loads trajectories''' | 42 '''Loads trajectories''' |
53 | 43 |
54 file = utils.openCheck(filename) | 44 file = utils.openCheck(filename) |
56 return [] | 46 return [] |
57 | 47 |
58 objects = [] | 48 objects = [] |
59 objNum = 0 | 49 objNum = 0 |
60 objectType = getFileType(filename) | 50 objectType = getFileType(filename) |
61 lines = getLines(file) | 51 lines = utils.getLines(file) |
62 while (lines != []) and ((nObjects<0) or (objNum<nObjects)): | 52 while (lines != []) and ((nObjects<0) or (objNum<nObjects)): |
63 l = lines[0].split(' ') | 53 l = lines[0].split(' ') |
64 parsedLine = [int(n) for n in l[:4]] | 54 parsedLine = [int(n) for n in l[:4]] |
65 obj = MovingObject(num = objNum, timeInterval = TimeInterval(parsedLine[1],parsedLine[2])) | 55 obj = MovingObject(num = objNum, timeInterval = TimeInterval(parsedLine[1],parsedLine[2])) |
66 #add = True | 56 #add = True |
86 objects.append(obj) | 76 objects.append(obj) |
87 objNum+=1 | 77 objNum+=1 |
88 else: | 78 else: |
89 print("Error two lines of data for feature %d"%(f.num)) | 79 print("Error two lines of data for feature %d"%(f.num)) |
90 | 80 |
91 lines = getLines(file) | 81 lines = utils.getLines(file) |
92 | 82 |
93 file.close() | 83 file.close() |
94 return objects | 84 return objects |
95 | 85 |
96 def loadCollisionPoints(filename, nPoints = -1): | 86 def loadCollisionPoints(filename, nPoints = -1): |
100 if (not file): | 90 if (not file): |
101 return [] | 91 return [] |
102 | 92 |
103 points = {} | 93 points = {} |
104 num = 0 | 94 num = 0 |
105 lines = getLines(file) | 95 lines = utils.getLines(file) |
106 while (lines != []) and ((nPoints<0) or (num<nPoints)): | 96 while (lines != []) and ((nPoints<0) or (num<nPoints)): |
107 parsedLine = [int(n) for n in lines[0].split(' ')] | 97 parsedLine = [int(n) for n in lines[0].split(' ')] |
108 protagonistNums = (parsedLine[0], parsedLine[1]) | 98 protagonistNums = (parsedLine[0], parsedLine[1]) |
109 points[protagonistNums] = [[float(n) for n in lines[1].split(' ')], | 99 points[protagonistNums] = [[float(n) for n in lines[1].split(' ')], |
110 [float(n) for n in lines[2].split(' ')]] | 100 [float(n) for n in lines[2].split(' ')]] |
111 | 101 |
112 num+=1 | 102 num+=1 |
113 lines = getLines(file) | 103 lines = utils.getLines(file) |
114 | 104 |
115 file.close() | 105 file.close() |
116 return points | 106 return points |